ale_air 0.0.8 → 0.2.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: 236de19ba776e22ce1ba00b8ec550acaaee3e6af
4
- data.tar.gz: 0459e6112e4a416cc0a7371b09dd9047888469c2
2
+ SHA256:
3
+ metadata.gz: a97580cf9de23d6c07b045f34682529d49f781d6bee608318371839c6fa43117
4
+ data.tar.gz: 965e2d3065322e72252b444369ada0c587dc8e5d110b9d5eec26101a746ac1e8
5
5
  SHA512:
6
- metadata.gz: a6131fce0250abc79903196bb33e837d03333eb130c60ce8aec8c191e57d1f6a2ae6b9f0d1c210fbd1b30f67385c54e179af4c5fb2541240f4bd9194460deb61
7
- data.tar.gz: 7c650bae6f188da1e1714e2835dab8ad6b0773a7448fae5e3e153f9ac8bd622a019b2d9999ab4f5983167444bfb806fde6d47123fa4126c6c0737b53202723a9
6
+ metadata.gz: 733d1a91d0c92584975394c0643fa60667da849fdf8851bc62af1faa2da4e776404d7fb0825dee3f659477ed70b46774bf0091cbc913c22b327b93f5afe8b525
7
+ data.tar.gz: e1aa5c14cafc9a1960ab404e7da28dd35f5ea981bd70b62fc0e3d77ebea5c033f7a67092133a569d9d51240044ed4936a6a7fc8a58f67d80ffe0cd7d46c48afd
data/CHANGELOG.md ADDED
@@ -0,0 +1,29 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
4
+
5
+ ## [0.2.0] - 2026-04-22
6
+
7
+ ### Added
8
+ - `lib/ale_air.rb` top-level entry point — `require 'ale_air'` now works as advertised.
9
+ - Error class hierarchy: `AleAir::Error`, `MissingTokenError`, `InvalidTokenError`, `ApiError`, `NetworkError`.
10
+ - Configurable `base_url`, `open_timeout`, `read_timeout` on `FetchJSON.new`.
11
+ - GitHub Actions CI matrix across Ruby 3.2, 3.3, 3.4, 4.0.
12
+ - `.rubocop.yml` with `rubocop-performance` and `rubocop-rspec`.
13
+ - `webmock` dev dependency — the test suite no longer hits the real WAQI API.
14
+
15
+ ### Changed
16
+ - HTTP client swapped from `rest-client` (unmaintained) to Ruby stdlib `net/http` — no runtime dependencies.
17
+ - `#air_quality` now consistently returns `true`/`false` and always sets `status`/`message` (previously returned `nil` when the token was missing).
18
+ - Gemspec: added `metadata` (source_code_uri, changelog_uri, rubygems_mfa_required), removed deprecated `test_files`, dropped the unnecessary `bundler` dev dep.
19
+
20
+ ### Removed
21
+ - `rest-client` runtime dependency.
22
+ - `attr_writer :secret_token` (token is now set only via the constructor).
23
+ - `config/key.yml` — no longer needed; tests use WebMock fixtures.
24
+
25
+ ### Fixed
26
+ - `require 'cgi'` is now explicit (previously loaded transitively via rest-client).
27
+ - Typo in `.rspec`: `--order define` → `--order defined`.
28
+
29
+ ## [0.1.0] - Initial release
data/README.md CHANGED
@@ -1,54 +1,109 @@
1
- # Easy Air Quality Ruby Gem for Major Cities
1
+ # AleAir Air Quality for Major Cities
2
2
 
