Class: Ferryman::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ferryman.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



78
79
80
81
82
# File 'lib/ferryman.rb', line 78

def initialize
  @providers = {}
  @rules = []
  @arity = nil
end

Instance Attribute Details

#providersObject (readonly)

Returns the value of attribute providers.



76
77
78
# File 'lib/ferryman.rb', line 76

def providers
  @providers
end

#rulesObject (readonly)

Returns the value of attribute rules.



76
77
78
# File 'lib/ferryman.rb', line 76

def rules
  @rules
end

Instance Method Details

#add_provider(name, provider) ⇒ Object

Raises:



84
85
86
87
88
89
90
91
92
93
# File 'lib/ferryman.rb', line 84

def add_provider(name, provider)
  raise ConfigError.new("Provider #{name} already exists") if @providers.has_key?(name)
  raise ConfigError.new("Provider must accept at least a phone number") if provider.arity < 1
  raise ConfigError.new("Provider arguments does not match with existing") unless @arity.nil? || provider.arity == @arity

  @arity = provider.arity
  @providers[name] = provider

  true
end

#set_routing_table(table) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ferryman.rb', line 95

def set_routing_table(table)
  table.each do |row|
    raise ConfigError.new('providers cannot be nil!') if row[:providers].nil?

    rule = RoutingRule.new
    rule.set_match row[:match]
    rule.strategy = row.fetch(:strategy, 'first')
    row[:providers].each {|provider| rule.add_provider(provider)}
    @rules << rule
  end

  true
end