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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +60 -0
  4. data/.travis.yml +5 -1
  5. data/CHANGELOG.md +9 -0
  6. data/Gemfile +11 -3
  7. data/README.md +34 -4
  8. data/china_aqi.gemspec +18 -14
  9. data/lib/china_aqi.rb +3 -2
  10. data/lib/china_aqi/aqi/dynamic/city.rb +1 -1
  11. data/lib/china_aqi/aqi/dynamic/city_pro.rb +1 -1
  12. data/lib/china_aqi/aqi/dynamic/co.rb +1 -1
  13. data/lib/china_aqi/aqi/dynamic/no2.rb +1 -1
  14. data/lib/china_aqi/aqi/dynamic/o3.rb +1 -1
  15. data/lib/china_aqi/aqi/dynamic/pm10.rb +1 -1
  16. data/lib/china_aqi/aqi/dynamic/pm25.rb +1 -1
  17. data/lib/china_aqi/aqi/dynamic/so2.rb +1 -1
  18. data/lib/china_aqi/aqi/static/global.rb +1 -1
  19. data/lib/china_aqi/aqi/static/ranking.rb +1 -1
  20. data/lib/china_aqi/aqi/station.rb +1 -1
  21. data/lib/china_aqi/base.rb +7 -7
  22. data/lib/china_aqi/exceptions.rb +1 -1
  23. data/lib/china_aqi/helper/available_cities.rb +11 -0
  24. data/lib/china_aqi/helper/city_stations.rb +2 -3
  25. data/lib/china_aqi/utility.rb +5 -3
  26. data/lib/china_aqi/version.rb +1 -1
  27. data/lib/generators/china_aqi/install/install_generator.rb +3 -2
  28. data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/ChinaAqi_AvailableCities/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +139 -0
  29. data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/have_module_function_called_available_cities_to_get_all_available_cities.yml +139 -0
  30. data/spec/cassettes/ChinaAqi/ChinaAqi_AvailableCities/return_a_hash_with_aqi_data_when_call_get_instance_method.yml +139 -0
  31. data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/ChinaAqi_CityStations/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +69 -0
  32. data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/have_module_function_called_get_stations_for_city_to_get_stations_for_one_city.yml +69 -0
  33. data/spec/cassettes/ChinaAqi/ChinaAqi_CityStations/return_a_hash_with_aqi_data_when_call_get_instance_method.yml +69 -0
  34. data/spec/cassettes/ChinaAqi/ChinaAqi_Station/ChinaAqi_Station/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +63 -0
  35. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_CO/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
  36. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_City/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +95 -0
  37. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_CityPro/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
  38. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_NO2/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
  39. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_O3/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +106 -0
  40. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_PM10/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +101 -0
  41. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_PM25/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
  42. data/spec/cassettes/ChinaAqi/Dynamic_models/ChinaAqi_SO2/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +100 -0
  43. data/spec/cassettes/ChinaAqi/Static_models/ChinaAqi_Global/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
  44. data/spec/cassettes/ChinaAqi/Static_models/ChinaAqi_Ranking/return_an_array_or_hash_with_aqi_data_when_call_get_method.yml +58 -0
  45. data/spec/china_aqi/aqi_spec.rb +89 -113
  46. data/spec/china_aqi/base_spec.rb +25 -32
  47. data/spec/china_aqi_spec.rb +6 -6
  48. data/spec/spec_helper.rb +7 -0
  49. metadata +54 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5629478b4a11380f5cf5768d6e93381dcef35081
4
- data.tar.gz: 73938de2bcb5dcbd52b3607f391759823dccdc6b
3
+ metadata.gz: e3feada665c53c16d850f1046b9b3e4fbe124720
4
+ data.tar.gz: 9d13fbabc8af55eb04b19550aab1f1d9ab26a507
5
5
  SHA512:
