class WeatherGov::API

Public Class Methods

new(user_agent:) click to toggle source
# File lib/weather_gov/api.rb, line 13
def initialize(user_agent:)
  @headers = {
    "User-Agent" => user_agent,
  }
end

Public Instance Methods

alerts_active(zone: nil, area: nil, region: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# File lib/weather_gov/api.rb, line 47
def office(id:)
  get(path: "/offices/#{id}")
end
point(lat:, lon:) click to toggle source
# File lib/weather_gov/api.rb, line 51
def point(lat:, lon:)
  get(path: "/points/#{format_coords(lat, lon)}")
end
product(id:) click to toggle source
# File lib/weather_gov/api.rb, line 81
def product(id:)
  get(path: "/products/#{id}")
end
product_locations(type: nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# File lib/weather_gov/api.rb, line 43
def station(id:)
  get(path: "/stations/#{id}")
end
zone(type:, id:) click to toggle source
# File lib/weather_gov/api.rb, line 59
def zone(type:, id:)
  get(path: "/zones/#{type}/#{id}")
end