Add initial classes to manage answering machine
This commit is contained in:
parent
b99915de5f
commit
8543374046
5
bin/smsd
Executable file
5
bin/smsd
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require 'smsd'
|
||||||
|
|
||||||
|
SMSd::CLI.new(ARGV)
|
19
lib/smsd.rb
19
lib/smsd.rb
@ -1,5 +1,18 @@
|
|||||||
require "smsd/version"
|
require 'i18n'
|
||||||
|
|
||||||
module Smsd
|
require 'smsd/version'
|
||||||
# Your code goes here...
|
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
|
end
|
||||||
|
22
lib/smsd/answering_machine.rb
Normal file
22
lib/smsd/answering_machine.rb
Normal file
@ -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
|
21
lib/smsd/answering_machine/action.rb
Normal file
21
lib/smsd/answering_machine/action.rb
Normal file
@ -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
|
25
lib/smsd/cli.rb
Normal file
25
lib/smsd/cli.rb
Normal file
@ -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
|
39
lib/smsd/cli/options.rb
Normal file
39
lib/smsd/cli/options.rb
Normal file
@ -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
|
@ -1,3 +1,3 @@
|
|||||||
module Smsd
|
module SMSd
|
||||||
VERSION = "0.0.1"
|
VERSION = "0.0.1"
|
||||||
end
|
end
|
||||||
|
3
locale/en.yml
Normal file
3
locale/en.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
en:
|
||||||
|
default_answer: "I didn't understand your message"
|
||||||
|
what: 'The phone number %{from} wrote "%{message} to %{to}'
|
3
locale/fr.yml
Normal file
3
locale/fr.yml
Normal file
@ -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}'
|
@ -5,7 +5,7 @@ require 'smsd/version'
|
|||||||
|
|
||||||
Gem::Specification.new do |spec|
|
Gem::Specification.new do |spec|
|
||||||
spec.name = "smsd"
|
spec.name = "smsd"
|
||||||
spec.version = Smsd::VERSION
|
spec.version = SMSd::VERSION
|
||||||
spec.authors = ["Guillaume DOTT"]
|
spec.authors = ["Guillaume DOTT"]
|
||||||
spec.email = ["guillaume.dott@lafourmi-immo.com"]
|
spec.email = ["guillaume.dott@lafourmi-immo.com"]
|
||||||
spec.description = %q{TODO: Write a gem description}
|
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.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
||||||
spec.require_paths = ["lib"]
|
spec.require_paths = ["lib"]
|
||||||
|
|
||||||
|
spec.add_dependency 'i18n'
|
||||||
|
|
||||||
spec.add_development_dependency "bundler", "~> 1.3"
|
spec.add_development_dependency "bundler", "~> 1.3"
|
||||||
spec.add_development_dependency "rake"
|
spec.add_development_dependency "rake"
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user