6
- metadata.gz: 54948bc0de829400be061cda90c2b810f87440cfb2d390dfc1188a0763186ad4744341fe6da42061f91c90399a02cda896d94b70e5532a77f1fbd5a678082ce5
7
- data.tar.gz: 53c0da5802bc59812543ada719f660d20a47ab24e8d40eeee438fe41b6da8a936b48ac48d5f80900ba9755a4e21087af83fa0f57b10b930d139140da7fabfb43
6
+ metadata.gz: 4e40d15d2d7b14a24ad8e4c969c50dd5cc7fcba26cd406def4d29bb6afaf0fe006c9c34294e5af4f188b2be3047c7466590b496c72b01442ea9f4916ae9a3a66
7
+ data.tar.gz: d6a9ed650f69ccaabd803da5af8ce86a2573fad1de0df1454b808c7e4f4f00e6246a495fa1ad68eeac793e31ac6949d2dfaecb257d770a55960cb408de002268
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .versions.conf
@@ -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
@@ -1,4 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 2.0.0
4
+ - 2.0
5
+ - 2.2
6
+ - 2.3
7
+ before_install:
8
+ - gem install bundler
@@ -0,0 +1,9 @@
1
+ ## v0.0.5 (Sep 17, 2016)
2
+
3
+ * New api for get available cities:
4
+ ```ruby
5
+ ChinaAqi.available_cities # or ChinaAqi::AvailableCities.new.get
6
+ # {"cities"=>["七台河", "三亚", "三明", "三门峡" ...
7
+ ```
8
+
9
+ * Compatibility improvements.
data/Gemfile CHANGED
@@ -1,8 +1,16 @@
1
- source 'https://rubygems.org'
2
- # source 'http://ruby.taobao.org'
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
- gem 'rake', group: :test
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
- Put you token there.
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
@@ -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 = "china_aqi"
6
+ spec.name = 'china_aqi'
8
7
  spec.version = ChinaAqi::VERSION
9
- spec.authors = ["Xuhao"]
10
- spec.email = ["xuhao@rubyfans.com"]
11
- spec.description = %q{China AQI API from PM25.in}
12
- spec.summary = %q{China AQI API from PM25.in}
13
- spec.homepage = "https://github.com/Xuhao/china_aqi"
14
- spec.license = "MIT"
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 = ["lib"]
18
+ spec.require_paths = ['lib']
20
19
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
24
- spec.add_dependency 'activesupport'
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
@@ -1,4 +1,4 @@
1
- require "china_aqi/version"
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'
@@ -3,4 +3,4 @@ module ChinaAqi
3
3
  class City < DynamicBase
4
4
  self.method = :only_aqi
5
5
  end
6
- end
6
+ end
@@ -3,4 +3,4 @@ module ChinaAqi
3
3
  class CityPro < DynamicBase
4
4
  self.method = :aqi_details
5
5
  end
6
- end
6
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class CO < DynamicBase
3
3
  self.method = :co
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class NO2 < DynamicBase
3
3
  self.method = :no2
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class O3 < DynamicBase
3
3
  self.method = :o3
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class PM10 < DynamicBase
3
3
  self.method = :pm10
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class PM25 < DynamicBase
3
3
  self.method = :pm2_5
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module ChinaAqi
2
2
  class SO2 < DynamicBase
3
3
  self.method = :so2
4
4
  end
5
- end
5
+ end
@@ -5,4 +5,4 @@ module ChinaAqi
5
5
  class Global < StaticBase
6
6
  self.method = :all_cities
7
7
  end
8
- end
8
+ end
@@ -3,4 +3,4 @@ module ChinaAqi
3
3
  class Ranking < StaticBase
4
4
  self.method = :aqi_ranking
5
5
  end
6
- end
6
+ end
@@ -30,4 +30,4 @@ module ChinaAqi
30
30
  @parmas = { station_code: station_code, token: ChinaAqi.token }
31
31
  end
32
32
  end
33
- end
33
+ end
@@ -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(*args)
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}~ unless ChinaAqi.token
54
+ #{'*' * 66}) unless ChinaAqi.token
55
55
  @token = ChinaAqi.token
56
56
  end
57
57
 
58
58
  def self.get(*args)
59
- self.new(*args).get
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 = {avg: true, stations: :yes})
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'
@@ -1,4 +1,4 @@
1
1
  module ChinaAqi
2
2
  class TokenMissingError < StandardError; end
3
3
  class NotImplementedError < StandardError; end
4
- end
4
+ end
@@ -0,0 +1,11 @@
1
+ module ChinaAqi
2
+ class AvailableCities < StaticBase
3
+ self.method = '/querys'
4
+ end
5
+
6
+ def available_cities
7
+ AvailableCities.new.get
8
+ end
9
+
10
+ module_function :available_cities
11
+ end
@@ -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
@@ -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({host: 'www.pm25.in'})
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.base_uri.path = "/api/querys/#{self.class.method.to_s}.json"
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