Class: Sunlight::District
Constant Summary
Constants inherited from Base
Base::API_FORMAT, Base::API_URL
Instance Attribute Summary collapse
-
#number ⇒ Object
Returns the value of attribute number.
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
-
.all_from_zipcode(zipcode) ⇒ Object
Usage: Sunlight::District.all_from_zipcode(90210) # returns array of District objects.
-
.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.
-
.get_from_lat_long(latitude, longitude) ⇒ Object
Usage: Sunlight::District.get_from_lat_long(-123, 123) # returns District object or nil.
-
.zipcodes_in(state, number) ⇒ Object
Usage: Sunlight::District.zipcodes_in(“NY”, 29) # returns [“14009”, “14024”, “14029”, …].
Instance Method Summary collapse
-
#initialize(state, number) ⇒ District
constructor
A new instance of District.
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 end |
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
5 6 7 |
# File 'lib/sunlight/district.rb', line 5 def number @number end |
#state ⇒ Object
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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sunlight/district.rb', line 68 def self.all_from_zipcode(zipcode) url = construct_url("districts.getDistrictsFromZip", {:zip => zipcode}) 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
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 = Geocoding::get(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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sunlight/district.rb', line 44 def self.get_from_lat_long(latitude, longitude) url = construct_url("districts.getDistrictFromLatLong", {:latitude => latitude, :longitude => longitude}) if (result = get_json_data(url)) districts = [] result["response"]["districts"].each do |district| districts << District.new(district["district"]["state"], district["district"]["number"]) end districts.first else nil end # if response.class end |
.zipcodes_in(state, number) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sunlight/district.rb', line 92 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 |