ale_air 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2a35e5053e3d933d5b7eb69d1584745685ea3fdc
4
- data.tar.gz: bbfbe540eb33fd389185e31fea87e46764363a8d
2
+ SHA256:
3
+ metadata.gz: f60580661ac1dae4b860d496130df098215ec3514a14aba785ccb12b651da6ea
4
+ data.tar.gz: cc5816589710c9d61e224099776f42b0485797d0f2774ad67c694385becc6eff
5
5
  SHA512:
6
- metadata.gz: 06fe6626332fd4ae0d0fb3ec1b2f3a43e723e51221378a68cbd4a48449f351d0342bdc40e539cb4f94fb1efb97cb2222d198c32ae1088202cbd3ae5d2a27e28a
7
- data.tar.gz: d2813ec13feee278cb1f668111fb3bfe9e75d412e3a873e6f35cb9e266132ae6bcc234bcca5e2b772f5970ddfc682ec3860aab3ea31c4ac3cbafa74c38c854c2
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
@@ -21,3 +21,5 @@ tmp
21
21
  *.a
22
22
  *.swp
23
23
  mkmf.log
24
+ config/key.yml
25
+ .idea/
data/.rspec CHANGED
@@ -1,6 +1,6 @@
1
1
  --color
2
2
  --require spec_helper
3
- --format progress
3
+ --format documentation
4
4
  --profile
5
5
  --no-fail-fast
6
6
  --order define
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in testGem.gemspec
4
4
  gemspec
5
+
6
+ gem 'rubocop'
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 -- 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.irc_string -- Ready string with all the info for IRC for example
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"]
@@ -19,7 +20,7 @@ Gem::Specification.new do |s|
19
20
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
21
  s.require_paths = ["lib"]
21
22
 
22
- s.add_dependency 'rest-client'
23
+ s.add_dependency "rest-client"
23
24
  s.add_development_dependency "rspec"
24
25
  s.add_development_dependency "rake"
25
26
  s.add_development_dependency "bundler"
@@ -0,0 +1 @@
1
+ api_key: 'XXX'
@@ -1,89 +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, :irc_string
10
+ attr_reader :status, :message, :time_measured, :location, :quality, :danger_level, :descriptive_text
8
11
 
9
- def initialize(token = nil)
10
- @secret_token = token
12
+ def initialize(secret_token = '')
13
+ @secret_token = secret_token
11
14
  end
12
15
 
13
- def air_quality(city = nil)
14
- unless @secret_token.nil?
15
- document = JSON.parse(RestClient.get 'https://api.waqi.info/search/?keyword=' + cleaned(city) + '&token=' + @secret_token)
16
- get_info(document)
17
- end
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
- def cleaned(chars = "")
22
- URI.encode(chars.downcase.strip)
29
+
30
+ def cleaned(chars = '')
31
+ CGI.escape(chars.downcase.strip)
23
32
  end
24
33
 
25
- def get_info(document = nil)
26
- @status = "error"
27
- unless document.nil?
28
- if document["status"] == "ok"
29
- if document["data"].length > 0
30
- document["data"].each do |air|
31
- if !air["aqi"].nil? || !air["aqi"].empty?
32
- unless is_int(air["aqi"])
33
- @message = "Invalid Airquality Value"
34
- return false
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
-
53
- if !document["data"].nil?
54
- @message = document["data"]
55
- else
56
- @message = "Unknown Error"
57
- @message = document
58
- end
59
- 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)
60
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'
61
52
  false
62
53
  end
63
54
 
64
- def danger_lev(aqi=0)
65
- case aqi
66
- when 0..50
67
- "Good"
68
- when 51..100
69
- "Moderate"
70
- when 101..150
71
- "Unhealthy for Sensitive Groups"
72
- when 151..200
73
- "Unhealthy"
74
- when 201..300
75
- "Very Unhealthy"
76
- else
77
- "Hazardous"
78
- 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
79
65
  end
80
66
 
81
- def is_int(string)
82
- Integer(string).is_a?(Integer)
83
- rescue ArgumentError, TypeError
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
84
73
  false
85
74
  end
86
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
87
94
  end
88
95
  end
89
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AleAir
2
- VERSION = "0.0.6"
4
+ VERSION = '0.1.0'
3
5
  end
data/lib/ale_air.rb CHANGED
@@ -1,5 +1,8 @@
1
- require "ale_air/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'ale_air/version'
2
4
  require 'ale_air/fetch_json'
3
5
 
6
+ # Air quality of major cities
4
7
  module AleAir
5
8
  end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ describe 'FetchJSON' do
