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:
-
`electoral_districts!`
The following class methods must be called in sub-classes:
-
`http_method`
-
`path`
Public Class Methods
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
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
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
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
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
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
@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
@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
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
@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