ale_air 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +14 -0
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/README.md +7 -7
- data/ale_air.gemspec +1 -0
- data/lib/ale_air/fetch_json.rb +70 -63
- data/lib/ale_air/version.rb +3 -1
- data/lib/ale_air.rb +4 -1
- data/spec/fetch_json_spec.rb +34 -42
- data/spec/spec_helper.rb +53 -53
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f60580661ac1dae4b860d496130df098215ec3514a14aba785ccb12b651da6ea
|
4
|
+
data.tar.gz: cc5816589710c9d61e224099776f42b0485797d0f2774ad67c694385becc6eff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05c6364bdb707eff52e160a633f713e020140d7d81eb59fb2d3bd65ce6e1f9f0924d046f0c0b51958d47af3b88448065c685e4f25e3e47c8e927099749d82f62
|
7
|
+
data.tar.gz: ece5caf157b032b0b4ac6585cbd28921cade0e72d39eb5e70b2197483009875e40a1117b4011a44331e5b393699b98343802f80603a57f54c45790d47eedf888
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
3
|
+
# Please see the documentation for all configuration options:
|
4
|
+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
5
|
+
|
6
|
+
version: 2
|
7
|
+
updates:
|
8
|
+
- package-ecosystem: "bundler" # See documentation for possible values
|
9
|
+
directory: "/" # Location of package manifests
|
10
|
+
registries: "*"
|
11
|
+
schedule:
|
12
|
+
interval: "weekly"
|
13
|
+
day: "sunday"
|
14
|
+
time: "20:00"
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -33,13 +33,13 @@ Now you can get the air quality where you wish. This will return true if succesf
|
|
33
33
|
|
34
34
|
Then you can just use the results
|
35
35
|
|
36
|
-
air_results.status
|
37
|
-
air_results.message
|
38
|
-
air_results.location
|
39
|
-
air_results.quality
|
40
|
-
air_results.time_measured
|
41
|
-
air_results.danger_level
|
42
|
-
air_results.
|
36
|
+
air_results.status -- will return "ok" or "error"
|
37
|
+
air_results.message -- what kind of error occured
|
38
|
+
air_results.location -- the measurement station location
|
39
|
+
air_results.quality -- Air Quality Index scale as defined by the US-EPA 2016
|
40
|
+
air_results.time_measured -- time of measurement
|
41
|
+
air_results.danger_level -- level
|
42
|
+
air_results.descriptive_text -- Ready string with all the info for IRC for example
|
43
43
|
## Development
|
44
44
|
|
45
45
|
Install on locally and start developing.
|
data/ale_air.gemspec
CHANGED
@@ -6,6 +6,7 @@ require 'ale_air/version'
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'ale_air'
|
8
8
|
s.version = AleAir::VERSION
|
9
|
+
s.required_ruby_version = '>= 3.2.2'
|
9
10
|
s.summary = "Air Quality of Major Cities"
|
10
11
|
s.description = "Easy to use air quality of major cities. Everything has been parsed for you and ready to use."
|
11
12
|
s.authors = ["FistOfTheNorthStar"]
|
data/lib/ale_air/fetch_json.rb
CHANGED
@@ -1,88 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
2
4
|
require 'rest-client'
|
3
5
|
|
4
6
|
module AleAir
|
7
|
+
# Fetches JSON from Waqi API
|
5
8
|
class FetchJSON
|
6
9
|
attr_writer :secret_token
|
7
|
-
attr_reader :status, :message, :time_measured, :location, :quality, :danger_level, :
|
10
|
+
attr_reader :status, :message, :time_measured, :location, :quality, :danger_level, :descriptive_text
|
8
11
|
|
9
|
-
def initialize(
|
10
|
-
@secret_token =
|
12
|
+
def initialize(secret_token = '')
|
13
|
+
@secret_token = secret_token
|
11
14
|
end
|
12
15
|
|
13
|
-
def air_quality(city =
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def air_quality(city = '')
|
17
|
+
return if @secret_token.nil?
|
18
|
+
|
19
|
+
response = RestClient.get("https://api.waqi.info/search/?keyword=#{cleaned(city)}&token=#{@secret_token}")
|
20
|
+
document = JSON.parse(response.body)
|
21
|
+
|
22
|
+
get_info(document)
|
23
|
+
rescue RestClient::ExceptionWithResponse => e
|
24
|
+
@message = e.response&.body || 'Unknown Error'
|
25
|
+
false
|
18
26
|
end
|
19
27
|
|
20
28
|
private
|
21
|
-
|
22
|
-
|
29
|
+
|
30
|
+
def cleaned(chars = '')
|
31
|
+
CGI.escape(chars.downcase.strip)
|
23
32
|
end
|
24
33
|
|
25
|
-
def get_info(document
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
@status = "ok"
|
37
|
-
@message = "Air Quality"
|
38
|
-
@quality = air["aqi"]
|
39
|
-
@time_measured = air["time"]["stime"] + ' ' + air["time"]["tz"]
|
40
|
-
@location = air["station"]["name"]
|
41
|
-
@danger_level = danger_lev(air["aqi"].to_i)
|
42
|
-
@irc_string = "Air quality: " + air["aqi"] + " AQI " + danger_lev(air["aqi"].to_i) + " @ " + air["station"]["name"] + " " + @time_measured
|
43
|
-
return true
|
44
|
-
else
|
45
|
-
@message = "No Data Available"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
else
|
49
|
-
@message = "No Stations Found"
|
50
|
-
end
|
51
|
-
else
|
52
|
-
if !document["data"].nil?
|
53
|
-
@message = document["data"]
|
54
|
-
else
|
55
|
-
@message = "Unknown Error"
|
56
|
-
@message = document
|
57
|
-
end
|
58
|
-
end
|
34
|
+
def get_info(document)
|
35
|
+
if document['status'] == 'ok' && document['data'].any?
|
36
|
+
air = document['data'].first
|
37
|
+
aqi = air['aqi']
|
38
|
+
|
39
|
+
return false unless check_aqi(aqi)
|
40
|
+
|
41
|
+
information_message(aqi, air)
|
42
|
+
else
|
43
|
+
no_data_available_message(document)
|
59
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def check_aqi(aqi)
|
48
|
+
return true if integer?(aqi)
|
49
|
+
|
50
|
+
@status = 'error'
|
51
|
+
@message = 'Invalid Air Quality Value'
|
60
52
|
false
|
61
53
|
end
|
62
54
|
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
when 201..300
|
74
|
-
"Very Unhealthy"
|
75
|
-
else
|
76
|
-
"Hazardous"
|
77
|
-
end
|
55
|
+
def information_message(aqi, air)
|
56
|
+
@status = 'ok'
|
57
|
+
@message = 'Air Quality'
|
58
|
+
@quality = aqi
|
59
|
+
@time_measured = "#{air.dig('time', 'stime')} #{air.dig('time', 'tz')}"
|
60
|
+
@location = air.dig('station', 'name')
|
61
|
+
@danger_level = danger_lev(aqi)
|
62
|
+
@descriptive_text = "Air quality: #{aqi} AQI #{danger_lev(aqi)} @ #{air.dig('station',
|
63
|
+
'name')} #{@time_measured}"
|
64
|
+
true
|
78
65
|
end
|
79
66
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
67
|
+
def no_data_available_message(document)
|
68
|
+
@status = 'error'
|
69
|
+
@message = case document['data']
|
70
|
+
when String then document['data']
|
71
|
+
else 'No Data Available'
|
72
|
+
end
|
83
73
|
false
|
84
74
|
end
|
85
75
|
|
76
|
+
def danger_lev(aqi)
|
77
|
+
case aqi.to_i
|
78
|
+
when 0..50 then 'Good'
|
79
|
+
when 51..100 then 'Moderate'
|
80
|
+
when 101..150 then 'Unhealthy for Sensitive Groups'
|
81
|
+
when 151..200 then 'Unhealthy'
|
82
|
+
when 201..300 then 'Very Unhealthy'
|
83
|
+
else 'Hazardous'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def integer?(aqi)
|
88
|
+
Integer(aqi)
|
89
|
+
rescue ArgumentError
|
90
|
+
false
|
91
|
+
else
|
92
|
+
true
|
93
|
+
end
|
86
94
|
end
|
87
95
|
end
|
88
|
-
|
data/lib/ale_air/version.rb
CHANGED
data/lib/ale_air.rb
CHANGED
data/spec/fetch_json_spec.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rest-client'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
4
3
|
require 'yaml'
|
5
4
|
|
6
5
|
describe 'FetchJSON' do
|
7
|
-
|
8
6
|
describe '#danger_lev' do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
levels = [0,51,101,151,201,301]
|
13
|
-
danger_array = [
|
7
|
+
let(:fetch_json) { AleAir::FetchJSON.new }
|
8
|
+
|
9
|
+
it 'returns danger level of air according to aqi' do
|
10
|
+
levels = [0, 51, 101, 151, 201, 301]
|
11
|
+
danger_array = ['Good', 'Moderate', 'Unhealthy for Sensitive Groups', 'Unhealthy', 'Very Unhealthy', 'Hazardous']
|
14
12
|
danger_array_append = []
|
15
13
|
levels.each do |level|
|
16
14
|
danger_array_append << fetch_json.send(:danger_lev, level)
|
@@ -19,56 +17,50 @@ describe 'FetchJSON' do
|
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
|
-
describe '#
|
20
|
+
describe '#integer?' do
|
23
21
|
fetch_json = AleAir::FetchJSON.new
|
24
|
-
it
|
25
|
-
temp_val =
|
26
|
-
expect(fetch_json.send(:
|
22
|
+
it 'returns false on a string without value to convert' do
|
23
|
+
temp_val = 'this is just a string'
|
24
|
+
expect(fetch_json.send(:integer?, temp_val)).to be false
|
27
25
|
end
|
28
26
|
|
29
|
-
it
|
30
|
-
expect(fetch_json.send(:
|
27
|
+
it 'returns int from a string' do
|
28
|
+
expect(fetch_json.send(:integer?, '100')).to be true
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
|
-
describe
|
32
|
+
describe '#get_info' do
|
33
|
+
before(:example) do
|
34
|
+
@fetch_json = AleAir::FetchJSON.new
|
35
|
+
end
|
35
36
|
|
36
37
|
it 'send correctly formatted hash with status ok' do
|
37
|
-
hash_correct = {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
back_array
|
44
|
-
|
45
|
-
|
46
|
-
back_array << fetch_json.danger_level
|
47
|
-
back_array << fetch_json.time_measured
|
48
|
-
back_array << fetch_json.irc_string
|
49
|
-
expect(got_back).to be true
|
38
|
+
hash_correct = { 'status' => 'ok', 'message' => 'all good test',
|
39
|
+
'data' => [{ 'aqi' => '100', 'time' => { 'stime' => '19:00', 'tz' => '+2' },
|
40
|
+
'station' => { 'name' => 'test place' } }] }
|
41
|
+
correct_array = ['100', '19:00 +2', 'Air Quality', 'Air quality: 100 AQI Moderate @ test place 19:00 +2',
|
42
|
+
'Moderate', 'ok', 'test place']
|
43
|
+
response = @fetch_json.send(:get_info, hash_correct)
|
44
|
+
back_array = [@fetch_json.status, @fetch_json.message, @fetch_json.quality, @fetch_json.location,
|
45
|
+
@fetch_json.danger_level, @fetch_json.time_measured, @fetch_json.descriptive_text]
|
46
|
+
expect(response).to be true
|
50
47
|
expect(correct_array).to match_array(back_array)
|
51
48
|
end
|
52
49
|
|
53
50
|
it 'send nil hash should receive false' do
|
54
|
-
hash_correct =
|
55
|
-
fetch_json
|
56
|
-
expect(fetch_json.send(:get_info, hash_correct)).to be false
|
57
|
-
|
51
|
+
hash_correct = ''
|
52
|
+
expect(@fetch_json.send(:get_info, hash_correct)).to be false
|
58
53
|
end
|
59
54
|
|
60
55
|
it 'send error receive false' do
|
61
|
-
hash_correct = {
|
62
|
-
fetch_json
|
63
|
-
expect(fetch_json.send(:get_info, hash_correct)).to be false
|
56
|
+
hash_correct = { 'status' => 'error' }
|
57
|
+
expect(@fetch_json.send(:get_info, hash_correct)).to be false
|
64
58
|
end
|
65
|
-
|
66
59
|
end
|
67
60
|
|
68
|
-
describe
|
69
|
-
|
61
|
+
describe '#air_quality' do
|
70
62
|
it 'returns false with nil key with correct city' do
|
71
|
-
fetch_json = AleAir::FetchJSON.new
|
63
|
+
fetch_json = AleAir::FetchJSON.new
|
72
64
|
expect(fetch_json.air_quality('Helsinki')).to be false
|
73
65
|
sleep(5)
|
74
66
|
end
|
@@ -92,8 +84,8 @@ describe 'FetchJSON' do
|
|
92
84
|
end
|
93
85
|
|
94
86
|
protected
|
87
|
+
|
95
88
|
def api_key
|
96
|
-
@config = YAML.load_file(
|
89
|
+
@config = YAML.load_file('./config/key.yml')
|
97
90
|
end
|
98
|
-
|
99
91
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Dir['./lib/**/*.rb'].each { |file| require file }
|
2
4
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
3
5
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
@@ -45,57 +47,55 @@ RSpec.configure do |config|
|
|
45
47
|
# triggering implicit auto-inclusion in groups with matching metadata.
|
46
48
|
config.shared_context_metadata_behavior = :apply_to_host_groups
|
47
49
|
|
48
|
-
# The settings below are suggested to provide a good initial experience
|
49
|
-
# with RSpec, but feel free to customize to your heart's content.
|
50
|
-
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
#
|
65
|
-
# - http://
|
66
|
-
# - http://
|
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
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
Kernel.srand config.seed
|
100
|
-
=end
|
50
|
+
# The settings below are suggested to provide a good initial experience
|
51
|
+
# with RSpec, but feel free to customize to your heart's content.
|
52
|
+
# # This allows you to limit a spec run to individual examples or groups
|
53
|
+
# # you care about by tagging them with `:focus` metadata. When nothing
|
54
|
+
# # is tagged with `:focus`, all examples get run. RSpec also provides
|
55
|
+
# # aliases for `it`, `describe`, and `context` that include `:focus`
|
56
|
+
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
57
|
+
# config.filter_run_when_matching :focus
|
58
|
+
#
|
59
|
+
# # Allows RSpec to persist some state between runs in order to support
|
60
|
+
# # the `--only-failures` and `--next-failure` CLI options. We recommend
|
61
|
+
# # you configure your source control system to ignore this file.
|
62
|
+
# config.example_status_persistence_file_path = "spec/examples.txt"
|
63
|
+
#
|
64
|
+
# # Limits the available syntax to the non-monkey patched syntax that is
|
65
|
+
# # recommended. For more details, see:
|
66
|
+
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
67
|
+
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
68
|
+
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
69
|
+
# config.disable_monkey_patching!
|
70
|
+
#
|
71
|
+
# # This setting enables warnings. It's recommended, but in some cases may
|
72
|
+
# # be too noisy due to issues in dependencies.
|
73
|
+
# config.warnings = true
|
74
|
+
#
|
75
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
76
|
+
# # file, and it's useful to allow more verbose output when running an
|
77
|
+
# # individual spec file.
|
78
|
+
# if config.files_to_run.one?
|
79
|
+
# # Use the documentation formatter for detailed output,
|
80
|
+
# # unless a formatter has already been configured
|
81
|
+
# # (e.g. via a command-line flag).
|
82
|
+
# config.default_formatter = "doc"
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# # Print the 10 slowest examples and example groups at the
|
86
|
+
# # end of the spec run, to help surface which specs are running
|
87
|
+
# # particularly slow.
|
88
|
+
# config.profile_examples = 10
|
89
|
+
#
|
90
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
91
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
92
|
+
# # the seed, which is printed after each run.
|
93
|
+
# # --seed 1234
|
94
|
+
# config.order = :random
|
95
|
+
#
|
96
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
97
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
98
|
+
# # test failures related to randomization by passing the same `--seed` value
|
99
|
+
# # as the one that triggered the failure.
|
100
|
+
# Kernel.srand config.seed
|
101
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ale_air
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- FistOfTheNorthStar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -74,6 +74,7 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
+
- ".github/dependabot.yml"
|
77
78
|
- ".gitignore"
|
78
79
|
- ".rspec"
|
79
80
|
- Gemfile
|
@@ -99,15 +100,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
100
|
requirements:
|
100
101
|
- - ">="
|
101
102
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
103
|
+
version: 3.2.2
|
103
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
105
|
requirements:
|
105
106
|
- - ">="
|
106
107
|
- !ruby/object:Gem::Version
|
107
108
|
version: '0'
|
108
109
|
requirements: []
|
109
|
-
|
110
|
-
rubygems_version: 2.2.2
|
110
|
+
rubygems_version: 3.4.10
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: Air Quality of Major Cities
|