barometer 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.pelusa.yml +7 -0
- data/.travis.yml +6 -3
- data/Gemfile +11 -1
- data/README.md +89 -244
- data/Rakefile +1 -1
- data/barometer.gemspec +22 -24
- data/lib/barometer.rb +14 -65
- data/lib/barometer/base.rb +35 -65
- data/lib/barometer/data.rb +15 -20
- data/lib/barometer/data/attribute.rb +19 -0
- data/lib/barometer/data/attributes/distance.rb +11 -0
- data/lib/barometer/data/attributes/float.rb +11 -0
- data/lib/barometer/data/attributes/integer.rb +11 -0
- data/lib/barometer/data/attributes/location.rb +11 -0
- data/lib/barometer/data/attributes/pressure.rb +11 -0
- data/lib/barometer/data/attributes/sun.rb +15 -0
- data/lib/barometer/data/attributes/temperature.rb +11 -0
- data/lib/barometer/data/attributes/time.rb +11 -0
- data/lib/barometer/data/attributes/vector.rb +11 -0
- data/lib/barometer/data/attributes/zone.rb +11 -0
- data/lib/barometer/data/convertable_units.rb +145 -0
- data/lib/barometer/data/coordinates.rb +16 -0
- data/lib/barometer/data/distance.rb +14 -133
- data/lib/barometer/data/geo.rb +22 -59
- data/lib/barometer/data/location.rb +14 -20
- data/lib/barometer/data/pressure.rb +14 -135
- data/lib/barometer/data/sun.rb +24 -24
- data/lib/barometer/data/temperature.rb +14 -169
- data/lib/barometer/data/vector.rb +44 -0
- data/lib/barometer/data/zone.rb +109 -112
- data/lib/barometer/query.rb +23 -161
- data/lib/barometer/query/base.rb +88 -0
- data/lib/barometer/query/converter.rb +64 -0
- data/lib/barometer/query/converters/from_coordinates_to_noaa_station_id.rb +30 -0
- data/lib/barometer/query/converters/from_geocode_to_coordinates.rb +28 -0
- data/lib/barometer/query/converters/from_short_zipcode_to_zipcode.rb +28 -0
- data/lib/barometer/query/converters/from_weather_id_to_geocode.rb +30 -0
- data/lib/barometer/query/converters/from_woe_id_or_ipv4_to_geocode.rb +32 -0
- data/lib/barometer/query/converters/to_geocode.rb +30 -0
- data/lib/barometer/query/converters/to_weather_id.rb +30 -0
- data/lib/barometer/query/converters/to_woe_id.rb +30 -0
- data/lib/barometer/query/format.rb +59 -0
- data/lib/barometer/query/formats/base.rb +22 -0
- data/lib/barometer/query/formats/coordinates.rb +14 -0
- data/lib/barometer/query/formats/geocode.rb +15 -0
- data/lib/barometer/query/formats/icao.rb +35 -0
- data/lib/barometer/query/formats/ipv4_address.rb +19 -0
- data/lib/barometer/query/formats/noaa_station_id.rb +15 -0
- data/lib/barometer/query/formats/postalcode.rb +20 -0
- data/lib/barometer/query/formats/short_zipcode.rb +15 -0
- data/lib/barometer/{translations → query/formats/translations}/icao_country_codes.yml +0 -0
- data/lib/barometer/{translations → query/formats/translations}/weather_country_codes.yml +0 -0
- data/lib/barometer/query/formats/unknown.rb +14 -0
- data/lib/barometer/query/formats/weather_id.rb +33 -0
- data/lib/barometer/query/formats/woe_id.rb +28 -0
- data/lib/barometer/query/formats/zipcode.rb +15 -0
- data/lib/barometer/query/service.rb +13 -0
- data/lib/barometer/query/services/apis/geonames_timezone.rb +26 -0
- data/lib/barometer/query/services/apis/google_geocode.rb +35 -0
- data/lib/barometer/query/services/apis/noaa_station.rb +31 -0
- data/lib/barometer/query/services/apis/weather_id.rb +35 -0
- data/lib/barometer/query/services/apis/wunderground_timezone.rb +26 -0
- data/lib/barometer/query/services/apis/yahoo_placefinder.rb +35 -0
- data/lib/barometer/query/services/apis/yahoo_weather.rb +31 -0
- data/lib/barometer/query/services/from_weather_id.rb +64 -0
- data/lib/barometer/query/services/geonames_timezone.rb +18 -0
- data/lib/barometer/query/services/google_geocode.rb +106 -0
- data/lib/barometer/query/services/noaa_station_id.rb +28 -0
- data/lib/barometer/query/services/to_weather_id.rb +25 -0
- data/lib/barometer/query/services/to_woe_id.rb +29 -0
- data/lib/barometer/query/services/wunderground_timezone.rb +18 -0
- data/lib/barometer/query/services/yahoo_geocode.rb +69 -0
- data/lib/barometer/response.rb +12 -0
- data/lib/barometer/response/base.rb +57 -0
- data/lib/barometer/response/current.rb +27 -0
- data/lib/barometer/response/prediction.rb +41 -0
- data/lib/barometer/response/prediction_collection.rb +48 -0
- data/lib/barometer/utils.rb +17 -0
- data/lib/barometer/utils/address.rb +33 -0
- data/lib/barometer/utils/api.rb +30 -0
- data/lib/barometer/utils/config_reader.rb +40 -0
- data/lib/barometer/utils/get.rb +17 -0
- data/lib/barometer/utils/json_reader.rb +22 -0
- data/lib/barometer/utils/payload.rb +100 -0
- data/lib/barometer/utils/payload_request.rb +37 -0
- data/lib/barometer/utils/post.rb +24 -0
- data/lib/barometer/utils/time.rb +78 -0
- data/lib/barometer/{translations → utils/translations}/zone_codes.yml +0 -0
- data/lib/barometer/utils/versioned_registration.rb +70 -0
- data/lib/barometer/utils/xml_reader.rb +27 -0
- data/lib/barometer/utils/zone_code_lookup.rb +30 -0
- data/lib/barometer/version.rb +1 -1
- data/lib/barometer/weather.rb +44 -173
- data/lib/barometer/weather_service.rb +41 -0
- data/lib/barometer/weather_services/base.rb +50 -0
- data/lib/barometer/weather_services/forecast_io.rb +36 -0
- data/lib/barometer/weather_services/forecast_io/api.rb +22 -0
- data/lib/barometer/weather_services/forecast_io/query.rb +38 -0
- data/lib/barometer/weather_services/forecast_io/response.rb +31 -0
- data/lib/barometer/weather_services/forecast_io/response/current_weather.rb +78 -0
- data/lib/barometer/weather_services/forecast_io/response/forecasted_weather.rb +74 -0
- data/lib/barometer/weather_services/forecast_io/response/location.rb +19 -0
- data/lib/barometer/weather_services/forecast_io/response/timezone.rb +25 -0
- data/lib/barometer/weather_services/noaa.rb +21 -305
- data/lib/barometer/weather_services/noaa/current_api.rb +25 -0
- data/lib/barometer/weather_services/noaa/current_query.rb +30 -0
- data/lib/barometer/weather_services/noaa/current_response.rb +29 -0
- data/lib/barometer/weather_services/noaa/forecast_api.rb +25 -0
- data/lib/barometer/weather_services/noaa/forecast_query.rb +39 -0
- data/lib/barometer/weather_services/noaa/forecast_response.rb +28 -0
- data/lib/barometer/weather_services/noaa/response/current_location.rb +42 -0
- data/lib/barometer/weather_services/noaa/response/current_station.rb +46 -0
- data/lib/barometer/weather_services/noaa/response/current_weather.rb +82 -0
- data/lib/barometer/weather_services/noaa/response/forecasted_weather.rb +90 -0
- data/lib/barometer/weather_services/noaa/response/location.rb +19 -0
- data/lib/barometer/weather_services/noaa/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/response.rb +9 -0
- data/lib/barometer/weather_services/response/location.rb +42 -0
- data/lib/barometer/weather_services/response/time_zone.rb +19 -0
- data/lib/barometer/weather_services/weather_bug.rb +24 -280
- data/lib/barometer/weather_services/weather_bug/current_api.rb +26 -0
- data/lib/barometer/weather_services/weather_bug/current_response.rb +33 -0
- data/lib/barometer/weather_services/weather_bug/forecast_api.rb +26 -0
- data/lib/barometer/weather_services/weather_bug/forecast_response.rb +29 -0
- data/lib/barometer/weather_services/weather_bug/query.rb +42 -0
- data/lib/barometer/weather_services/weather_bug/response/current_weather.rb +82 -0
- data/lib/barometer/weather_services/weather_bug/response/forecasted_weather.rb +67 -0
- data/lib/barometer/weather_services/weather_bug/response/location.rb +23 -0
- data/lib/barometer/weather_services/weather_bug/response/station.rb +43 -0
- data/lib/barometer/weather_services/weather_bug/response/sun.rb +32 -0
- data/lib/barometer/weather_services/weather_bug/response/time_helper.rb +52 -0
- data/lib/barometer/weather_services/weather_bug/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/wunderground_v1.rb +32 -0
- data/lib/barometer/weather_services/wunderground_v1/current_api.rb +21 -0
- data/lib/barometer/weather_services/wunderground_v1/current_response.rb +31 -0
- data/lib/barometer/weather_services/wunderground_v1/forecast_api.rb +21 -0
- data/lib/barometer/weather_services/wunderground_v1/forecast_response.rb +33 -0
- data/lib/barometer/weather_services/wunderground_v1/query.rb +30 -0
- data/lib/barometer/weather_services/wunderground_v1/response/current_weather.rb +92 -0
- data/lib/barometer/weather_services/wunderground_v1/response/forecasted_weather.rb +87 -0
- data/lib/barometer/weather_services/wunderground_v1/response/full_timezone.rb +22 -0
- data/lib/barometer/weather_services/wunderground_v1/response/location.rb +43 -0
- data/lib/barometer/weather_services/wunderground_v1/response/station.rb +39 -0
- data/lib/barometer/weather_services/wunderground_v1/response/sun.rb +53 -0
- data/lib/barometer/weather_services/wunderground_v1/response/timezone.rb +15 -0
- data/lib/barometer/weather_services/yahoo.rb +16 -198
- data/lib/barometer/weather_services/yahoo/api.rb +21 -0
- data/lib/barometer/weather_services/yahoo/query.rb +42 -0
- data/lib/barometer/weather_services/yahoo/response.rb +39 -0
- data/lib/barometer/weather_services/yahoo/response/current_weather.rb +86 -0
- data/lib/barometer/weather_services/yahoo/response/forecasted_weather.rb +71 -0
- data/lib/barometer/weather_services/yahoo/response/location.rb +47 -0
- data/lib/barometer/weather_services/yahoo/response/sun.rb +43 -0
- data/lib/barometer/weather_services/yahoo/response/timezone.rb +15 -0
- data/spec/barometer_spec.rb +18 -120
- data/spec/base_spec.rb +114 -0
- data/spec/cassettes/Converter_FromCoordinatesToNoaaStationId.json +1 -0
- data/spec/cassettes/Converter_FromWeatherIdToGeocode.json +1 -0
- data/spec/cassettes/Converter_FromWoeIdOrIpv4ToGeocode.json +1 -0
- data/spec/cassettes/Converter_ToGeocode.json +1 -0
- data/spec/cassettes/Converter_ToWeatherId.json +1 -0
- data/spec/cassettes/Converter_ToWoeId.json +1 -0
- data/spec/cassettes/Service_FromWeatherId.json +1 -0
- data/spec/cassettes/Service_GoogleGeocode.json +1 -0
- data/spec/cassettes/Service_NoaaStation.json +1 -0
- data/spec/cassettes/Service_ToWeatherId.json +1 -0
- data/spec/cassettes/Service_ToWoeId.json +1 -0
- data/spec/cassettes/Service_YahooGeocode.json +1 -0
- data/spec/cassettes/WeatherService_ForecastIo.json +1 -0
- data/spec/cassettes/WeatherService_Noaa.json +1 -1
- data/spec/cassettes/WeatherService_WeatherBug.json +1 -1
- data/spec/cassettes/WeatherService_WundergroundV1.json +1 -0
- data/spec/cassettes/WeatherService_Yahoo.json +1 -1
- data/spec/data/attributes/distance_spec.rb +60 -0
- data/spec/data/attributes/location_spec.rb +41 -0
- data/spec/data/attributes/pressure_spec.rb +60 -0
- data/spec/data/attributes/sun_spec.rb +33 -0
- data/spec/data/attributes/temperature_spec.rb +60 -0
- data/spec/data/attributes/time_spec.rb +58 -0
- data/spec/data/attributes/vector_spec.rb +43 -0
- data/spec/data/attributes/zone_spec.rb +34 -0
- data/spec/data/convertable_units_spec.rb +299 -0
- data/spec/data/coordinates_spec.rb +15 -0
- data/spec/data/distance_spec.rb +49 -333
- data/spec/data/geo_spec.rb +72 -71
- data/spec/data/location_spec.rb +70 -65
- data/spec/data/pressure_spec.rb +49 -333
- data/spec/data/sun_spec.rb +57 -81
- data/spec/data/temperature_spec.rb +49 -393
- data/spec/data/vector_spec.rb +100 -0
- data/spec/data/zone_spec.rb +199 -266
- data/spec/query/base_spec.rb +296 -0
- data/spec/query/converter_spec.rb +98 -0
- data/spec/query/converters/from_coordinates_to_noaa_station_id_spec.rb +35 -0
- data/spec/query/converters/from_geocode_to_coordinates_spec.rb +25 -0
- data/spec/query/converters/from_short_zipcode_to_zipcode_spec.rb +31 -0
- data/spec/query/converters/from_weather_id_to_geocode_spec.rb +40 -0
- data/spec/query/converters/from_woe_id_or_ipv4_to_geocode_spec.rb +51 -0
- data/spec/query/converters/to_geocode_spec.rb +99 -0
- data/spec/query/converters/to_weather_id_spec.rb +35 -0
- data/spec/query/converters/to_woe_id_spec.rb +74 -0
- data/spec/query/formats/base_spec.rb +15 -0
- data/spec/query/formats/coordinates_spec.rb +13 -0
- data/spec/query/formats/geocode_spec.rb +9 -0
- data/spec/query/formats/icao_spec.rb +20 -0
- data/spec/query/formats/ipv4_address_spec.rb +33 -0
- data/spec/query/formats/noaa_station_id_spec.rb +9 -0
- data/spec/query/formats/postalcode_spec.rb +18 -0
- data/spec/query/formats/short_zipcode_spec.rb +18 -0
- data/spec/query/formats/unknown_spec.rb +9 -0
- data/spec/query/formats/weather_id_spec.rb +31 -0
- data/spec/query/formats/woe_id_spec.rb +45 -0
- data/spec/query/formats/zipcode_spec.rb +18 -0
- data/spec/query/formats_spec.rb +55 -0
- data/spec/query/services/from_weather_id_spec.rb +54 -0
- data/spec/query/services/google_geocode_spec.rb +60 -0
- data/spec/query/services/noaa_station_id_spec.rb +23 -0
- data/spec/query/services/to_weather_id_spec.rb +23 -0
- data/spec/query/services/to_woe_id_spec.rb +51 -0
- data/spec/query/services/yahoo_geocode_spec.rb +72 -0
- data/spec/response/base_spec.rb +103 -0
- data/spec/response/current_spec.rb +33 -0
- data/spec/response/predicition_spec.rb +65 -0
- data/spec/response/prediction_collection_spec.rb +97 -0
- data/spec/spec_helper.rb +14 -12
- data/spec/support/key_file_parser.rb +22 -0
- data/spec/support/matchers/formats.rb +36 -0
- data/spec/support/matchers/have_data.rb +46 -0
- data/spec/support/matchers/have_field.rb +128 -0
- data/spec/support/matchers/have_forecast.rb +46 -0
- data/spec/support/matchers/path.rb +23 -0
- data/spec/support/query_factory.rb +20 -0
- data/spec/utils/address_spec.rb +67 -0
- data/spec/utils/config_reader_spec.rb +157 -0
- data/spec/utils/get_spec.rb +22 -0
- data/spec/utils/payload_request_spec.rb +69 -0
- data/spec/utils/payload_spec.rb +168 -0
- data/spec/utils/post_spec.rb +20 -0
- data/spec/utils/time_spec.rb +93 -0
- data/spec/utils/versioned_registration_spec.rb +105 -0
- data/spec/weather_services/base_spec.rb +116 -0
- data/spec/weather_services/forecast_io_spec.rb +71 -0
- data/spec/weather_services/noaa/current_response_spec.rb +22 -0
- data/spec/weather_services/noaa/forecast_response_spec.rb +33 -0
- data/spec/weather_services/noaa_spec.rb +68 -166
- data/spec/weather_services/weather_bug/current_response_spec.rb +64 -0
- data/spec/weather_services/weather_bug/forecast_response_spec.rb +23 -0
- data/spec/weather_services/weather_bug_spec.rb +62 -202
- data/spec/weather_services/wunderground_v1/current_response_spec.rb +19 -0
- data/spec/weather_services/wunderground_v1/forecast_response_spec.rb +62 -0
- data/spec/weather_services/wunderground_v1_spec.rb +78 -0
- data/spec/weather_services/yahoo/response_spec.rb +73 -0
- data/spec/weather_services/yahoo_spec.rb +52 -135
- data/spec/weather_services_spec.rb +103 -0
- data/spec/weather_spec.rb +167 -347
- metadata +342 -123
- data/.document +0 -5
- data/TODO +0 -60
- data/bin/barometer +0 -441
- data/lib/barometer/data/local_datetime.rb +0 -145
- data/lib/barometer/data/local_time.rb +0 -134
- data/lib/barometer/data/speed.rb +0 -158
- data/lib/barometer/data/units.rb +0 -49
- data/lib/barometer/formats.rb +0 -13
- data/lib/barometer/formats/coordinates.rb +0 -57
- data/lib/barometer/formats/format.rb +0 -64
- data/lib/barometer/formats/geocode.rb +0 -60
- data/lib/barometer/formats/icao.rb +0 -37
- data/lib/barometer/formats/postalcode.rb +0 -22
- data/lib/barometer/formats/short_zipcode.rb +0 -17
- data/lib/barometer/formats/weather_id.rb +0 -92
- data/lib/barometer/formats/woe_id.rb +0 -150
- data/lib/barometer/formats/zipcode.rb +0 -31
- data/lib/barometer/key_file_parser.rb +0 -20
- data/lib/barometer/measurements/measurement.rb +0 -202
- data/lib/barometer/measurements/result.rb +0 -207
- data/lib/barometer/measurements/result_array.rb +0 -75
- data/lib/barometer/services.rb +0 -19
- data/lib/barometer/weather_services/service.rb +0 -189
- data/lib/barometer/weather_services/wunderground.rb +0 -264
- data/lib/barometer/web_services/geocode.rb +0 -34
- data/lib/barometer/web_services/noaa_station_id.rb +0 -53
- data/lib/barometer/web_services/placemaker.rb +0 -95
- data/lib/barometer/web_services/timezone.rb +0 -38
- data/lib/barometer/web_services/weather_id.rb +0 -50
- data/lib/barometer/web_services/web_service.rb +0 -29
- data/spec/cassettes/Barometer.json +0 -1
- data/spec/cassettes/Query.json +0 -1
- data/spec/cassettes/Query_Format_Coordinates.json +0 -1
- data/spec/cassettes/Query_Format_Geocode.json +0 -1
- data/spec/cassettes/Query_Format_WeatherID.json +0 -1
- data/spec/cassettes/Query_Format_WoeID.json +0 -1
- data/spec/cassettes/WeatherService.json +0 -1
- data/spec/cassettes/WeatherService_Wunderground.json +0 -1
- data/spec/cassettes/WebService_Geocode.json +0 -1
- data/spec/cassettes/WebService_NoaaStation.json +0 -1
- data/spec/data/local_datetime_spec.rb +0 -274
- data/spec/data/local_time_spec.rb +0 -239
- data/spec/data/speed_spec.rb +0 -374
- data/spec/data/units_spec.rb +0 -101
- data/spec/formats/coordinates_spec.rb +0 -166
- data/spec/formats/format_spec.rb +0 -74
- data/spec/formats/geocode_spec.rb +0 -163
- data/spec/formats/icao_spec.rb +0 -55
- data/spec/formats/postalcode_spec.rb +0 -53
- data/spec/formats/short_zipcode_spec.rb +0 -47
- data/spec/formats/weather_id_spec.rb +0 -182
- data/spec/formats/woe_id_spec.rb +0 -211
- data/spec/formats/zipcode_spec.rb +0 -103
- data/spec/key_file_parser_spec.rb +0 -28
- data/spec/measurements/measurement_spec.rb +0 -381
- data/spec/measurements/result_array_spec.rb +0 -150
- data/spec/measurements/result_spec.rb +0 -632
- data/spec/query_spec.rb +0 -498
- data/spec/weather_services/services_spec.rb +0 -135
- data/spec/weather_services/wunderground_spec.rb +0 -179
- data/spec/web_services/geocode_spec.rb +0 -31
- data/spec/web_services/noaa_station_id_spec.rb +0 -33
- data/spec/web_services/placemaker_spec.rb +0 -41
- data/spec/web_services/web_services_spec.rb +0 -20
@@ -0,0 +1,17 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require_relative 'utils/api'
|
7
|
+
require_relative 'utils/config_reader'
|
8
|
+
require_relative 'utils/time'
|
9
|
+
require_relative 'utils/payload'
|
10
|
+
require_relative 'utils/payload_request'
|
11
|
+
require_relative 'utils/versioned_registration'
|
12
|
+
require_relative 'utils/xml_reader'
|
13
|
+
require_relative 'utils/json_reader'
|
14
|
+
require_relative 'utils/address'
|
15
|
+
require_relative 'utils/get'
|
16
|
+
require_relative 'utils/post'
|
17
|
+
require_relative 'utils/zone_code_lookup'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'addressable/uri'
|
2
|
+
|
3
|
+
module Barometer
|
4
|
+
module Utils
|
5
|
+
class Address
|
6
|
+
def initialize(url, query={})
|
7
|
+
@address = Addressable::URI.parse(url)
|
8
|
+
add(query) unless query.blank?
|
9
|
+
end
|
10
|
+
|
11
|
+
def query
|
12
|
+
address.query_values
|
13
|
+
end
|
14
|
+
|
15
|
+
def url
|
16
|
+
address.omit(:query).to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def add(addition)
|
20
|
+
current_values = address.query_values || {}
|
21
|
+
address.query_values = current_values.merge(addition)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
address.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
attr_reader :address
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
class Api
|
4
|
+
attr_reader :query
|
5
|
+
|
6
|
+
def initialize(query)
|
7
|
+
@query = query
|
8
|
+
end
|
9
|
+
|
10
|
+
def current_query
|
11
|
+
@query.converted_query if @query.respond_to?(:converted_query)
|
12
|
+
end
|
13
|
+
|
14
|
+
def url
|
15
|
+
end
|
16
|
+
|
17
|
+
def params
|
18
|
+
@query.to_param
|
19
|
+
end
|
20
|
+
|
21
|
+
def unwrap_nodes
|
22
|
+
[]
|
23
|
+
end
|
24
|
+
|
25
|
+
def get
|
26
|
+
Utils::PayloadRequest.new(self).get
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
module ConfigReader
|
4
|
+
def self.take_level_while(&block)
|
5
|
+
Barometer.config.take_while do |level, level_config|
|
6
|
+
yield level
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.services(level, &block)
|
11
|
+
_dig(Barometer.config[level], {}, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
# iterate through the setup until we have a source name (and possibly
|
15
|
+
# a config for that source), then yield with that source and config
|
16
|
+
#
|
17
|
+
# this allows for many different config formats, like
|
18
|
+
# { 1 => :wunderground }
|
19
|
+
# { 1 => [:wunderground]}
|
20
|
+
# { 1 => [:wunderground, :yahoo]}
|
21
|
+
# { 1 => [:wunderground, {yahoo: {weight: 2}}]}
|
22
|
+
# { 1 => {wunderground: {weight: 2}}}
|
23
|
+
# { 1 => [{wunderground: {weight: 2}}]}
|
24
|
+
#
|
25
|
+
def self._dig(data, config, &block)
|
26
|
+
if data.respond_to?(:to_sym)
|
27
|
+
yield(data.to_sym, config)
|
28
|
+
elsif data.is_a?(Array)
|
29
|
+
data.each do |datum|
|
30
|
+
_dig(datum, {}, &block)
|
31
|
+
end
|
32
|
+
elsif data.is_a?(Hash)
|
33
|
+
data.each do |datum, config|
|
34
|
+
_dig(datum, config, &block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
module Barometer
|
4
|
+
module Utils
|
5
|
+
module Get
|
6
|
+
def self.call(url, query={})
|
7
|
+
address = Barometer::Utils::Address.new(url, query)
|
8
|
+
|
9
|
+
http = HTTPClient.new
|
10
|
+
http.receive_timeout = Barometer.timeout
|
11
|
+
http.get(address)
|
12
|
+
rescue HTTPClient::TimeoutError
|
13
|
+
raise Barometer::TimeoutError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'yajl'
|
2
|
+
|
3
|
+
module Barometer
|
4
|
+
module Utils
|
5
|
+
module JsonReader
|
6
|
+
def self.parse(json, *nodes_to_remove)
|
7
|
+
json_reader = Yajl::Parser.new
|
8
|
+
output = json_reader.parse(json)
|
9
|
+
|
10
|
+
nodes_to_remove.each do |node|
|
11
|
+
output = output.fetch(node, output)
|
12
|
+
end
|
13
|
+
|
14
|
+
if block_given? && output
|
15
|
+
output = yield(output)
|
16
|
+
end
|
17
|
+
|
18
|
+
output
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
class Payload
|
4
|
+
attr_reader :hash, :regex, :query
|
5
|
+
|
6
|
+
def initialize(hash, query=nil)
|
7
|
+
@hash = hash
|
8
|
+
@query = query
|
9
|
+
end
|
10
|
+
|
11
|
+
def using(regex)
|
12
|
+
@regex = regex
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch(*paths)
|
17
|
+
if hash
|
18
|
+
result = fetch_value_or_attribute(paths)
|
19
|
+
else
|
20
|
+
result = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
result = _apply_regex(result)
|
24
|
+
result = _cleanup(result)
|
25
|
+
result = _convert_alternate_nil_values(result)
|
26
|
+
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(*paths, &block)
|
31
|
+
path = fetch(*paths)
|
32
|
+
if path
|
33
|
+
path.each do |result|
|
34
|
+
result_payload = Barometer::Utils::Payload.new(result)
|
35
|
+
block.call(result_payload)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def each_with_index(*paths, &block)
|
41
|
+
fetch(*paths).each_with_index do |result, index|
|
42
|
+
result_payload = Barometer::Utils::Payload.new(result)
|
43
|
+
block.call(result_payload, index)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def fetch_each(*paths, &block)
|
48
|
+
each(*paths, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
def fetch_each_with_index(*paths, &block)
|
52
|
+
each_with_index(*paths, &block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def units
|
56
|
+
query.units if query
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def fetch_value_or_attribute(paths)
|
62
|
+
result = paths.inject(hash) do |result, path|
|
63
|
+
fetch_value(result, path) || fetch_attribute(result, path) || break
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def fetch_value(result, path)
|
68
|
+
if result.respond_to? :fetch
|
69
|
+
result.fetch(path, nil)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def fetch_attribute(result, path)
|
74
|
+
if path.to_s.start_with?('@') && result.respond_to?(:attributes)
|
75
|
+
result.attributes.fetch(path.slice(1..-1))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def _apply_regex(result)
|
80
|
+
if @regex && @regex.is_a?(Regexp) && matched = result.to_s.match(@regex)
|
81
|
+
result = matched[1] if matched[1]
|
82
|
+
end
|
83
|
+
@regex = nil
|
84
|
+
result
|
85
|
+
end
|
86
|
+
|
87
|
+
def _cleanup(result)
|
88
|
+
result.respond_to?(:strip) ? result.strip : result
|
89
|
+
end
|
90
|
+
|
91
|
+
def _convert_alternate_nil_values(result)
|
92
|
+
if result == "NA"
|
93
|
+
nil
|
94
|
+
else
|
95
|
+
result
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
class PayloadRequest
|
4
|
+
def initialize(api)
|
5
|
+
@api = api
|
6
|
+
end
|
7
|
+
|
8
|
+
def get
|
9
|
+
response = make_request
|
10
|
+
output = parse_response(response)
|
11
|
+
Payload.new(output, api.current_query)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def make_request
|
17
|
+
Get.call(api.url, api.params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse_response(response)
|
21
|
+
reader(response).parse(response.content, *api.unwrap_nodes)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :api
|
27
|
+
|
28
|
+
def reader(response)
|
29
|
+
if response.headers.fetch('Content-Type', '').match(/json/)
|
30
|
+
JsonReader
|
31
|
+
else
|
32
|
+
XmlReader
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'httpclient'
|
2
|
+
|
3
|
+
module Barometer
|
4
|
+
module Utils
|
5
|
+
module Post
|
6
|
+
def self.call(url, params={})
|
7
|
+
http = HTTPClient.new
|
8
|
+
http.receive_timeout = Barometer.timeout
|
9
|
+
http.post(url, default_params.merge(params)).content
|
10
|
+
rescue HTTPClient::TimeoutError
|
11
|
+
raise Barometer::TimeoutError
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.default_params
|
17
|
+
{
|
18
|
+
documentType: 'text/html',
|
19
|
+
outputType: 'xml'
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
module Time
|
4
|
+
def self.parse(*args)
|
5
|
+
return unless args.compact.size > 0
|
6
|
+
first_arg = args.first
|
7
|
+
|
8
|
+
if first_arg.is_a? ::Time
|
9
|
+
first_arg
|
10
|
+
elsif first_arg.is_a?(::DateTime) || first_arg.is_a?(::Date)
|
11
|
+
::Time.parse(first_arg.to_s)
|
12
|
+
elsif args.size == 1 || args.size == 2
|
13
|
+
strptime(*args)
|
14
|
+
else
|
15
|
+
::Time.utc(*args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.strftime(time)
|
20
|
+
time.strftime("%Y-%m-%d %H:%M:%S %z")
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.strptime(str, format=nil)
|
24
|
+
dt = if format
|
25
|
+
::DateTime.strptime(str, format)
|
26
|
+
else
|
27
|
+
::DateTime.parse(str)
|
28
|
+
end
|
29
|
+
::Time.utc(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec) - (dt.zone.to_f * 60 * 60)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.utc_from_base_plus_local_time(tz, base, hour, min)
|
33
|
+
return unless tz && base
|
34
|
+
local_base = tz.utc_to_local(base.utc)
|
35
|
+
|
36
|
+
local_time = ::Time.utc(local_base.year, local_base.month, local_base.day, hour, min, 0)
|
37
|
+
tz.local_to_utc(local_time)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.utc_merge_base_plus_time(base_time=nil, time=nil)
|
41
|
+
return unless base_time && time
|
42
|
+
base_time_utc = base_time.utc
|
43
|
+
time_utc = time.utc
|
44
|
+
|
45
|
+
::Time.utc(
|
46
|
+
base_time_utc.year, base_time_utc.month, base_time_utc.day,
|
47
|
+
time_utc.hour, time_utc.min, 0
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.add_one_day(time)
|
52
|
+
return unless time
|
53
|
+
one_day_minus_one_second = (60 * 60 * 24 - 1)
|
54
|
+
time + one_day_minus_one_second
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.add_one_hour(time)
|
58
|
+
return unless time
|
59
|
+
one_hour = (60 * 60 * 1)
|
60
|
+
time + one_hour
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.start_of_day(time)
|
64
|
+
::Time.utc(
|
65
|
+
time.year, time.month, time.day,
|
66
|
+
0, 0, 0
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.end_of_day(time)
|
71
|
+
::Time.utc(
|
72
|
+
time.year, time.month, time.day,
|
73
|
+
23, 59, 59
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
File without changes
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Barometer
|
2
|
+
module Utils
|
3
|
+
class VersionedRegistration
|
4
|
+
def initialize
|
5
|
+
@registrations = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def register(key, *args, &block)
|
9
|
+
if block_given?
|
10
|
+
version = args.shift
|
11
|
+
value = block
|
12
|
+
elsif args.size > 1
|
13
|
+
version = args.shift
|
14
|
+
value = args.shift
|
15
|
+
else
|
16
|
+
version = nil
|
17
|
+
value = args.shift
|
18
|
+
end
|
19
|
+
|
20
|
+
return if has_version?(key, version)
|
21
|
+
add_value(key, version, value)
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(key, version=nil)
|
25
|
+
return unless has_key?(key)
|
26
|
+
registration = find_version(key, version) || find_default(key) || {}
|
27
|
+
registration.fetch(:value, nil)
|
28
|
+
end
|
29
|
+
|
30
|
+
def size
|
31
|
+
@registrations.inject(0){|count,key_value| key_value[1].size + count }
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def has_key?(key)
|
37
|
+
@registrations.has_key? key
|
38
|
+
end
|
39
|
+
|
40
|
+
def has_version?(key, version)
|
41
|
+
registrations_for_key = @registrations[key] || []
|
42
|
+
registrations_for_key.detect{|r| r[:version] == (version || :default)}
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_value(key, version, value)
|
46
|
+
registration = {
|
47
|
+
version: (version || :default),
|
48
|
+
value: value
|
49
|
+
}
|
50
|
+
add_registration(key, registration)
|
51
|
+
end
|
52
|
+
|
53
|
+
def add_registration(key, registration)
|
54
|
+
registrations_for_key = @registrations[key] || []
|
55
|
+
registrations_for_key << registration
|
56
|
+
@registrations[key] = registrations_for_key
|
57
|
+
end
|
58
|
+
|
59
|
+
def find_version(key, version)
|
60
|
+
registrations = @registrations[key]
|
61
|
+
registrations.detect{|r| r[:version] == version}
|
62
|
+
end
|
63
|
+
|
64
|
+
def find_default(key)
|
65
|
+
registrations = @registrations[key]
|
66
|
+
registrations.detect{|r| r[:version] == :default}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|