3
- This is an easy to use Gem for your Ruby projects when you wish to add air quality parameters. It uses World Air Quality Projects open api and parses the responses for ease of use. To use it go to [Air Quality Project](aqicn.org) and apply for a token (you will need it).
3
+ [![CI](https://github.com/FistOfTheNorthStar/ale_air/actions/workflows/ci.yml/badge.svg)](https://github.com/FistOfTheNorthStar/ale_air/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/ale_air.svg)](https://rubygems.org/gems/ale_air)
5
+
6
+ A small Ruby gem that wraps the [World Air Quality Index](https://aqicn.org) (WAQI) public API and returns parsed, ready-to-use air-quality data for a given city. No external dependencies — just Ruby standard library.
4
7
 
5
8
  ## Installation
6
9
 
7
- Add this to Gemfile:
10
+ Add to your `Gemfile`:
8
11
 
9
12
  ```ruby
10
13
  gem 'ale_air'
11
14
  ```
12
15
 
13
- And then execute:
16
+ Then:
17
+
18
+ ```sh
19
+ bundle install
20
+ ```
21
+
22
+ Or install directly:
23
+
24
+ ```sh
25
+ gem install ale_air
26
+ ```
14
27
 
15
- $ bundle install
16
-
17
- Or
28
+ ## Configuration
18
29
 
19
- $ gem install ale_air
30
+ You need a free WAQI API token. Request one at <https://aqicn.org/data-platform/token/>.
20
31
 
21
32
  ## Usage
22
- Add
23
33
 
24
- require 'ale_air'
34
+ ```ruby
35
+ require 'ale_air'
25
36
 
26
- Just initialize first with your token
37
+ client = AleAir::FetchJSON.new('YOUR_TOKEN')
27
38
 
28
- air_results = AleAir::FetchJSON.new('YOUR_TOKEN')
39
+ if client.air_quality('Helsinki')
40
+ puts client.descriptive_text
41
+ # => "Air quality: 42 AQI Good @ Kallio 12:00 +02:00"
42
+ else
43
+ warn "Lookup failed: #{client.message}"
44
+ end
45
+ ```
29
46
 
30
- Now you can get the air quality where you wish. This will return true if succesful and false if some error occured
47
+ ### Accessors
31
48
 
32
- air_results.air_quality('Helsinki')
49
+ After a call to `#air_quality`, the client exposes:
33
50
 
34
- Then you can just use the results
51
+ | Method | Description |
52
+ |----------------------|----------------------------------------------------------------------|
53
+ | `status` | `"ok"` or `"error"` |
54
+ | `message` | Human-readable status message or error detail |
55
+ | `location` | Name of the measurement station |
56
+ | `quality` | Air Quality Index (US-EPA 2016 scale) |
57
+ | `time_measured` | Measurement timestamp with timezone |
58
+ | `danger_level` | One of: Good, Moderate, Unhealthy for Sensitive Groups, Unhealthy, Very Unhealthy, Hazardous |
59
+ | `descriptive_text` | Pre-formatted single-line summary |
35
60
 
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
43
- ## Development
61
+ ### Options
44
62
 
45
- Install on locally and start developing.
63
+ ```ruby
64
+ AleAir::FetchJSON.new(
65
+ 'YOUR_TOKEN',
66
+ base_url: 'https://api.waqi.info', # override for testing
67
+ open_timeout: 5, # seconds
68
+ read_timeout: 10 # seconds
69
+ )
70
+ ```
46
71
 
47
- ## License
72
+ ### Error handling
48
73
 
49
- MIT License.
74
+ `#air_quality` always returns `true` or `false`. On failure, `status` is set to `"error"` and `message` contains the reason:
75
+
76
+ ```ruby
77
+ client = AleAir::FetchJSON.new('bad-token')
78
+ client.air_quality('Helsinki') # => false
79
+ client.status # => "error"
80
+ client.message # => "Invalid key"
81
+ ```
82
+
83
+ Error classes are also defined under `AleAir::` for callers that prefer to raise:
84
+ `AleAir::Error`, `AleAir::MissingTokenError`, `AleAir::InvalidTokenError`, `AleAir::ApiError`, `AleAir::NetworkError`.
85
+
86
+ ## Development
87
+
88
+ ```sh
89
+ bundle install
90
+ bundle exec rspec # run the test suite
91
+ bundle exec rubocop # run the linter
92
+ bundle exec rake # run both (default task)
93
+ ```
94
+
95
+ Tests use [WebMock](https://github.com/bblimke/webmock) to stub the WAQI API — no real network access is required.
50
96
 
51
97
  ## Contributing
52
98
 
53
- Report bugs and pull requests on GitHub at https://github.com/FistOfTheNorthStar/ale_air
99
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/FistOfTheNorthStar/ale_air>.
100
+
101
+ 1. Fork the repo
102
+ 2. Create a feature branch
103
+ 3. Add tests for your change
104
+ 4. Ensure `bundle exec rake` is green
105
+ 5. Open a pull request
106
+
107
+ ## License
54
108
 
109
+ Released under the [MIT License](LICENSE.txt).
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AleAir
4
+ class Error < StandardError; end
5
+
6
+ class MissingTokenError < Error; end
7
+ class InvalidTokenError < Error; end
8
+ class ApiError < Error; end
9
+ class NetworkError < Error; end
10
+ end
@@ -1,88 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi'
1
4
  require 'json'
2
- require 'rest-client'
5
+ require 'net/http'
6
+ require 'uri'
3
7
 
4
8
  module AleAir
9
+ # Fetches air-quality data from the WAQI (World Air Quality Index) API.
5
10
  class FetchJSON
6
- attr_writer :secret_token
7
- attr_reader :status, :message, :time_measured, :location, :quality, :danger_level, :irc_string
11
+ DEFAULT_BASE_URL = 'https://api.waqi.info'
12
+ DEFAULT_OPEN_TIMEOUT = 5
13
+ DEFAULT_READ_TIMEOUT = 10
14
+
15
+ attr_reader :status, :message, :time_measured, :location, :quality,
16
+ :danger_level, :descriptive_text
8
17
 
9
- def initialize(token = '')
10
- @secret_token = token
18
+ def initialize(secret_token = nil, base_url: DEFAULT_BASE_URL,
19
+ open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT)
20
+ @secret_token = secret_token.to_s
21
+ @base_url = base_url
22
+ @open_timeout = open_timeout
23
+ @read_timeout = read_timeout
11
24
  end
12
25
 
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
26
+ def air_quality(city = '')
27
+ return error('Missing API token') if @secret_token.empty?
28
+
29
+ document = get_json("/search/?keyword=#{cleaned(city)}&token=#{@secret_token}")
30
+ get_info(document)
31
+ rescue NetworkError => e
32
+ error("Network error: #{e.message}")
33
+ rescue JSON::ParserError => e
34
+ error("Invalid JSON response: #{e.message}")
18
35
  end
19
36
 
20
37
  private
21
- def cleaned(chars = "")
22
- URI.encode(chars.downcase.strip)
23
- end
24
38
 
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
39
+ def get_json(path)
40
+ uri = URI.join(@base_url, path)
41
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https',
42
+ open_timeout: @open_timeout,
43
+ read_timeout: @read_timeout) do |http|
44
+ http.get(uri.request_uri)
59
45
  end
46
+ JSON.parse(response.body.to_s)
47
+ rescue SocketError, Timeout::Error, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => e
48
+ raise NetworkError, e.message
49
+ end
50
+
51
+ def cleaned(chars = '')
52
+ CGI.escape(chars.to_s.downcase.strip)
53
+ end
54
+
55
+ def get_info(document)
56
+ return no_data_available_message(document) unless document.is_a?(Hash)
57
+ return no_data_available_message(document) unless document['status'] == 'ok'
58
+ return no_data_available_message(document) unless document['data'].is_a?(Array) && document['data'].any?
59
+
60
+ air = document['data'].first
61
+ aqi = air['aqi']
62
+ return error('Invalid Air Quality Value') unless integer?(aqi)
63
+
64
+ information_message(aqi, air)
65
+ end
66
+
67
+ def information_message(aqi, air)
68
+ station_name = air.dig('station', 'name')
69
+ @status = 'ok'
70
+ @message = 'Air Quality'
71
+ @quality = aqi
72
+ @time_measured = "#{air.dig('time', 'stime')} #{air.dig('time', 'tz')}"
73
+ @location = station_name
74
+ @danger_level = danger_lev(aqi)
75
+ @descriptive_text = "Air quality: #{aqi} AQI #{@danger_level} @ #{station_name} #{@time_measured}"
76
+ true
77
+ end
78
+
79
+ def no_data_available_message(document)
80
+ detail = document.is_a?(Hash) ? document['data'] : nil
81
+ error(detail.is_a?(String) ? detail : 'No Data Available')
82
+ end
83
+
84
+ def error(message)
85
+ @status = 'error'
86
+ @message = message
60
87
  false
61
88
  end
62
89
 
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"
90
+ def danger_lev(aqi)
91
+ case aqi.to_i
92
+ when 0..50 then 'Good'
93
+ when 51..100 then 'Moderate'
94
+ when 101..150 then 'Unhealthy for Sensitive Groups'
95
+ when 151..200 then 'Unhealthy'
96
+ when 201..300 then 'Very Unhealthy'
97
+ else 'Hazardous'
77
98
  end
78
99
  end
79
100
 
80
- def is_int(string)
81
- Integer(string).is_a?(Integer)
101
+ def integer?(aqi)
102
+ Integer(aqi)
103
+ true
82
104
  rescue ArgumentError, TypeError
83
105
  false
84
106
  end
85
-
86
107
  end
87
108
  end
88
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AleAir
2
- VERSION = "0.0.8"
4
+ VERSION = '0.2.0'
3
5
  end
data/lib/ale_air.rb CHANGED
@@ -1,5 +1,9 @@
1
- require "ale_air/version"
2
- require 'ale_air/fetch_json'
1
+ # frozen_string_literal: true
3
2
 
3
+ require_relative 'ale_air/version'
4
+ require_relative 'ale_air/errors'
5
+ require_relative 'ale_air/fetch_json'
6
+
7
+ # Air quality of major cities
4
8
  module AleAir
5
9
  end
metadata CHANGED
@@ -1,23 +1,23 @@
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.2.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: 2026-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -39,7 +39,21 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-performance
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -53,7 +67,21 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: bundler
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
57
85
  requirement: !ruby/object:Gem::Requirement
58
86
  requirements:
59
87
  - - ">="
@@ -74,23 +102,22 @@ executables: []
74
102
  extensions: []
75
103
  extra_rdoc_files: []
76
104
  files:
77
- - ".gitignore"
78
- - ".rspec"
79
- - Gemfile
105
+ - CHANGELOG.md
80
106
  - LICENSE.txt
81
107
  - README.md
82
- - Rakefile
83
- - ale_air.gemspec
84
- - config/key.sample.yml
85
108
  - lib/ale_air.rb
109
+ - lib/ale_air/errors.rb
86
110
  - lib/ale_air/fetch_json.rb
87
111
  - lib/ale_air/version.rb
88
- - spec/fetch_json_spec.rb
89
- - spec/spec_helper.rb
90
112
  homepage: https://github.com/FistOfTheNorthStar/ale_air
91
113
  licenses:
92
114
  - MIT
93
- metadata: {}
115
+ metadata:
116
+ homepage_uri: https://github.com/FistOfTheNorthStar/ale_air
117
+ source_code_uri: https://github.com/FistOfTheNorthStar/ale_air
118
+ bug_tracker_uri: https://github.com/FistOfTheNorthStar/ale_air/issues
119
+ changelog_uri: https://github.com/FistOfTheNorthStar/ale_air/blob/master/CHANGELOG.md
120
+ rubygems_mfa_required: 'true'
94
121
  post_install_message:
95
122
  rdoc_options: []
96
123
  require_paths:
@@ -99,18 +126,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
126
  requirements:
100
127
  - - ">="
101
128
  - !ruby/object:Gem::Version
102
- version: '0'
129
+ version: '3.2'
103
130
  required_rubygems_version: !ruby/object:Gem::Requirement
104
131
  requirements:
105
132
  - - ">="
106
133
  - !ruby/object:Gem::Version
107
134
  version: '0'
108
135
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.2.2
136
+ rubygems_version: 3.0.3.1
111
137
  signing_key:
112
138
  specification_version: 4
113
139
  summary: Air Quality of Major Cities
114
- test_files:
115
- - spec/fetch_json_spec.rb
116
- - spec/spec_helper.rb
140
+ test_files: []
data/.gitignore DELETED
@@ -1,24 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
- *.bundle
19
- *.so
20
- *.o
21
- *.a
22
- *.swp
23
- mkmf.log
24
- config/key.yml
data/.rspec DELETED
@@ -1,7 +0,0 @@
1
- --color
2
- --require spec_helper
3
- --format documentation
4
- --profile
5
- --no-fail-fast
6
- --order define
7
-
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in testGem.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
-
data/ale_air.gemspec DELETED
@@ -1,26 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'ale_air/version'
5
-
6
- Gem::Specification.new do |s|
7
- s.name = 'ale_air'
8
- s.version = AleAir::VERSION
9
- s.summary = "Air Quality of Major Cities"
10
- s.description = "Easy to use air quality of major cities. Everything has been parsed for you and ready to use."
11
- s.authors = ["FistOfTheNorthStar"]
12
- s.email = ["k.kulvik@gmail.com"]
13
- s.homepage =
14
- 'https://github.com/FistOfTheNorthStar/ale_air'
15
- s.license = 'MIT'
16
-
17
- s.files = `git ls-files -z`.split("\x0")
18
- s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- s.test_files = s.files.grep(%r{^(test|spec|features)/})
20
- s.require_paths = ["lib"]
21
-
22
- s.add_dependency "rest-client"
23
- s.add_development_dependency "rspec"
24
- s.add_development_dependency "rake"
25
- s.add_development_dependency "bundler"
26
- end
@@ -1 +0,0 @@
1
- api_key: 'XXX'
@@ -1,99 +0,0 @@
1
- require 'spec_helper'
2
- require 'json'
3
- require 'rest-client'
4
- require 'yaml'
5
-
6
- describe 'FetchJSON' do
7
-
8
- 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"]
14
- danger_array_append = []
15
- levels.each do |level|
16
- danger_array_append << fetch_json.send(:danger_lev, level)
17
- end
18
- expect(danger_array).to match_array(danger_array_append)
19
- end
20
- end
21
-
22
- describe '#is_int' do
23
- 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
27
- end
28
-
29
- it "returns int from a string" do
30
- expect(fetch_json.send(:is_int, "100")).to be true
31
- end
32
- end
33
-
34
- describe "#get_info" do
35
-
36
- 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
50
- expect(correct_array).to match_array(back_array)
51
- end
52
-
53
- 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
-
58
- end
59
-
60
- 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
64
- end
65
-
66
- end
67
-
68
- describe "#air_quality" do
69
-
70
- it 'returns false with nil key with correct city' do
71
- fetch_json = AleAir::FetchJSON.new
72
- expect(fetch_json.air_quality('Helsinki')).to be false
73
- sleep(5)
74
- end
75
-
76
- it 'returns false with incorrect key with correct city' do
77
- fetch_json = AleAir::FetchJSON.new('ABCDSLJ')
78
- expect(fetch_json.air_quality('Helsinki')).to be false
79
- sleep(5)
80
- end
81
-
82
- it 'returns false with nonexisting name correct key' do
83
- fetch_json = AleAir::FetchJSON.new(api_key['api_key'])
84
- expect(fetch_json.air_quality('lkjlkjlkjlkjlkj')).to be false
85
- sleep(5)
86
- end
87
-
88
- it 'returns true with correct place name correct key' do
89
- fetch_json = AleAir::FetchJSON.new(api_key['api_key'])
90
- expect(fetch_json.air_quality('Helsinki')).to be true
91
- end
92
- end
93
-
94
- protected
95
- def api_key
96
- @config = YAML.load_file("./config/key.yml")
97
- end
98
-
99
- end
data/spec/spec_helper.rb DELETED
@@ -1,101 +0,0 @@
1
- Dir['./lib/**/*.rb'].each { |file| require file }
2
- # This file was generated by the `rspec --init` command. Conventionally, all
3
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
- # The generated `.rspec` file contains `--require spec_helper` which will cause
5
- # this file to always be loaded, without a need to explicitly require it in any
6
- # files.
7
- #
8
- # Given that it is always loaded, you are encouraged to keep this file as
9
- # light-weight as possible. Requiring heavyweight dependencies from this file
10
- # will add to the boot time of your test suite on EVERY test run, even for an
11
- # individual file that may not need all of that loaded. Instead, consider making
12
- # a separate helper file that requires the additional dependencies and performs
13
- # the additional setup, and require it from the spec files that actually need
14
- # it.
15
- #
16
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
- RSpec.configure do |config|
18
- # rspec-expectations config goes here. You can use an alternate
19
- # assertion/expectation library such as wrong or the stdlib/minitest
20
- # assertions if you prefer.
21
- config.expect_with :rspec do |expectations|
22
- # This option will default to `true` in RSpec 4. It makes the `description`
23
- # and `failure_message` of custom matchers include text for helper methods
24
- # defined using `chain`, e.g.:
25
- # be_bigger_than(2).and_smaller_than(4).description
26
- # # => "be bigger than 2 and smaller than 4"
27
- # ...rather than:
28
- # # => "be bigger than 2"
29
- expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
- end
31
-
32
- # rspec-mocks config goes here. You can use an alternate test double
33
- # library (such as bogus or mocha) by changing the `mock_with` option here.
34
- config.mock_with :rspec do |mocks|
35
- # Prevents you from mocking or stubbing a method that does not exist on
36
- # a real object. This is generally recommended, and will default to
37
- # `true` in RSpec 4.
38
- mocks.verify_partial_doubles = true
39
- end
40
-
41
- # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
42
- # have no way to turn it off -- the option exists only for backwards
43
- # compatibility in RSpec 3). It causes shared context metadata to be
44
- # inherited by the metadata hash of host groups and examples, rather than
45
- # triggering implicit auto-inclusion in groups with matching metadata.
46
- config.shared_context_metadata_behavior = :apply_to_host_groups
47
-
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
101
- end