china_aqi 0.0.1 → 0.0.2

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
2
  SHA1:
3
- metadata.gz: ca4d41542b8602aa534e099a19393fdd74ff74e6
4
- data.tar.gz: 1622846a24168d2ca7724a0f8ea981a2d46e4b2f
3
+ metadata.gz: 8557f12eec57ca0e6a707ff5578d1a19317a7d7d
4
+ data.tar.gz: f7c6adfeffe1020eb8632aebeef0da4dbd2864ef
5
5
  SHA512:
6
- metadata.gz: 5f47c648c382a4258f1ce0cf8d74e2a57e0c0e6b13e5c3b6ea38448fd9455798f5416c4a7ff75f10383850227e1d6ace422e660f56002fc821e259e54b5fc875
7
- data.tar.gz: ea802169eb2a35232b3e58e06480b4cb52a7634103d2d08c8a657b748d3b2ef37616cf9ca3b74d548534ec2823b5a209c46b87b7ba42fbbfa33db345b85e921a
6
+ metadata.gz: 6cc29f3ef3d23ae6195a11e1d909125e0507b0c3009d6e9deff423ce2f19290fe685e00eacd3f1692a4756cabba14f96b03bee3e6a332fdd02dbbd172f06c19b
7
+ data.tar.gz: e87708752855e36a58c35873e4d2f787c5ca649285afef97bedc44852c78314b75000d682b182a307899c92d723a3602fd89a699fc04d04acbfab18a41ec75e2
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # ChinaAqi
2
2
 
