class GovKit::CA::Represent

A Ruby wrapper for the Represent API. @see represent.opennorth.ca/api/

Public Class Methods

new(connection = nil) click to toggle source

@param [Faraday::Connection] connection a Faraday connection

# File lib/gov_kit-ca/represent.rb, line 7
def initialize(connection = nil)
  @client = connection || Faraday
end

Public Instance Methods

boundaries(opts = {}) click to toggle source

Get boundaries.

@param [Hash] opts optional arguments @option opts [String] :boundary_set a boundary set @option opts [String] :boundary a boundary @option opts [Boolean] :representatives @option opts [Array<String>,String] :sets comma-separated list of boundary sets @option opts [Array<Float>,String] :contains comma-separated latitude and longitude @option opts [String] :touches a boundary @option opts [String] :intersects a boundary @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#boundary

# File lib/gov_kit-ca/represent.rb, line 35
def boundaries(opts = {})
  if Array === opts[:sets]
    opts[:sets] = opts[:sets].join(',')
  end
  if Array === opts[:contains]
    opts[:contains] = opts[:contains].join(',')
  end
  if opts.has_key?(:boundary) && !opts.has_key?(:boundary_set)
    raise ArgumentError, ':boundary_set must be set if :boundary is set'
  end
  if opts[:representatives] && !(opts.has_key?(:boundary) && opts.has_key?(:boundary_set))
    raise ArgumentError, ':boundary_set and :boundary must be set if :representatives is true'
  end
  request(['boundaries', opts.delete(:boundary_set), opts.delete(:boundary), opts.delete(:representatives) && 'representatives'], opts)
end
boundary_sets(opts = {}) click to toggle source

Get boundary sets.

@param [Hash] opts optional arguments @option opts [String] :boundary_set a boundary set @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#boundaryset

# File lib/gov_kit-ca/represent.rb, line 18
def boundary_sets(opts = {})
  request(['boundary-sets', opts.delete(:boundary_set)], opts)
end
candidates(opts = {}) click to toggle source

Get candidates.

@param [Hash] opts optional arguments @option opts [String] :election an election @option opts [Array<Float>,String] :point a comma-separated latitude and longitude @option opts [Array<Strong>,String] :districts a comma-separated list of boundaries @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#candidate

# File lib/gov_kit-ca/represent.rb, line 115
def candidates(opts = {})
  if Array === opts[:point]
    opts[:point] = opts[:point].join(',')
  end
  if Array === opts[:districts]
    opts[:districts] = opts[:districts].join(',')
  end
  request(['candidates', opts.delete(:election)], opts)
end
elections(opts = {}) click to toggle source

Get elections.

@param [Hash] opts optional arguments @option opts [String] :election an election @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#election

# File lib/gov_kit-ca/represent.rb, line 83
def elections(opts = {})
  request ['elections', opts.delete(:election)], opts
end
postcodes(postcode, opts = {}) click to toggle source

Get boundaries and representatives by postal code.

@param [Hash] opts optional arguments @option opts [Array<String>,String] :sets comma-separated list of boundary sets @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#postcode

# File lib/gov_kit-ca/represent.rb, line 58
def postcodes(postcode, opts = {})
  if Array === opts[:sets]
    opts[:sets] = opts[:sets].join(',')
  end
  request(['postcodes', PostalCode.format_postal_code(postcode)], opts)
end
representative_sets(opts = {}) click to toggle source

Get representative sets.

@param [Hash] opts optional arguments @option opts [String] :representative_set a representative set @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#representativeset

# File lib/gov_kit-ca/represent.rb, line 72
def representative_sets(opts = {})
  request ['representative-sets', opts.delete(:representative_set)], opts
end
representatives(opts = {}) click to toggle source

Get representatives.

@param [Hash] opts optional arguments @option opts [String] :representative_set a representative set @option opts [Array<Float>,String] :point a comma-separated latitude and longitude @option opts [Array<Strong>,String] :districts a comma-separated list of boundaries @option opts [Integer] :limit @option opts [Integer] :offset @see represent.opennorth.ca/api/#representative

# File lib/gov_kit-ca/represent.rb, line 96
def representatives(opts = {})
  if Array === opts[:point]
    opts[:point] = opts[:point].join(',')
  end
  if Array === opts[:districts]
    opts[:districts] = opts[:districts].join(',')
  end
  request(['representatives', opts.delete(:representative_set)], opts)
end

Private Instance Methods

request(parts, opts) click to toggle source
# File lib/gov_kit-ca/represent.rb, line 127
def request(parts, opts)
  begin
    url = "https://represent.opennorth.ca/#{parts.compact.join('/')}/"
    response = @client.get(url, opts)
    case response.status
    when 200
      JSON.parse(response.body)
    when 404
      raise ResourceNotFound, "#{response.status} #{url}?#{opts.map{|k,v| "#{k}=#{v}"}.join('&')}"
    else
      raise InvalidRequest, "#{response.status} #{url}?#{opts.map{|k,v| "#{k}=#{v}"}.join('&')} #{response.body}"
    end
  end
end