diff --git a/bin/smsd b/bin/smsd new file mode 100755 index 0000000..37d4075 --- /dev/null +++ b/bin/smsd @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +require 'smsd' + +SMSd::CLI.new(ARGV) diff --git a/lib/smsd.rb b/lib/smsd.rb index 96abdb7..0c57d07 100644 --- a/lib/smsd.rb +++ b/lib/smsd.rb @@ -1,5 +1,18 @@ -require "smsd/version" +require 'i18n' -module Smsd - # Your code goes here... +require 'smsd/version' +require 'smsd/cli' +require 'smsd/cli/options' +require 'smsd/answering_machine' +require 'smsd/answering_machine/action' + +module SMSd + def self.init_i18n + I18n.load_path = Dir[File.join(File.dirname(__FILE__), + '..', 'locale', '*.yml')] + end + + def self.locale=(locale) + I18n.locale = locale + end end diff --git a/lib/smsd/answering_machine.rb b/lib/smsd/answering_machine.rb new file mode 100644 index 0000000..ef492ab --- /dev/null +++ b/lib/smsd/answering_machine.rb @@ -0,0 +1,22 @@ +module SMSd + class AnsweringMachine + attr_accessor :actions, :default_action + + def initialize(default_answer = nil, &block) + self.actions = [] + self.default_action = Action.new(nil, default_answer, &block) + end + + def add_action(regexp, answer = nil, &block) + self.actions << Action.new(regexp, answer, &block) + end + + def execute(from, to, message) + actions.each do |action| + return action.answer(from, to, message) if message =~ action.regexp + end + + default_action.answer(from, to, message) + end + end +end diff --git a/lib/smsd/answering_machine/action.rb b/lib/smsd/answering_machine/action.rb new file mode 100644 index 0000000..77ea6c3 --- /dev/null +++ b/lib/smsd/answering_machine/action.rb @@ -0,0 +1,21 @@ +module SMSd + class AnsweringMachine + class Action + attr_accessor :regexp, :action + + def initialize(regexp, answer = nil, &block) + self.regexp = regexp + self.action = (block_given? ? block : answer) + end + + def answer(from, to, message) + case action + when String + action + when Proc + action.call(from, to, message) + end + end + end + end +end diff --git a/lib/smsd/cli.rb b/lib/smsd/cli.rb new file mode 100644 index 0000000..1c0a390 --- /dev/null +++ b/lib/smsd/cli.rb @@ -0,0 +1,25 @@ +module SMSd + class CLI + attr_accessor :machine, :options + + def initialize(args) + self.options = Options.parse(args) + + SMSd.init_i18n + SMSd.locale = options[:locale] || :fr + + define_actions + + puts machine.execute(ARGV[0], ARGV[1], ARGV[2]) + end + + def define_actions + self.machine = AnsweringMachine.new(I18n.t(:default_answer)) + + machine.add_action(/bonjour/i, 'Bonjour !!') + machine.add_action(/quoi/i) do |from, to, message| + I18n.t(:what, from: from, to: to, message: message) + end + end + end +end diff --git a/lib/smsd/cli/options.rb b/lib/smsd/cli/options.rb new file mode 100644 index 0000000..1cbc447 --- /dev/null +++ b/lib/smsd/cli/options.rb @@ -0,0 +1,39 @@ +require 'optparse' + +module SMSd + class CLI + class Options + def self.parse(args) + options = {} + + parser = ::OptionParser.new do |opts| + opts.banner = "Usage: smsd [options]" + + opts.separator "" + opts.separator "Specific options:" + + opts.on('-l', '--locale LOCALE', + 'Define the language of the script') do |locale| + options[:locale] = locale.to_sym + end + + opts.separator "" + opts.separator "Common options:" + + opts.on('-h', '--help', 'Show this message') do + puts opts + exit + end + + opts.on_tail('--version', 'Show version') do + puts "#{opts.program_name} #{SMSd::VERSION}" + exit + end + end + + parser.parse!(args) + options + end + end + end +end diff --git a/lib/smsd/version.rb b/lib/smsd/version.rb index fcc01ac..45ca28a 100644 --- a/lib/smsd/version.rb +++ b/lib/smsd/version.rb @@ -1,3 +1,3 @@ -module Smsd +module SMSd VERSION = "0.0.1" end diff --git a/locale/en.yml b/locale/en.yml new file mode 100644 index 0000000..0f31a25 --- /dev/null +++ b/locale/en.yml @@ -0,0 +1,3 @@ +en: + default_answer: "I didn't understand your message" + what: 'The phone number %{from} wrote "%{message} to %{to}' diff --git a/locale/fr.yml b/locale/fr.yml new file mode 100644 index 0000000..755b8d9 --- /dev/null +++ b/locale/fr.yml @@ -0,0 +1,3 @@ +fr: + default_answer: "Je n'ai pas compris votre message" + what: 'Le numéro %{from} a écrit "%{message}" au numéro %{to}' diff --git a/smsd.gemspec b/smsd.gemspec index 267423a..266372f 100644 --- a/smsd.gemspec +++ b/smsd.gemspec @@ -5,7 +5,7 @@ require 'smsd/version' Gem::Specification.new do |spec| spec.name = "smsd" - spec.version = Smsd::VERSION + spec.version = SMSd::VERSION spec.authors = ["Guillaume DOTT"] spec.email = ["guillaume.dott@lafourmi-immo.com"] spec.description = %q{TODO: Write a gem description} @@ -18,6 +18,8 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] + spec.add_dependency 'i18n' + spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" end