3
- [![Build Status](https://travis-ci.org/Xuhao/china_aqi.png?branch=master)](https://travis-ci.org/Xuhao/china_aqi)
4
- [![Code Climate](https://codeclimate.com/github/Xuhao/china_aqi.png)](https://codeclimate.com/github/Xuhao/china_aqi)
3
+ [![Gem Version](https://badge.fury.io/rb/china_aqi.png)](http://badge.fury.io/rb/china_aqi)
4
+ [![Build Status](https://travis-ci.org/Xuhao/china_aqi.png?branch=master)](https://travis-ci.org/Xuhao/china_aqi)
5
+ [![Code Climate](https://codeclimate.com/github/Xuhao/china_aqi.png)](https://codeclimate.com/github/Xuhao/china_aqi)
6
+ [![Coverage Status](https://coveralls.io/repos/Xuhao/china_aqi/badge.png?branch=master)](https://coveralls.io/r/Xuhao/china_aqi?branch=master)
5
7
 
6
- 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!
7
-
8
- 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].
8
+ 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].
9
9
 
10
10
  All monitoring stations in most cities of China are available, most AQI data are available, they are:
11
11
 
@@ -34,14 +34,18 @@ Run install generator:
34
34
 
35
35
  $ rails generate china_aqi:install
36
36
 
37
- It will create file: config/initializers/china_aqi.rb
37
+ It will add a new line in config/application.rb:
38
+
39
+ ```ruby
40
+ config.china_aqi_token = 'you_token_here'
41
+ ```
42
+
43
+ Put you token there.
38
44
 
39
45
  ## Usage
40
46
 
41
47
  As we mention at the beginning, we must get a token form [pm25.in][pm25_in_api] before we use their APIs, it's free!
42
48
 
43
- Once you get the token, put it in config/initializers/china_aqi.rb, then enjoy!
44
-
45
49
  ### Parameters
46
50
 
47
51
  Most APIs accept three params:
@@ -113,6 +117,14 @@ ChinaAqi::CityPro.new('shanghai').get # or ChinaAqi::CityPro.get('shanghai')
113
117
  # => [{"aqi"=>57, "area"=>"上海", "co"=>0.0, "co_24h"=>0.0, "no2"=>0, "no2_24h"=>0, ...
114
118
  ```
115
119
 
120
+ ##### Detail data for one monitoring station
121
+
122
+ ```ruby
123
+ ChinaAqi::Station.new('1141A').get # or ChinaAqi::Station.get('1141A')
124
+ # => [{"aqi"=>57, "area"=>"上海", "co"=>0.0, "co_24h"=>0.0, "no2"=>0, "no2_24h"=>0, ...
125
+ ```
126
+
127
+
116
128
  ##### Fetch all data
117
129
 
118
130
  ```ruby
@@ -7,11 +7,11 @@ module ChinaAqi
7
7
  #
8
8
  # same as:
9
9
  #
10
- # ChinaAqi::CityStations.get(station_code)
10
+ # ChinaAqi::Station.get(station_code)
11
11
  #
12
12
  # You can get all station codes for one city like this:
13
13
  #
14
- # ChinaAqi::CityStations.new(station_code).get
14
+ # ChinaAqi::Station.new(station_code).get
15
15
  #
16
16
  # or like this:
17
17
  #
@@ -19,7 +19,7 @@ module ChinaAqi
19
19
  #
20
20
  # and like this:
21
21
  #
22
- # ChinaAqi::CityStations.get(city)
22
+ # ChinaAqi::Station.get(city)
23
23
  class Station < Base
24
24
  self.method = :aqis_by_station
25
25
  attr_accessor :station_code
@@ -54,10 +54,6 @@ module ChinaAqi
54
54
  #{'*' * 66}~ unless ChinaAqi.token
55
55
  @token = ChinaAqi.token
56
56
  end
57
-
58
- def self.get(*args)
59
- self.new(*args).get
60
- end
61
57
  end
62
58
 
63
59
  # APIs with some parameters
@@ -14,6 +14,12 @@ module ChinaAqi
14
14
  self.base_uri = URI::HTTP.build({host: 'www.pm25.in'})
15
15
  end
16
16
 
17
+ module ClassMethods
18
+ def get(*args)
19
+ self.new(*args).get
20
+ end
21
+ end
22
+
17
23
  def get
18
24
  ::ActiveSupport::JSON.decode(HTTParty.get(url).body)
19
25
  end
@@ -1,3 +1,3 @@
1
1
  module ChinaAqi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi do
4
+ before(:each) { ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq' }
5
+
6
+ ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq'
7
+
8
+ describe ChinaAqi::City do
9
+ include_examples "common dynamic examples", ChinaAqi::City
10
+ include_examples "class attribute", ChinaAqi::City, :only_aqi
11
+ end
12
+
13
+ describe ChinaAqi::CityPro do
14
+ include_examples "common dynamic examples", ChinaAqi::CityPro
15
+ include_examples "class attribute", ChinaAqi::CityPro, :aqi_details
16
+ end
17
+
18
+ describe ChinaAqi::CO do
19
+ include_examples "common dynamic examples", ChinaAqi::CO
20
+ include_examples "class attribute", ChinaAqi::CO, :co
21
+ end
22
+
23
+ describe ChinaAqi::NO2 do
24
+ include_examples "common dynamic examples", ChinaAqi::NO2
25
+ include_examples "class attribute", ChinaAqi::NO2, :no2
26
+ end
27
+
28
+ describe ChinaAqi::O3 do
29
+ include_examples "common dynamic examples", ChinaAqi::O3
30
+ include_examples "class attribute", ChinaAqi::O3, :o3
31
+ end
32
+
33
+ describe ChinaAqi::PM10 do
34
+ include_examples "common dynamic examples", ChinaAqi::PM10
35
+ include_examples "class attribute", ChinaAqi::PM10, :pm10
36
+ end
37
+
38
+ describe ChinaAqi::PM25 do
39
+ include_examples "common dynamic examples", ChinaAqi::PM25
40
+ include_examples "class attribute", ChinaAqi::PM25, :pm2_5
41
+ end
42
+
43
+ describe ChinaAqi::SO2 do
44
+ include_examples "common dynamic examples", ChinaAqi::SO2
45
+ include_examples "class attribute", ChinaAqi::SO2, :so2
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi do
4
+ before(:each) { ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq' }
5
+
6
+ ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq'
7
+
8
+ describe ChinaAqi::Global do
9
+ include_examples "common static examples", ChinaAqi::Global
10
+ include_examples "class attribute", ChinaAqi::Global, :all_cities
11
+ end
12
+
13
+ describe ChinaAqi::Ranking do
14
+ include_examples "common static examples", ChinaAqi::Ranking
15
+ include_examples "class attribute", ChinaAqi::Ranking, :aqi_ranking
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi::Station do
4
+ before(:each) { ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq' }
5
+
6
+ ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq'
7
+ obj = ChinaAqi::Station.new('1141A')
8
+
9
+ include_examples "common examples", ChinaAqi::Station, obj
10
+ include_examples "class attribute", ChinaAqi::Station, :aqis_by_station
11
+
12
+ it 'should have station_code instance variable' do
13
+ obj = ChinaAqi::Station.new('1141A')
14
+ obj.instance_variable_defined?(:@station_code).should be_true
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi do
4
+ let(:token) { '5j1znBVAsnSf5xQyNQyq' }
5
+
6
+ describe ChinaAqi::Base do
7
+ before(:each) { ChinaAqi.token = nil }
8
+
9
+ shared_examples "raise NotImplementedError" do |instance_method_name|
10
+ it "should raise NotImplementedError when call #{instance_method_name} method" do
11
+ ChinaAqi.token = token
12
+ expect { ChinaAqi::Base.new.send instance_method_name }.to raise_error(ChinaAqi::NotImplementedError, "Please set value for 'method' class attributes in 'ChinaAqi::Base' class.")
13
+ end
14
+ end
15
+
16
+ context 'Instace variable' do
17
+ it 'should raise error when initial a object if there is no token' do
18
+ expect { ChinaAqi::Base.new }.to raise_error(ChinaAqi::TokenMissingError)
19
+ end
20
+
21
+ it 'should have instance variable to store token if token was seted' do
22
+ ChinaAqi.token = 'token_sample'
23
+ obj = ChinaAqi::Base.new
24
+ obj.instance_variable_defined?(:@token).should be_true
25
+ obj.token.should eq('token_sample')
26
+ end
27
+ end
28
+
29
+ context 'Instace methods' do
30
+ include_examples "raise NotImplementedError", 'uri'
31
+ include_examples "raise NotImplementedError", 'get'
32
+ include_examples "raise NotImplementedError", 'url'
33
+ end
34
+
35
+ context 'Class methods' do
36
+ it "should raise error when call get class method" do
37
+ expect { ChinaAqi::Base.get('Shanghai') }.to raise_error
38
+ end
39
+ end
40
+ end
41
+
42
+ describe ChinaAqi::DynamicBase do
43
+ it 'should have city and parmas instance variable' do
44
+ ChinaAqi.token = token
45
+ obj = ChinaAqi::DynamicBase.new('Shanghai')
46
+ obj.instance_variable_defined?(:@city).should be_true
47
+ obj.instance_variable_defined?(:@parmas).should be_true
48
+ end
49
+ end
50
+
51
+ describe ChinaAqi::StaticBase do
52
+ it 'should have parmas instance variable' do
53
+ ChinaAqi.token = token
54
+ obj = ChinaAqi::StaticBase.new
55
+ obj.instance_variable_defined?(:@parmas).should be_true
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi do
4
+ describe ChinaAqi::CityStations do
5
+ include_examples "common dynamic examples", ChinaAqi::CityStations
6
+ include_examples "class attribute", ChinaAqi::CityStations, :station_names
7
+
8
+ before(:each) { ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq' }
9
+
10
+ it 'should have module function called get_stations_for_city to get stations for one city' do
11
+ ChinaAqi.respond_to?(:get_stations_for_city).should be_true
12
+ stations = ChinaAqi.get_stations_for_city('Shanghai')
13
+ stations.should be_a(Hash)
14
+ stations.key?('city').should be_true
15
+ stations.key?('stations').should be_true
16
+ stations['stations'].should be_a(Array)
17
+ stations['city'].should eq('上海')
18
+ end
19
+
20
+ it 'should return a hash with aqi data when call "get" instance method' do
21
+ stations = ChinaAqi::CityStations.new('Shanghai').get
22
+ stations.should be_a(Hash)
23
+ stations.key?('city').should be_true
24
+ stations.key?('stations').should be_true
25
+ stations['stations'].should be_a(Array)
26
+ stations['city'].should eq('上海')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChinaAqi do
4
+ shared_examples "common examples" do |klass, obj|
5
+ it 'should return URI::HTTP object own context when call "uri" method' do
6
+ uri = obj.uri
7
+ uri.should be_a(URI::HTTP)
8
+ uri.host.should eq('www.pm25.in')
9
+ uri.path.should eq("/api/querys/#{klass.method.to_s}.json")
10
+ end
11
+
12
+ it 'should return url string with own context when call "url" method' do
13
+ obj.url.should be_a(String)
14
+ obj.url.include?("http://www.pm25.in/api/querys/#{klass.method.to_s}.json").should be_true
15
+ end
16
+ end
17
+
18
+ shared_examples 'class attribute' do |klass, value|
19
+ it "should have class attribute 'method' equal #{value}" do
20
+ klass.method.should eq(value)
21
+ end
22
+ end
23
+
24
+ shared_examples 'get array' do |klass|
25
+ before(:each) { ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq' }
26
+ let(:obj) { klass.new('Shanghai') }
27
+
28
+ it 'should return an array with aqi data when call "get" method' do
29
+ data = obj.get
30
+ data.should be_a(Array)
31
+ data.all? {|aqi| aqi.key? 'aqi'}.should be_true
32
+ end
33
+ end
34
+
35
+ shared_examples "common dynamic examples" do |klass|
36
+ ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq'
37
+ obj = klass.new('Shanghai')
38
+
39
+ include_examples "common examples", klass, obj
40
+
41
+ end
42
+
43
+ shared_examples "common static examples" do |klass|
44
+ ChinaAqi.token = '5j1znBVAsnSf5xQyNQyq'
45
+ obj = klass.new
46
+
47
+ include_examples "common examples", klass, obj
48
+ end
49
+ end
@@ -1,11 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ChinaAqi do
4
+ before(:each) { ChinaAqi.token = nil }
5
+
4
6
  it 'should have a version number' do
5
7
  ChinaAqi::VERSION.should_not be_nil
6
8
  end
7
9
 
8
- it 'should do something useful' do
9
- false.should be_false
10
+ it 'should return token which defined in rails application configuration file.' do
11
+ stub_const('Rails', Module.new)
12
+ Rails.stub_chain('application.config.china_aqi_token') { 'chian_aqi_token_test_sample' }
13
+ ChinaAqi.token.should eq('chian_aqi_token_test_sample')
10
14
  end
11
15
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'china_aqi'
3
1
  require 'coveralls'
4
2
  Coveralls.wear!
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'china_aqi'
5
+
6
+ require 'china_aqi/shared_spec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: china_aqi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xuhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-18 00:00:00.000000000 Z
11
+ date: 2013-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -114,6 +114,12 @@ files:
114
114
  - lib/china_aqi/version.rb
115
115
  - lib/generators/china_aqi/install/USAGE
116
116
  - lib/generators/china_aqi/install/install_generator.rb
117
+ - spec/china_aqi/aqi/dynamic_spec.rb
118
+ - spec/china_aqi/aqi/static_spec.rb
119
+ - spec/china_aqi/aqi/station_spec.rb
120
+ - spec/china_aqi/base_spec.rb
121
+ - spec/china_aqi/helper/city_stations_spec.rb
122
+ - spec/china_aqi/shared_spec.rb
117
123
  - spec/china_aqi_spec.rb
118
124
  - spec/spec_helper.rb
119
125
  homepage: https://github.com/Xuhao/china_aqi
@@ -141,5 +147,11 @@ signing_key:
141
147
  specification_version: 4
142
148
  summary: China AQI API from PM25.in
143
149
  test_files:
150
+ - spec/china_aqi/aqi/dynamic_spec.rb
151
+ - spec/china_aqi/aqi/static_spec.rb
152
+ - spec/china_aqi/aqi/station_spec.rb
153
+ - spec/china_aqi/base_spec.rb
154
+ - spec/china_aqi/helper/city_stations_spec.rb
155
+ - spec/china_aqi/shared_spec.rb
144
156
  - spec/china_aqi_spec.rb
145
157
  - spec/spec_helper.rb