Class: WeatherGov::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/weather_gov/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent:) ⇒ Client

Returns a new instance of Client.



18
19
20
# File 'lib/weather_gov/client.rb', line 18

def initialize(user_agent:)
  @api = API.new(user_agent: user_agent)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



16
17
18
# File 'lib/weather_gov/client.rb', line 16

def api
  @api
end

Instance Method Details

#alerts_active(data: nil, zone: nil, area: nil, region: nil) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/weather_gov/client.rb', line 68

def alerts_active(data: nil, zone: nil, area: nil, region: nil)
  return AlertCollection.new(client: self, data: data) if data

  if zone || area || region
    return AlertCollection.new(
      client: self,
      data: -> { json_for(api.alerts_active(zone: zone, area: area, region: region)) }
    )
  end

  raise ArgumentError, "data, zone, area, or region parameter required"
end

#forecast(data: nil, uri: nil) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
# File 'lib/weather_gov/client.rb', line 89

def forecast(data: nil, uri: nil)
  return Forecast.new(client: self, data: data) if data
  return Forecast.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri

  raise ArgumentError, "data or uri parameter required"
end

#gridpoint(data: nil, uri: nil) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/weather_gov/client.rb', line 61

def gridpoint(data: nil, uri: nil)
  return Gridpoint.new(client: self, data: data) if data
  return Gridpoint.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri

  raise ArgumentError, "data, uri, or lat and lon parameters required"
end

#json_for(response) ⇒ Object



22
23
24
25
26
# File 'lib/weather_gov/client.rb', line 22

def json_for(response)
  return response.parsed_response unless response.parsed_response.include?("status")

  raise response.parsed_response.fetch("title", "Unknown Error")
end

#office(data: nil, uri: nil, id: nil) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
# File 'lib/weather_gov/client.rb', line 45

def office(data: nil, uri: nil, id: nil)
  return Office.new(client: self, data: data) if data
  return Office.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri
  return Office.new(client: self, data: -> { json_for(api.office(id: id)) }) if id

  raise ArgumentError, "data, uri, or id parameter required"
end

#point(data: nil, uri: nil, lat: nil, lon: nil) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
# File 'lib/weather_gov/client.rb', line 53

def point(data: nil, uri: nil, lat: nil, lon: nil)
  return Point.new(client: self, data: data) if data
  return Point.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri
  return Point.new(client: self, data: -> { json_for(api.point(lat: lat, lon: lon)) }) if lat && lon

  raise ArgumentError, "data, uri, or lat and lon parameters required"
end

#product(data: nil, uri: nil, id: nil) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
# File 'lib/weather_gov/client.rb', line 107

def product(data: nil, uri: nil, id: nil)
  return Product.new(client: self, data: data) if data
  return Product.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri
  return Product.new(client: self, data: -> { json_for(api.product(id: id)) }) if id

  raise ArgumentError, "data, uri, or id parameter required"
end

#products(data: nil, uri: nil, type: nil, location: nil) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
105
# File 'lib/weather_gov/client.rb', line 96

def products(data: nil, uri: nil, type: nil, location: nil)
  return ProductList.new(client: self, data: data) if data
  return ProductList.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri

  if type && location
    return ProductList.new(client: self, data: -> { json_for(api.products(type: type, location: location)) })
  end

  raise ArgumentError, "data, uri, or type and location parameters required"
end

#station(data: nil, uri: nil, id: nil) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/weather_gov/client.rb', line 37

def station(data: nil, uri: nil, id: nil)
  return ObservationStation.new(client: self, data: data) if data
  return ObservationStation.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri
  return ObservationStation.new(client: self, data: -> { json_for(api.station(id: id)) }) if id

  raise ArgumentError, "data or id parameter required"
end

#stations(data: nil, uri: nil) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
# File 'lib/weather_gov/client.rb', line 28

def stations(data: nil, uri: nil)
  return ObservationStationCollection.new(client: self, data: data) if data
  return ObservationStationCollection.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri

  # In theory an "all stations" query is okay, but in practice it seems to time out.

  raise ArgumentError, "data or uri parameter required"
end

#zone(data: nil, uri: nil, type: nil, id: nil) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
# File 'lib/weather_gov/client.rb', line 81

def zone(data: nil, uri: nil, type: nil, id: nil)
  return Zone.new(client: self, data: data) if data
  return Zone.new(client: self, data: -> { json_for(api.get(uri: uri)) }) if uri
  return Zone.new(client: self, data: -> { json_for(api.zone(type: type, id: id)) }) if type && id

  raise ArgumentError, "data, uri, or type and id parameter required"
end