china_aqi 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +60 -0
- data/.travis.yml +5 -1
- data/CHANGELOG.md +9 -0
- data/Gemfile +11 -3
- data/README.md +34 -4
- data/china_aqi.gemspec +18 -14
- data/lib/china_aqi.rb +3 -2
- data/lib/china_aqi/aqi/dynamic/city.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/city_pro.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/co.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/no2.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/o3.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/pm10.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/pm25.rb +1 -1
- data/lib/china_aqi/aqi/dynamic/so2.rb +1 -1
- data/lib/china_aqi/aqi/static/global.rb +1 -1
- data/lib/china_aqi/aqi/static/ranking.rb +1 -1
- data/lib/china_aqi/aqi/station.rb +1 -1
- data/lib/china_aqi/base.rb +7 -7
- data/lib/china_aqi/exceptions.rb +1 -1
- data/lib/china_aqi/helper/available_cities.rb +11 -0
- data/lib/china_aqi/helper/city_stations.rb +2 -3
- data/lib/china_aqi/utility.rb +5 -3
- data/lib/china_aqi/version.rb +1 -1
- data/lib/generators/china_aqi/install/install_generator.rb +3 -2
- data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/ChinaAqi_AvailableCities/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +139 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/have_module_function_called_available_cities_to_get_all_available_cities.yml +139 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/return_a_hash_with_aqi_data_when_call_get_instance_method.yml +139 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/ChinaAqi_CityStations/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +69 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/have_module_function_called_get_stations_for_city_to_get_stations_for_one_city.yml +69 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/return_a_hash_with_aqi_data_when_call_get_instance_method.yml +69 -0
- data/spec/cassettes/ChinaAqi/ChinaAqi_Station/ChinaAqi_Station/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +63 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_CO/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_City/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +95 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_CityPro/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_NO2/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_O3/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +106 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_PM10/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +101 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_PM25/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
- data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_SO2/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
- data/spec/cassettes/ChinaAqi/Static_models/ChinaAqi_Global/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
- data/spec/cassettes/ChinaAqi/Static_models/ChinaAqi_Ranking/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
- data/spec/china_aqi/aqi_spec.rb +89 -113
- data/spec/china_aqi/base_spec.rb +25 -32
- data/spec/china_aqi_spec.rb +6 -6
- data/spec/spec_helper.rb +7 -0
- metadata +54 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3feada665c53c16d850f1046b9b3e4fbe124720
|
4
|
+
data.tar.gz: 9d13fbabc8af55eb04b19550aab1f1d9ab26a507
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e40d15d2d7b14a24ad8e4c969c50dd5cc7fcba26cd406def4d29bb6afaf0fe006c9c34294e5af4f188b2be3047c7466590b496c72b01442ea9f4916ae9a3a66
|
7
|
+
data.tar.gz: d6a9ed650f69ccaabd803da5af8ce86a2573fad1de0df1454b808c7e4f4f00e6246a495fa1ad68eeac793e31ac6949d2dfaecb257d770a55960cb408de002268
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
DisplayCopNames: true
|
5
|
+
DisplayStyleGuide: true
|
6
|
+
TargetRubyVersion: 2.3
|
7
|
+
Exclude:
|
8
|
+
- "bin/*"
|
9
|
+
- "tmp/**/*"
|
10
|
+
- Gemfile
|
11
|
+
- Guardfile
|
12
|
+
- Rakefile
|
13
|
+
|
14
|
+
Metrics/AbcSize:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Metrics/ClassLength:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/LineLength:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 12
|
25
|
+
|
26
|
+
Style/BarePercentLiterals:
|
27
|
+
EnforcedStyle: percent_q
|
28
|
+
|
29
|
+
Style/ClassAndModuleChildren:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/Documentation:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/DoubleNegation:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/FrozenStringLiteralComment:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
42
|
+
EnforcedStyle: no_space
|
43
|
+
|
44
|
+
Style/StringLiterals:
|
45
|
+
EnforcedStyle: single_quotes
|
46
|
+
|
47
|
+
Style/TrivialAccessors:
|
48
|
+
AllowPredicates: true
|
49
|
+
|
50
|
+
Style/AsciiComments:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
RSpec/MultipleExpectations:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
RSpec/MessageChain:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
RSpec/DescribedClass:
|
60
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
1
|
+
# source 'https://rubygems.org'
|
2
|
+
source 'https://gems.ruby-china.org'
|
3
3
|
|
4
4
|
# Specify your gem's dependencies in china_aqi.gemspec
|
5
5
|
gemspec
|
6
6
|
|
7
|
-
|
7
|
+
if RUBY_VERSION < '2'
|
8
|
+
gem 'json', '1.8.3'
|
9
|
+
gem 'tins', '1.6.0'
|
10
|
+
end
|
11
|
+
gem 'rspec', '~> 3.5.0'
|
12
|
+
gem 'webmock', require: 'webmock/rspec'
|
13
|
+
gem 'vcr', '~> 3.0', '>= 3.0.3'
|
8
14
|
gem 'coveralls', require: false
|
15
|
+
gem 'rubocop', require: false
|
16
|
+
gem 'rubocop-rspec', '1.4.1'
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/china_aqi.png)](http://badge.fury.io/rb/china_aqi)
|
4
4
|
[![Build Status](https://travis-ci.org/Xuhao/china_aqi.png?branch=master)](https://travis-ci.org/Xuhao/china_aqi)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/Xuhao/china_aqi.png)](https://codeclimate.com/github/Xuhao/china_aqi)
|
6
|
+
[![Dependency Status](https://gemnasium.com/Xuhao/china_aqi.png)](https://gemnasium.com/Xuhao/china_aqi)
|
6
7
|
[![Coverage Status](https://coveralls.io/repos/Xuhao/china_aqi/badge.png?branch=master)](https://coveralls.io/r/Xuhao/china_aqi?branch=master)
|
7
8
|
|
8
9
|
China Air Quality Index API for Ruby. Thanks [pm25.in][pm25_in] for provide all the AQI sources for us as free, all the data is form China's official sector. it's a reall gread work! ChinaAqi gem provide some interface base on Ruby On Rails, before to use it, you need ask for a token form [pm25.in][pm25_in_api].
|
@@ -16,6 +17,19 @@ All monitoring stations in most cities of China are available, most AQI data are
|
|
16
17
|
- **PM2.5**: 颗粒物(粒径小于等于2.5μm)
|
17
18
|
- **SO2**: 二氧化硫
|
18
19
|
|
20
|
+
### Chinese PM2.5 reporting from U.S. Consulate
|
21
|
+
|
22
|
+
About PM2.5 reporting from U.S. Consulate please check out: [pm25
|
23
|
+
](https://github.com/ekohe/pm25)
|
24
|
+
|
25
|
+
## Compatibility
|
26
|
+
CRuby 1.9.3 or greater, 2.2+ recommended. JRuby not sure.
|
27
|
+
|
28
|
+
Support for Rails:
|
29
|
+
* Rails 3 (not sure)
|
30
|
+
* Rails 4 (tested)
|
31
|
+
* Rails 5 (tested)
|
32
|
+
|
19
33
|
## Installation
|
20
34
|
|
21
35
|
Add this line to your application's Gemfile:
|
@@ -30,6 +44,8 @@ Or install it yourself as:
|
|
30
44
|
|
31
45
|
$ gem install china_aqi
|
32
46
|
|
47
|
+
### For Rails app
|
48
|
+
|
33
49
|
Run install generator:
|
34
50
|
|
35
51
|
$ rails generate china_aqi:install
|
@@ -39,8 +55,18 @@ It will add a new line in config/application.rb:
|
|
39
55
|
```ruby
|
40
56
|
config.china_aqi_token = 'you_token_here'
|
41
57
|
```
|
58
|
+
Put your token there.
|
59
|
+
|
60
|
+
### For non-Rails app
|
42
61
|
|
43
|
-
|
62
|
+
Just require `china_aqi` and then assign a valid token for it:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'rubygems'
|
66
|
+
require 'china_aqi'
|
67
|
+
|
68
|
+
ChinaAqi.token = 'you_token_here'
|
69
|
+
```
|
44
70
|
|
45
71
|
## Usage
|
46
72
|
|
@@ -51,7 +77,7 @@ As we mention at the beginning, we must get a token form [pm25.in][pm25_in_api]
|
|
51
77
|
Most APIs accept three params:
|
52
78
|
|
53
79
|
- `city`: city name can be chinese characters, pinyin and area code('上海' or 'shanghai' or '021')
|
54
|
-
- `avg`: true/false, optional, if true,return average for all monitoring stations, default is true.
|
80
|
+
- `avg`: true/false, optional, if true, return average for all monitoring stations, default is true.
|
55
81
|
- `stations`: yes/no, optional, if yes, return data for all monitoring stations; if no, just return average without stations data, default is yes.
|
56
82
|
|
57
83
|
`avg` and `stations` params is optional, it will use defaut value if not set them.
|
@@ -152,7 +178,11 @@ ChinaAqi.get_stations_for_city('shanghai')
|
|
152
178
|
ChinaAqi.get_stations_for_city('021')
|
153
179
|
# same as:
|
154
180
|
ChinaAqi::CityStations.get('上海/shanghai/021')
|
155
|
-
# {"city"=>"上海", "stations"=>[{"station_name"=>"普陀"
|
181
|
+
# {"city"=>"上海", "stations"=>[{"station_name"=>"普陀" ...
|
182
|
+
|
183
|
+
# Get the list for cities which have AQI data.
|
184
|
+
ChinaAqi.available_cities
|
185
|
+
# {"cities"=>["七台河", "三亚", "三明", "三门峡" ...
|
156
186
|
```
|
157
187
|
|
158
188
|
## Contributing
|
@@ -165,4 +195,4 @@ ChinaAqi::CityStations.get('上海/shanghai/021')
|
|
165
195
|
|
166
196
|
|
167
197
|
[pm25_in]: http://www.pm25.in
|
168
|
-
[pm25_in_api]: http://www.pm25.in/api_doc
|
198
|
+
[pm25_in_api]: http://www.pm25.in/api_doc
|
data/china_aqi.gemspec
CHANGED
@@ -1,26 +1,30 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'china_aqi/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'china_aqi'
|
8
7
|
spec.version = ChinaAqi::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
8
|
+
spec.authors = ['Xuhao']
|
9
|
+
spec.email = ['xuhao@rubyfans.com']
|
10
|
+
spec.description = 'China AQI API from PM25.in'
|
11
|
+
spec.summary = 'China AQI API from PM25.in'
|
12
|
+
spec.homepage = 'https://github.com/Xuhao/china_aqi'
|
13
|
+
spec.license = 'MIT'
|
15
14
|
|
16
|
-
spec.files = `git ls-files`.split(
|
15
|
+
spec.files = `git ls-files`.split($RS)
|
17
16
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
18
|
+
spec.require_paths = ['lib']
|
20
19
|
|
21
|
-
spec.
|
22
|
-
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.
|
20
|
+
spec.required_ruby_version = '>= 1.9.3'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
if RUBY_VERSION < '2.2.2'
|
25
|
+
spec.add_dependency 'activesupport', '~> 4.0'
|
26
|
+
else
|
27
|
+
spec.add_dependency 'activesupport'
|
28
|
+
end
|
25
29
|
spec.add_dependency 'httparty'
|
26
30
|
end
|
data/lib/china_aqi.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'china_aqi/version'
|
2
2
|
module ChinaAqi
|
3
3
|
def self.token
|
4
4
|
@token ||= begin
|
@@ -15,4 +15,5 @@ end
|
|
15
15
|
|
16
16
|
require 'china_aqi/base'
|
17
17
|
require 'china_aqi/exceptions'
|
18
|
-
require 'china_aqi/helper/city_stations'
|
18
|
+
require 'china_aqi/helper/city_stations'
|
19
|
+
require 'china_aqi/helper/available_cities'
|
data/lib/china_aqi/base.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'china_aqi/utility'
|
2
2
|
module ChinaAqi
|
3
3
|
# Fetch data with different method
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Pass city name and params those API accept.
|
6
6
|
#
|
7
7
|
# shanghai = ChinaAqi::PM25.new('city_name', params = { avg: true/false, stations: :yes/:no })
|
@@ -37,8 +37,8 @@ module ChinaAqi
|
|
37
37
|
include ChinaAqi::Utility
|
38
38
|
attr_accessor :token
|
39
39
|
|
40
|
-
def initialize(*
|
41
|
-
raise TokenMissingError,
|
40
|
+
def initialize(*_)
|
41
|
+
raise TokenMissingError, %Q(
|
42
42
|
#{'*' * 66}
|
43
43
|
Token is missing!
|
44
44
|
|
@@ -51,12 +51,12 @@ module ChinaAqi
|
|
51
51
|
# config/application.rb
|
52
52
|
config.china_aqi_token = "you_token"
|
53
53
|
|
54
|
-
#{'*' * 66}
|
54
|
+
#{'*' * 66}) unless ChinaAqi.token
|
55
55
|
@token = ChinaAqi.token
|
56
56
|
end
|
57
57
|
|
58
58
|
def self.get(*args)
|
59
|
-
|
59
|
+
new(*args).get
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
@@ -64,7 +64,7 @@ module ChinaAqi
|
|
64
64
|
class DynamicBase < Base
|
65
65
|
attr_accessor :city
|
66
66
|
|
67
|
-
def initialize(city, querys
|
67
|
+
def initialize(city, querys={ avg: true, stations: :yes })
|
68
68
|
super
|
69
69
|
@city = city
|
70
70
|
@parmas = querys.merge(city: city, token: ChinaAqi.token)
|
@@ -92,4 +92,4 @@ require 'china_aqi/aqi/dynamic/so2'
|
|
92
92
|
require 'china_aqi/aqi/static/global'
|
93
93
|
require 'china_aqi/aqi/static/ranking'
|
94
94
|
|
95
|
-
require 'china_aqi/aqi/station'
|
95
|
+
require 'china_aqi/aqi/station'
|
data/lib/china_aqi/exceptions.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'china_aqi/utility'
|
2
1
|
module ChinaAqi
|
3
2
|
# All monitoring station names in one city
|
4
3
|
class CityStations < DynamicBase
|
@@ -9,6 +8,6 @@ module ChinaAqi
|
|
9
8
|
def get_stations_for_city(city)
|
10
9
|
CityStations.new(city).get
|
11
10
|
end
|
12
|
-
|
11
|
+
|
13
12
|
module_function :get_stations_for_city
|
14
|
-
end
|
13
|
+
end
|
data/lib/china_aqi/utility.rb
CHANGED
@@ -2,6 +2,7 @@ require 'httparty'
|
|
2
2
|
require 'active_support/core_ext/class/attribute'
|
3
3
|
require 'active_support/json/decoding'
|
4
4
|
require 'active_support/core_ext/object/to_query'
|
5
|
+
require 'active_support/core_ext/object/blank'
|
5
6
|
require 'active_support/concern'
|
6
7
|
module ChinaAqi
|
7
8
|
module Utility
|
@@ -11,7 +12,7 @@ module ChinaAqi
|
|
11
12
|
include HTTParty
|
12
13
|
attr_accessor :parmas
|
13
14
|
class_attribute :base_uri, :method
|
14
|
-
self.base_uri = URI::HTTP.build(
|
15
|
+
self.base_uri = URI::HTTP.build(host: 'www.pm25.in')
|
15
16
|
end
|
16
17
|
|
17
18
|
def get
|
@@ -20,7 +21,8 @@ module ChinaAqi
|
|
20
21
|
|
21
22
|
def uri
|
22
23
|
raise NotImplementedError, "Please set value for 'method' class attributes in '#{self.class.name}' class." unless self.class.method
|
23
|
-
self.class.
|
24
|
+
method = self.class.method.to_s.include?('/') ? self.class.method : "/querys/#{self.class.method}"
|
25
|
+
self.class.base_uri.path = "/api#{method}.json"
|
24
26
|
self.class.base_uri.query = @parmas.to_query
|
25
27
|
self.class.base_uri
|
26
28
|
end
|
@@ -29,4 +31,4 @@ module ChinaAqi
|
|
29
31
|
uri.to_s
|
30
32
|
end
|
31
33
|
end
|
32
|
-
end
|
34
|
+
end
|