Module: Stormglass
- Defined in:
- lib/stormglass.rb,
lib/stormglass/version.rb,
lib/stormglass/result_dict.rb
Defined Under Namespace
Classes: AlternateValues, ConnectionError, Error, ExceededLimitError, Hour, Response, Subvalue, Value
Constant Summary collapse
- VERSION =
"0.2.0"
- RESULT_DICT =
{ :air_temperature =>{:unit_type => "C", :unit=>"C", :description=>"Air temperature as", :unit_description =>"degrees Celsius"}, :air_pressure =>{:unit_type => "hPa", :unit=>"hPa", :description=>"Air pressure as", :unit_description =>"hectopascal"}, :cloud_cover =>{:unit_type => "%", :unit=>"%", :description=>"Total cloud coverage as", :unit_description =>"percent"}, :current_direction =>{:unit_type => "Deg", :unit=>"°", :description=>"Direction of current", :unit_description =>"0° indicates coming from north"}, :current_speed =>{:unit_type => "Ms", :unit=>"M/s", :description=>"Speed of current as", :unit_description =>"meters per second"}, :gust =>{:unit_type => "Ms", :unit=>"M/s", :description=>"Wind gust as", :unit_description =>"meters per second"}, :humidity =>{:unit_type => "%", :unit=>"%", :description=>"Relative humidity as", :unit_description => "percent"}, :ice_cover =>{:unit_type => "/1", :unit=>"/1", :description=>"Proportion", :unit_description=> "over 1"}, :precipitation =>{:unit_type => "Kgm2", :unit=>"kg/m²", :description=>"Mean precipitation as", :unit_description => "kilogram per square meter"}, :sea_level =>{:unit_type => "M", :unit=>"M", :description=>"Height of sea level as", :unit_description =>"meters"}, :snow_depth =>{:unit_type => "M", :unit=>"M", :description=>"Depth of snow as", :unit_description =>"meters"}, :swell_direction =>{:unit_type => "Deg", :unit=>"°", :description=>"Direction of swell waves.", :unit_description => "0° indicates coming from north"}, :swell_height =>{:unit_type => "M", :unit=>"M", :description=>"Height of swell waves as", :unit_description =>"meters"}, :swell_period =>{:unit_type => "Sec", :unit=>"s", :description=>"Period of swell waves as", :unit_description =>"seconds"}, :visibility =>{:unit_type => "Km", :unit=>"km", :description=>"Horizontal visibility as", :unit_description =>"Kilometer"}, :water_temperature =>{:unit_type => "C", :unit=>"C", :description=>"Water temperature as", :unit_description =>"degrees Celsius"}, :wave_direction =>{:unit_type => "Deg", :unit=>"°", :description=>"Direction of combined wind and swell waves.",:unit_description => "0° indicates coming from north"}, :wave_height =>{:unit_type => "M", :unit=>"M", :description=>"Height of combined wind and swell waves as", :unit_description =>"meters"}, :wave_period =>{:unit_type => "Sec", :unit=>"s", :description=>"Period of combined wind and swell waves as", :unit_description =>"seconds"}, :wind_wave_direction=>{:unit_type => "Deg", :unit=>"°", :description=>"Direction of wind waves.", :unit_description => "0° indicates coming from north"}, :wind_wave_height =>{:unit_type => "M", :unit=>"M", :description=>"Height of wind waves as", :unit_description =>"meters"}, :wind_wave_period =>{:unit_type => "Sec", :unit=>"s", :description=>"Period of wind waves as", :unit_description =>"seconds"}, :wind_direction =>{:unit_type => "Deg", :unit=>"°", :description=>"Direction of wind.", :unit_description => "0° indicates coming from north"}, :wind_speed =>{:unit_type => "Ms", :unit=>"M/s", :description=>"Speed of wind as", :unit_description =>"meters per second"} }
Class Attribute Summary collapse
-
.settings ⇒ Object
Returns the value of attribute settings.
Class Method Summary collapse
-
.api_key ⇒ Object
API key for stormglass.
- .configure {|settings| ... } ⇒ Object
-
.for_address(address_string, params = {}) ⇒ Object
lookup an address (such as city + zip) and get the coordinates for the first match.
-
.for_lat_lng(lat:, lng:, params: {}) ⇒ Object
query StormGlass given lat/lng.
- .hours_offset(start_time, hours = 12) ⇒ Object
- .query_time_string(datetime) ⇒ Object
-
.request(endpoint: 'point', params: {}) ⇒ Object
Primary interface to StormGlass.
- .set_settings ⇒ Object
Class Attribute Details
.settings ⇒ Object
Returns the value of attribute settings.
26 27 28 |
# File 'lib/stormglass.rb', line 26 def settings @settings end |
Class Method Details
.api_key ⇒ Object
API key for stormglass. key is sourced:
-
if set via enviroment variable
-
if set in configuration block
-
if defined in Rails secrets
-
when passed directly to parameters
88 89 90 91 92 |
# File 'lib/stormglass.rb', line 88 def self.api_key key = Stormglass.settings.api_key key ||= Rails.application.credentials[:stormglass_api_key] if Gem.loaded_specs.has_key?('rails') key end |
.configure {|settings| ... } ⇒ Object
28 29 30 31 |
# File 'lib/stormglass.rb', line 28 def configure self.settings ||= Configuration.new yield(settings) end |
.for_address(address_string, params = {}) ⇒ Object
lookup an address (such as city + zip) and get the coordinates for the first match
39 40 41 42 43 44 45 46 |
# File 'lib/stormglass.rb', line 39 def self.for_address(address_string, params={}) if results = Geocoder.search(address_string) lat,lng = results.first.coordinates self.for_lat_lng(lat: lat, lng: lng, params: params) else raise Error, 'Could not find address' end end |
.for_lat_lng(lat:, lng:, params: {}) ⇒ Object
query StormGlass given lat/lng. params:
:lat - dateTime (default Now)
:lng - Datetime (default 12 hours from :start)
:params - additional params available to self.reqest
53 54 55 |
# File 'lib/stormglass.rb', line 53 def self.for_lat_lng(lat:, lng:, params: {}) self.request(params: {lat: lat, lng: lng}.merge(params)) end |
.hours_offset(start_time, hours = 12) ⇒ Object
94 95 96 |
# File 'lib/stormglass.rb', line 94 def self.hours_offset(start_time, hours=12) (start_time + (Rational(1,24) * hours)) end |
.query_time_string(datetime) ⇒ Object
98 99 100 |
# File 'lib/stormglass.rb', line 98 def self.query_time_string(datetime) datetime.new_offset(0).iso8601 end |
.request(endpoint: 'point', params: {}) ⇒ Object
Primary interface to StormGlass. params:
:start - dateTime (default Now)
:end - Datetime (default 12 hours from :start)
:hours - number of hours to determine end (default 12)
:key - API key (default to api_key method )
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/stormglass.rb', line 63 def self.request(endpoint: 'point', params: {}) hours = (params.delete(:hours) || 11) - 1 params[:key] ||= api_key params[:start] ||= DateTime.now params[:end] ||= hours_offset(params[:start], hours) params[:start] = query_time_string(params[:start]) params[:end] = query_time_string(params[:end]) key = params.delete(:key) begin body = RestClient.get("https://api.stormglass.io/#{endpoint}", {params: params, 'Authorization' => key}).body rescue SocketError => msg puts msg raise ConnectionError, 'error connecting to stormglass' rescue RestClient::PaymentRequired => msg puts msg raise ExceededLimitError, 'exceeded limit. payment required for additional daily requests' end Stormglass::Response.new(body) end |
.set_settings ⇒ Object
33 34 35 |
# File 'lib/stormglass.rb', line 33 def set_settings self.settings = Configuration.new end |