class GovKit::CA::PostalCode::Strategy::Base

Abstract class for implementing postal code to electoral district strategies.

The following instance methods must be implemented in sub-classes:

The following class methods must be called in sub-classes:

Public Class Methods

new(postal_code) click to toggle source

Creates a new postal code to electoral district strategy. @param [String] postal_code a postal code

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 23
def initialize(postal_code)
  @postal_code = postal_code
end

Private Class Methods

http_method(http_method = nil) click to toggle source

Allows setting an HTTP method to be used for each request. @param [Symbol] http_method an HTTP method @return [Symbol] the HTTP method to use to send the request

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 62
def self.http_method(http_method = nil)
  return default_options[:http_method] unless http_method
  default_options[:http_method] = http_method
end
path(path = nil) click to toggle source

Allows setting an absolute path to be used in each request. @param [String] path an ERB template for the absolute path @return [String] the absolute path to request

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 70
def self.path(path = nil)
  return default_options[:path] unless path
  default_options[:path] = path
end
post_data(post_data = nil) click to toggle source

Allows setting POST data to be sent in each request. @param [String] post_data an ERB template for the POST data @return [String] the POST data for the request

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 83
def self.post_data(post_data = nil)
  return default_options[:post_data] unless post_data
  default_options[:post_data] = post_data
end

Public Instance Methods

electoral_districts() click to toggle source

Returns the electoral districts within a postal code. @return [Array<Fixnum>] the electoral districts within the postal code

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 29
def electoral_districts
  valid? && electoral_districts!.map(&:to_i).sort
end

Private Instance Methods

electoral_districts!() click to toggle source

Returns the electoral districts within a postal code, without passing validation first. @return [Array<Fixnum>] the electoral districts within the postal code

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 38
def electoral_districts!
  raise NotImplementedError
end
path() click to toggle source

@return [String] the absolute path to be used in the request

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 76
def path
  ERB.new(self.class.path).result binding
end
post_data() click to toggle source

@return [String] the POST data for the request

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 89
def post_data
  ERB.new(self.class.post_data).result binding
end
response() click to toggle source

Performs the request and returns the response. @return [HTTParty::Response] a HTTParty response object

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 49
def response
  @response ||= begin
    if self.class.http_method == :post
      self.class.post path, :body => post_data
    else
      self.class.send self.class.http_method, path
    end
  end
end
valid?() click to toggle source

@return [Boolean] whether the electoral districts can be determined

# File lib/gov_kit-ca/postal_code/strategy/base.rb, line 43
def valid?
  true
end