Class: Daywalker::District

Inherits:
Base
  • Object
show all
Includes:
HappyMapper
Defined in:
lib/daywalker/district.rb

Overview

Represents a Congressional district.

They have the following attributes:

  • number

  • state (as a two-letter abbreviation)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ District

Returns a new instance of District.



57
58
59
60
61
# File 'lib/daywalker/district.rb', line 57

def initialize(attributes = {})
  attributes.each do |attribute, value|
    send("#{attribute}=", value)
  end
end

Class Method Details

.all_by_zipcode(zip) ⇒ Object

Finds all districts for a specific zip code.

Returns an Array of Districts. Raises ArgumentError if you omit the zip.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/daywalker/district.rb', line 33

def self.all_by_zipcode(zip)
  raise(ArgumentError, 'missing required parameter zip') if zip.nil?

  query = {
    :zip => zip,
    :apikey => Daywalker.api_key
  }
  response = get('/districts.getDistrictsFromZip.xml', :query => query)

  handle_response(response)
end

.unique_by_address(address) ⇒ Object

Find the district for a specific address.

Returns a District.

Raises Daywalker::AddressError if the address can’t be geocoded.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
# File 'lib/daywalker/district.rb', line 50

def self.unique_by_address(address)
  raise(ArgumentError, 'missing required parameter address') if address.nil?
  location = Daywalker.geocoder.locate(address)

  unique_by_latitude_and_longitude(location[:latitude], location[:longitude])
end

.unique_by_latitude_and_longitude(latitude, longitude) ⇒ Object

Find the district for a specific latitude and longitude.

Returns a District. Raises ArgumentError if you omit latitude or longitude.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/daywalker/district.rb', line 17

def self.unique_by_latitude_and_longitude(latitude, longitude)
  raise(ArgumentError, 'missing required parameter latitude') if latitude.nil?
  raise(ArgumentError, 'missing required parameter longitude') if longitude.nil?

  query = {
    :latitude => latitude,
    :longitude => longitude,
    :apikey => Daywalker.api_key
  }
  response = get('/districts.getDistrictFromLatLong.xml', :query => query)
  handle_response(response).first
end

Instance Method Details

#legislatorsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/daywalker/district.rb', line 63

def legislators
  @legislators ||= begin
    representative = Legislator.unique_by_state_and_district(state, number)
    junior_senator = Legislator.unique_by_state_and_district(state, :junior_seat)
    senior_senator = Legislator.unique_by_state_and_district(state, :senior_seat)

    {
      :representative => representative,
      :junior_senator => junior_senator,
      :senior_senator => senior_senator
    }
  end
end