Class: SouthAfricanAirQualityIndex::Client
- Inherits:
-
Object
- Object
- SouthAfricanAirQualityIndex::Client
- Includes:
- Constants
- Defined in:
- lib/south_african_air_quality_index/client.rb
Constant Summary
Constants included from Constants
SouthAfricanAirQualityIndex::Constants::AQI_INDEX, SouthAfricanAirQualityIndex::Constants::BASE_PATH, SouthAfricanAirQualityIndex::Constants::DEFAULT_INTERVAL, SouthAfricanAirQualityIndex::Constants::HOURLY_REPORT, SouthAfricanAirQualityIndex::Constants::REPORT_TYPE, SouthAfricanAirQualityIndex::Constants::STATION_REPORT
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#stations ⇒ Object
readonly
Memoize stations as they are unlikely to change often.
Class Method Summary collapse
-
.api_version ⇒ Object
This is the version of the API docs this client was built off-of.
- .compatible_api_version ⇒ Object
Instance Method Summary collapse
-
#initialize(base_path: BASE_PATH, port: 80) ⇒ Client
constructor
A new instance of Client.
- #multi_station_report(station_names, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) ⇒ Object
- #selected_stations(station_names) ⇒ Object
- #station_report(station_name, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) ⇒ Object
- #stations_from_code(codes, build_for_response: false) ⇒ Object
Methods included from Constants
Constructor Details
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
5 6 7 |
# File 'lib/south_african_air_quality_index/client.rb', line 5 def base_path @base_path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/south_african_air_quality_index/client.rb', line 5 def port @port end |
#stations ⇒ Object (readonly)
Memoize stations as they are unlikely to change often
24 25 26 |
# File 'lib/south_african_air_quality_index/client.rb', line 24 def stations @stations end |
Class Method Details
.api_version ⇒ Object
This is the version of the API docs this client was built off-of
17 18 19 |
# File 'lib/south_african_air_quality_index/client.rb', line 17 def self.api_version 'v1 2022-12-15' end |
.compatible_api_version ⇒ Object
12 13 14 |
# File 'lib/south_african_air_quality_index/client.rb', line 12 def self.compatible_api_version 'v1' end |
Instance Method Details
#multi_station_report(station_names, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/south_african_air_quality_index/client.rb', line 63 def multi_station_report(station_names, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) extracted_stations = selected_stations(station_names) monitor_channels_by_station_id = extracted_stations.map { |station| [station['serialCode'].to_s, fetch_monitor_ids(station)] }.to_h body = { "monitorChannelsByStationId": monitor_channels_by_station_id, "reportName": HOURLY_REPORT, "startDateAbsolute": start_date.to_s, "endDateAbsolute": end_date.to_s, "startDate": "/Date(#{Time.parse(start_date.to_s).to_i})/", "endDate": "/Date(#{Time.parse(end_date.to_s).to_i})/", "reportType": REPORT_TYPE, "fromTb": DEFAULT_INTERVAL, "toTb": DEFAULT_INTERVAL } monitor_channels_by_station_id.each.with_index do |pair, index| hsh = { "monitorChannelsByStationId[#{index}].Key": pair[0], "monitorChannelsByStationId[#{index}].Value": pair[1] } body = body.merge(hsh) end send(http_method: :get, path: 'report/GetMultiStationReportData', body: body.to_json) end |
#selected_stations(station_names) ⇒ Object
35 36 37 38 39 |
# File 'lib/south_african_air_quality_index/client.rb', line 35 def selected_stations(station_names) stations['body'].select do |station| station_matches?(station_names, station) end end |
#station_report(station_name, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/south_african_air_quality_index/client.rb', line 41 def station_report(station_name, start_date, end_date, interval: DEFAULT_INTERVAL, report_type: REPORT_TYPE) station = selected_stations([station_name]).first return if station.empty? monitor_ids = fetch_monitor_ids(station) body = { "StationId": station['serialCode'], "MonitorsChannels": monitor_ids, "reportName": STATION_REPORT, "startDateAbsolute": start_date.to_s, "endDateAbsolute": end_date.to_s, "startDate": start_date.to_s, "endDate": end_date.to_s, "reportType": report_type, "fromTb": 60, "toTb": 60 }.to_json send(http_method: :get, path: 'report/GetStationReportData', body: body, params: { 'InPopUp': false }) end |
#stations_from_code(codes, build_for_response: false) ⇒ Object
29 30 31 32 33 |
# File 'lib/south_african_air_quality_index/client.rb', line 29 def stations_from_code(codes, build_for_response: false) stations['body']['Stations'].select do |station| codes.map(&:to_s).include?(station["serialCode"].to_s) end end |