Class: WeatherGov::API
- Inherits:
-
Object
- Object
- WeatherGov::API
- Includes:
- HTTParty
- Defined in:
- lib/weather_gov/api.rb
Defined Under Namespace
Classes: RequestError
Instance Method Summary collapse
- #alerts_active(zone: nil, area: nil, region: nil) ⇒ Object
- #format_coord(value) ⇒ Object
- #format_coords(lat, lon) ⇒ Object
- #get(uri: nil, path: nil) ⇒ Object
- #gridpoint(office_id:, grid_x:, grid_y:) ⇒ Object
-
#initialize(user_agent:) ⇒ API
constructor
A new instance of API.
- #office(id:) ⇒ Object
- #point(lat:, lon:) ⇒ Object
- #product(id:) ⇒ Object
- #product_locations(type: nil) ⇒ Object
- #product_types(location: nil) ⇒ Object
- #products(type: nil, location: nil) ⇒ Object
- #station(id:) ⇒ Object
- #zone(type:, id:) ⇒ Object
Constructor Details
#initialize(user_agent:) ⇒ API
Returns a new instance of API.
13 14 15 16 17 |
# File 'lib/weather_gov/api.rb', line 13 def initialize(user_agent:) @headers = { "User-Agent" => user_agent, } end |
Instance Method Details
#alerts_active(zone: nil, area: nil, region: nil) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/weather_gov/api.rb', line 85 def alerts_active(zone: nil, area: nil, region: nil) return get(path: "/alerts/active/zone/#{zone}") if zone return get(path: "/alerts/active/area/#{area}") if area return get(path: "/alerts/active/region/#{region}") if region get(path: "/alerts/active") end |
#format_coord(value) ⇒ Object
19 20 21 |
# File 'lib/weather_gov/api.rb', line 19 def format_coord(value) format("%.2f", value).sub(/(\.[0-9])0+$/, "\\1") end |
#format_coords(lat, lon) ⇒ Object
23 24 25 |
# File 'lib/weather_gov/api.rb', line 23 def format_coords(lat, lon) "#{format_coord(lat)},#{format_coord(lon)}" end |
#get(uri: nil, path: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/weather_gov/api.rb', line 27 def get(uri: nil, path: nil) response = self.class.get(uri || path) unless response.ok? if response.parsed_response error_type_id = Identifier::Problem.new(response.parsed_response.fetch("type")).id error_detail = response.parsed_response.fetch("detail", response.code) raise RequestError, "#{error_type_id}: #{error_detail}" end raise RequestError, "Error: Code #{response.code}" end response end |
#gridpoint(office_id:, grid_x:, grid_y:) ⇒ Object
55 56 57 |
# File 'lib/weather_gov/api.rb', line 55 def gridpoint(office_id:, grid_x:, grid_y:) get(path: "/gridpoints/#{office_id}/#{grid_x},#{grid_y}") end |
#office(id:) ⇒ Object
47 48 49 |
# File 'lib/weather_gov/api.rb', line 47 def office(id:) get(path: "/offices/#{id}") end |
#point(lat:, lon:) ⇒ Object
51 52 53 |
# File 'lib/weather_gov/api.rb', line 51 def point(lat:, lon:) get(path: "/points/#{format_coords(lat, lon)}") end |
#product(id:) ⇒ Object
81 82 83 |
# File 'lib/weather_gov/api.rb', line 81 def product(id:) get(path: "/products/#{id}") end |
#product_locations(type: nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/weather_gov/api.rb', line 63 def product_locations(type: nil) return get(path: "/products/types/#{type}/locations") if type get(path: "/products/locations") end |
#product_types(location: nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/weather_gov/api.rb', line 69 def product_types(location: nil) return get(path: "/products/locations/#{location}/types") if location get(path: "/products/types") end |
#products(type: nil, location: nil) ⇒ Object
75 76 77 78 79 |
# File 'lib/weather_gov/api.rb', line 75 def products(type: nil, location: nil) return get(path: "/products/types/#{type}/locations/#{location}") if type && location get(path: "/products") end |
#station(id:) ⇒ Object
43 44 45 |
# File 'lib/weather_gov/api.rb', line 43 def station(id:) get(path: "/stations/#{id}") end |
#zone(type:, id:) ⇒ Object
59 60 61 |
# File 'lib/weather_gov/api.rb', line 59 def zone(type:, id:) get(path: "/zones/#{type}/#{id}") end |