6
+ describe '#danger_lev' do
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']
12
+ danger_array_append = []
13
+ levels.each do |level|
14
+ danger_array_append << fetch_json.send(:danger_lev, level)
15
+ end
16
+ expect(danger_array).to match_array(danger_array_append)
17
+ end
18
+ end
19
+
20
+ describe '#integer?' do
21
+ fetch_json = AleAir::FetchJSON.new
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
25
+ end
26
+
27
+ it 'returns int from a string' do
28
+ expect(fetch_json.send(:integer?, '100')).to be true
29
+ end
30
+ end
31
+
32
+ describe '#get_info' do
33
+ before(:example) do
34
+ @fetch_json = AleAir::FetchJSON.new
35
+ end
36
+
37
+ it 'send correctly formatted hash with status ok' do
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
47
+ expect(correct_array).to match_array(back_array)
48
+ end
49
+
50
+ it 'send nil hash should receive false' do
51
+ hash_correct = ''
52
+ expect(@fetch_json.send(:get_info, hash_correct)).to be false
53
+ end
54
+
55
+ it 'send error receive false' do
56
+ hash_correct = { 'status' => 'error' }
57
+ expect(@fetch_json.send(:get_info, hash_correct)).to be false
58
+ end
59
+ end
60
+
61
+ describe '#air_quality' do
62
+ it 'returns false with nil key with correct city' do
63
+ fetch_json = AleAir::FetchJSON.new
64
+ expect(fetch_json.air_quality('Helsinki')).to be false
65
+ sleep(5)
66
+ end
67
+
68
+ it 'returns false with incorrect key with correct city' do
69
+ fetch_json = AleAir::FetchJSON.new('ABCDSLJ')
70
+ expect(fetch_json.air_quality('Helsinki')).to be false
71
+ sleep(5)
72
+ end
73
+
74
+ it 'returns false with nonexisting name correct key' do
75
+ fetch_json = AleAir::FetchJSON.new(api_key['api_key'])
76
+ expect(fetch_json.air_quality('lkjlkjlkjlkjlkj')).to be false
77
+ sleep(5)
78
+ end
79
+
80
+ it 'returns true with correct place name correct key' do
81
+ fetch_json = AleAir::FetchJSON.new(api_key['api_key'])
82
+ expect(fetch_json.air_quality('Helsinki')).to be true
83
+ end
84
+ end
85
+
86
+ protected
87
+
88
+ def api_key
89
+ @config = YAML.load_file('./config/key.yml')
90
+ end
91
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir['./lib/**/*.rb'].each { |file| require file }
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
7
+ # this file to always be loaded, without a need to explicitly require it in any
8
+ # files.
9
+ #
10
+ # Given that it is always loaded, you are encouraged to keep this file as
11
+ # light-weight as possible. Requiring heavyweight dependencies from this file
12
+ # will add to the boot time of your test suite on EVERY test run, even for an
13
+ # individual file that may not need all of that loaded. Instead, consider making
14
+ # a separate helper file that requires the additional dependencies and performs
15
+ # the additional setup, and require it from the spec files that actually need
16
+ # it.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
44
+ # have no way to turn it off -- the option exists only for backwards
45
+ # compatibility in RSpec 3). It causes shared context metadata to be
46
+ # inherited by the metadata hash of host groups and examples, rather than
47
+ # triggering implicit auto-inclusion in groups with matching metadata.
48
+ config.shared_context_metadata_behavior = :apply_to_host_groups
49
+
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
+ 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.6
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: 2017-12-13 00:00:00.000000000 Z
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
@@ -81,9 +82,12 @@ files:
81
82
  - README.md
82
83
  - Rakefile
83
84
  - ale_air.gemspec
85
+ - config/key.sample.yml
84
86
  - lib/ale_air.rb
85
87
  - lib/ale_air/fetch_json.rb
86
88
  - lib/ale_air/version.rb
89
+ - spec/fetch_json_spec.rb
90
+ - spec/spec_helper.rb
87
91
  homepage: https://github.com/FistOfTheNorthStar/ale_air
88
92
  licenses:
89
93
  - MIT
@@ -96,16 +100,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
100
  requirements:
97
101
  - - ">="
98
102
  - !ruby/object:Gem::Version
99
- version: '0'
103
+ version: 3.2.2
100
104
  required_rubygems_version: !ruby/object:Gem::Requirement
101
105
  requirements:
102
106
  - - ">="
103
107
  - !ruby/object:Gem::Version
104
108
  version: '0'
105
109
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.2.2
110
+ rubygems_version: 3.4.10
108
111
  signing_key:
109
112
  specification_version: 4
110
113
  summary: Air Quality of Major Cities
111
- test_files: []
114
+ test_files:
115
+ - spec/fetch_json_spec.rb
116
+ - spec/spec_helper.rb