Class: Ferryman::RoutingRule
- Inherits:
-
Object
- Object
- Ferryman::RoutingRule
- Defined in:
- lib/ferryman.rb
Constant Summary collapse
- MATCH_ALL =
lambda {|_| true}
- STRATEGIES =
{ 'first' => lambda {|providers| providers}, 'random' => lambda {|providers| providers.shuffle} }
Instance Attribute Summary collapse
-
#providers ⇒ Object
readonly
Returns the value of attribute providers.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
Instance Method Summary collapse
- #accept?(phone_number) ⇒ Boolean
- #add_provider(provider) ⇒ Object
-
#initialize ⇒ RoutingRule
constructor
A new instance of RoutingRule.
- #set_match(match) ⇒ Object
Constructor Details
#initialize ⇒ RoutingRule
Returns a new instance of RoutingRule.
22 23 24 25 26 |
# File 'lib/ferryman.rb', line 22 def initialize @providers = [] @match_rules = [] @strategy = 'first' end |
Instance Attribute Details
#providers ⇒ Object (readonly)
Returns the value of attribute providers.
14 15 16 |
# File 'lib/ferryman.rb', line 14 def providers @providers end |
#strategy ⇒ Object
Returns the value of attribute strategy.
13 14 15 |
# File 'lib/ferryman.rb', line 13 def strategy @strategy end |
Instance Method Details
#accept?(phone_number) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ferryman.rb', line 28 def accept?(phone_number) matched = true @match_rules.each do |rule| matched &&= rule.call(phone_number) break unless matched end matched end |
#add_provider(provider) ⇒ Object
63 64 65 66 |
# File 'lib/ferryman.rb', line 63 def add_provider(provider) raise ConfigError.new('provider cannot be nil!') if provider.nil? @providers << provider end |
#set_match(match) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ferryman.rb', line 39 def set_match(match) raise ConfigError.new('match cannot be nil!') if match.nil? if match == '*' @match_rules << MATCH_ALL elsif match.respond_to? 'has_key?' if match.has_key?(:regions) regions = Array.new(match[:regions]) @match_rules << lambda do |number| parsed_number = GlobalPhone.parse(number) regions.include? parsed_number.territory.name end end else raise ConfigError.new("Unexpected value for 'match'") end true end |