Module: GovKit::CA::PostalCode::StrategySet

Defined in:
lib/gov_kit-ca/postal_code/strategy_set.rb

Overview

The set of postal code to electoral district strategies.

Class Method Summary collapse

Class Method Details

.register(strategy) ⇒ Array<Strategy::Base>

Adds a strategy to the strategy set.

Parameters:

Returns:



15
16
17
# File 'lib/gov_kit-ca/postal_code/strategy_set.rb', line 15

def self.register(strategy)
  strategies << strategy unless strategies.include?(strategy)
end

.run(postal_code) ⇒ Array<Fixnum>

Runs through the strategies in order of registration. Returns the output of the first strategy to successfully determine electoral districts from a postal code.

Parameters:

  • postal_code (String)

    a postal code

Returns:

  • (Array<Fixnum>)

    the electoral districts within the postal code

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gov_kit-ca/postal_code/strategy_set.rb', line 25

def self.run(postal_code)
  strategies.each do |strategy|
    begin
      electoral_districts = strategy.new(postal_code).electoral_districts
      return electoral_districts if electoral_districts
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
      Errno::ETIMEDOUT, EOFError, Net::HTTPBadResponse,
      Net::HTTPHeaderSyntaxError, Net::ProtocolError
      # Do nothing. Continue.
    end
  end

  raise ResourceNotFound
end

.strategiesArray<Strategy::Base>

Stores the strategy set.

Returns:



8
9
10
# File 'lib/gov_kit-ca/postal_code/strategy_set.rb', line 8

def self.strategies
  @@strategies ||= []
end