Class: Sunlight::District

Inherits:
Base
  • Object
show all
Defined in:
lib/sunlight/district.rb

Constant Summary

Constants inherited from Base

Base::API_FORMAT, Base::API_URL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key, api_key=, construct_url, get_json_data, hash2get

Constructor Details

#initialize(state, number) ⇒ District

Returns a new instance of District.


7
8
9
10
# File 'lib/sunlight/district.rb', line 7

def initialize(state, number)
  @state = state
  @number = number.to_i.to_s
end

Instance Attribute Details

#numberObject

Returns the value of attribute number


5
6
7
# File 'lib/sunlight/district.rb', line 5

def number
  @number
end

#stateObject

Returns the value of attribute state


5
6
7
# File 'lib/sunlight/district.rb', line 5

def state
  @state
end

Class Method Details

.all_from_zipcode(zipcode) ⇒ Object

Usage:

Sunlight::District.all_from_zipcode(90210)    # returns array of District objects

59
60
61
62
63
64
# File 'lib/sunlight/district.rb', line 59

def self.all_from_zipcode(zipcode)

  url = construct_url("districts.getDistrictsFromZip", {:zip => zipcode})

  districts_from_url(url)
end

.districts_from_url(url) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sunlight/district.rb', line 66

def self.districts_from_url(url)

  if (result = get_json_data(url))

    districts = []
    result["response"]["districts"].each do |district|
      districts << District.new(district["district"]["state"], district["district"]["number"])
    end

    districts

  else  
    nil
  end # if response.class

end

.get(params) ⇒ Object

Usage:

Sunlight::District.get(:latitude => 33.876145, :longitude => -84.453789)    # returns one District object or nil
Sunlight::District.get(:address => "123 Fifth Ave New York, NY")     # returns one District object or nil

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sunlight/district.rb', line 17

def self.get(params)

  if (params[:latitude] and params[:longitude])

    get_from_lat_long(params[:latitude], params[:longitude])

  elsif (params[:address])

    # get the lat/long from Google
    placemarks = Geocoder.search(params[:address])

    unless placemarks.empty?
      placemark = placemarks[0]
      get_from_lat_long(placemark.latitude, placemark.longitude)
    end

  else
    nil # appropriate params not found
  end

end

.get_from_lat_long(latitude, longitude) ⇒ Object

Usage:

Sunlight::District.get_from_lat_long(-123, 123)   # returns District object or nil

44
45
46
47
48
49
50
51
52
# File 'lib/sunlight/district.rb', line 44

def self.get_from_lat_long(latitude, longitude)

  url = construct_url("districts.getDistrictFromLatLong", {:latitude => latitude, :longitude => longitude})

  districts = districts_from_url(url)

  districts ? districts.first : nil

end

.zipcodes_in(state, number) ⇒ Object

Usage:

Sunlight::District.zipcodes_in("NY", 29)     # returns ["14009", "14024", "14029", ...]

88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sunlight/district.rb', line 88

def self.zipcodes_in(state, number)

  url = construct_url("districts.getZipsFromDistrict", {:state => state, :district => number})

  if (result = get_json_data(url))
    result["response"]["zips"]
  else  
    nil
  end # if response.class

end