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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 236de19ba776e22ce1ba00b8ec550acaaee3e6af
4
- data.tar.gz: 0459e6112e4a416cc0a7371b09dd9047888469c2
2
+ SHA256:
3
+ metadata.gz: f60580661ac1dae4b860d496130df098215ec3514a14aba785ccb12b651da6ea
4
+ data.tar.gz: cc5816589710c9d61e224099776f42b0485797d0f2774ad67c694385becc6eff
5
5
  SHA512:
6
- metadata.gz: a6131fce0250abc79903196bb33e837d03333eb130c60ce8aec8c191e57d1f6a2ae6b9f0d1c210fbd1b30f67385c54e179af4c5fb2541240f4bd9194460deb61
7
- data.tar.gz: 7c650bae6f188da1e1714e2835dab8ad6b0773a7448fae5e3e153f9ac8bd622a019b2d9999ab4f5983167444bfb806fde6d47123fa4126c6c0737b53202723a9
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
@@ -22,3 +22,4 @@ tmp
22
22
  *.swp
23
23
  mkmf.log
24
24
  config/key.yml
25
+ .idea/
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"]
@@ -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, :irc_string
10
+ attr_reader :status, :message, :time_measured, :location, :quality, :danger_level, :descriptive_text
8
11
 
9
- def initialize(token = '')
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
- 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 danger_lev(aqi=0)
64
- case aqi
65
- when 0..50
66
- "Good"
67
- when 51..100
68
- "Moderate"
69
- when 101..150
70
- "Unhealthy for Sensitive Groups"
71
- when 151..200
72
- "Unhealthy"
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 is_int(string)
81
- Integer(string).is_a?(Integer)
82
- 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
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
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AleAir
2
- VERSION = "0.0.8"
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
@@ -1,16 +1,14 @@
1
- require 'spec_helper'
2
- require 'json'
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
- it "returns danger level of air string according to aqi" do
11
- fetch_json = AleAir::FetchJSON.new
12
- levels = [0,51,101,151,201,301]
13
- danger_array = ["Good", "Moderate", "Unhealthy for Sensitive Groups", "Unhealthy", "Very Unhealthy", "Hazardous"]
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 '#is_int' do
20
+ describe '#integer?' do
23
21
  fetch_json = AleAir::FetchJSON.new
24
- it "returns false on a string without value to convert" do
25
- temp_val = "this is just a string"
26
- expect(fetch_json.send(:is_int, temp_val)).to be false
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 "returns int from a string" do
30
- expect(fetch_json.send(:is_int, "100")).to be true
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 "#get_info" do
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 = {"status" => "ok", "message" => "all good test", "data" => [{"aqi" => "100", "time" => {"stime" => "19:00", "tz" => "+2"}, "station" => {"name" => "test place"}}]}
38
- correct_array = ["ok", "Air Quality", "Moderate", "100", "19:00 +2", "test place", "Air quality: 100 AQI Moderate @ test place 19:00 +2"]
39
- fetch_json = AleAir::FetchJSON.new
40
- got_back = fetch_json.send(:get_info, hash_correct)
41
- back_array = []
42
- back_array << fetch_json.status
43
- back_array << fetch_json.message
44
- back_array << fetch_json.quality
45
- back_array << fetch_json.location
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 = nil
55
- fetch_json = AleAir::FetchJSON.new
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 = {"status" => "error"}
62
- fetch_json = AleAir::FetchJSON.new
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 "#air_quality" do
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("./config/key.yml")
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
- =begin
51
- # This allows you to limit a spec run to individual examples or groups
52
- # you care about by tagging them with `:focus` metadata. When nothing
53
- # is tagged with `:focus`, all examples get run. RSpec also provides
54
- # aliases for `it`, `describe`, and `context` that include `:focus`
55
- # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
56
- config.filter_run_when_matching :focus
57
-
58
- # Allows RSpec to persist some state between runs in order to support
59
- # the `--only-failures` and `--next-failure` CLI options. We recommend
60
- # you configure your source control system to ignore this file.
61
- config.example_status_persistence_file_path = "spec/examples.txt"
62
-
63
- # Limits the available syntax to the non-monkey patched syntax that is
64
- # recommended. For more details, see:
65
- # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
66
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
67
- # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
68
- config.disable_monkey_patching!
69
-
70
- # This setting enables warnings. It's recommended, but in some cases may
71
- # be too noisy due to issues in dependencies.
72
- config.warnings = true
73
-
74
- # Many RSpec users commonly either run the entire suite or an individual
75
- # file, and it's useful to allow more verbose output when running an
76
- # individual spec file.
77
- if config.files_to_run.one?
78
- # Use the documentation formatter for detailed output,
79
- # unless a formatter has already been configured
80
- # (e.g. via a command-line flag).
81
- config.default_formatter = "doc"
82
- end
83
-
84
- # Print the 10 slowest examples and example groups at the
85
- # end of the spec run, to help surface which specs are running
86
- # particularly slow.
87
- config.profile_examples = 10
88
-
89
- # Run specs in random order to surface order dependencies. If you find an
90
- # order dependency and want to debug it, you can fix the order by providing
91
- # the seed, which is printed after each run.
92
- # --seed 1234
93
- config.order = :random
94
-
95
- # Seed global randomization in this process using the `--seed` CLI option.
96
- # Setting this allows you to use `--seed` to deterministically reproduce
97
- # test failures related to randomization by passing the same `--seed` value
98
- # as the one that triggered the failure.
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.8
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-17 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
@@ -99,15 +100,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
100
  requirements:
100
101
  - - ">="
101
102
  - !ruby/object:Gem::Version
102
- version: '0'
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
- rubyforge_project:
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