Skip to content

Commit a87c4e3

Browse files
committed
MISC - prototype
1 parent 75d0fb5 commit a87c4e3

30 files changed

+394
-18
lines changed

.gitignore

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
*.gem
12
*.rbc
2-
*.sassc
3-
.sass-cache
4-
capybara-*.html
5-
.rspec
6-
/.bundle
7-
/vendor/bundle
8-
/log/*
9-
/tmp/*
10-
/db/*.sqlite3
11-
/public/system/*
12-
/coverage/
13-
/spec/tmp/*
14-
**.orig
15-
rerun.txt
16-
pickle-email-*.html
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

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: ruby
2+
rvm:
3+
- 2.0.0

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in china_aqi.gemspec
4+
gemspec

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Xuhao
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+81-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,82 @@
1-
china_aqi
2-
=========
1+
# ChinaAqi
32

4-
China AQI API from PM25.in
3+
China Air Quality Index API for Rails.
4+
5+
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!
6+
7+
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+
9+
## Installation
10+
11+
Add this line to your application's Gemfile:
12+
13+
gem 'china_aqi'
14+
15+
And then execute:
16+
17+
$ bundle
18+
19+
Or install it yourself as:
20+
21+
$ gem install china_aqi
22+
23+
Run install generator:
24+
25+
$ rails generate china_aqi:install
26+
27+
It will create file: config/initializers/china_aqi.rb
28+
29+
## Usage
30+
31+
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!
32+
33+
Once you get the token, put it in config/initializers/china_aqi.rb, then enjoy!
34+
35+
Most AQI data are available, they are:
36+
37+
- **co**: 一氧化碳
38+
- **no2**: 二氧化氮
39+
- **o3**: 臭氧
40+
- **pm10**: 颗粒物(粒径小于等于10μm)
41+
- **pm2.5**: 颗粒物(粒径小于等于2.5μm)
42+
- **so2**: 二氧化硫
43+
44+
All monitoring stations in most cities of China are available.
45+
46+
### Examples
47+
48+
# City name can be Chinese characters, pinyin and area code
49+
# params:
50+
# avg: if true,return average for all monitoring stations, default is true
51+
# stations: if yes, return data for all monitoring stations; if no, just return average.
52+
53+
shanghai = ChinaAqi::PM25.new('上海') # #<ChinaAqi::PM25:0x007fe2a631aef8 @city="上海" ...
54+
shanghai.get # [{"aqi"=>74, "area"=>"上海", "pm2_5"=>48 ...
55+
ChinaAqi::PM25.new('021', avg: true, stations: :yes).get # Same as above.
56+
57+
58+
# Get so2 data for Shanghai
59+
ChinaAqi::SO2.new('shanghai', avg: true, stations: :no).get # [{"aqi"=>74, "area"=> ...
60+
61+
# Get all data for one stations with code "1141A"
62+
ChinaAqi::Station.new('1141A').get # [{"aqi"=>75, "area"=>"上海", "co"=>0.0, ...
63+
64+
# Get all data that the API provide, never use it unless you have to.
65+
ChinaAqi::Global.new.get # [{"aqi"=>144, "area"=>"天津"
66+
67+
### Helpers
68+
69+
# get station names and station codes for one city
70+
ChinaAqi.get_stations_for_city('上海') # {"city"=>"上海", "stations"=>[{"station_name"=>"普陀"
71+
72+
## Contributing
73+
74+
1. Fork it
75+
2. Create your feature branch (`git checkout -b my-new-feature`)
76+
3. Commit your changes (`git commit -am 'Add some feature'`)
77+
4. Push to the branch (`git push origin my-new-feature`)
78+
5. Create new Pull Request
79+
80+
81+
[pm25_in]: http://www.pm25.in
82+
[pm25_in_api]: http://www.pm25.in/api_doc

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

china_aqi.gemspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'china_aqi/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "china_aqi"
8+
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"
15+
16+
spec.files = `git ls-files`.split($/)
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_development_dependency "bundler", "~> 1.3"
22+
spec.add_development_dependency "rake"
23+
spec.add_development_dependency "rspec"
24+
spec.add_dependency 'httparty'
25+
end

lib/ exceptions.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ChinaAqi
2+
class TokenMissingError < StandardError; end
3+
class NotImplementedError < StandardError; end
4+
end

lib/china_aqi.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "china_aqi/version"
2+
3+
module ChinaAqi
4+
mattr_accessor :token, instance_accessor: false
5+
end
6+
7+
require 'china_aqi/exceptions'
8+
require 'china_aqi/base'
9+
require 'china_aqi/city_stations'

lib/china_aqi/aqi/city.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module ChinaAqi
2+
# Fetch simple data for all monitoring stations in one city
3+
class City < Base
4+
self.method = :only_aqi
5+
end
6+
end

lib/china_aqi/aqi/city_pro.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module ChinaAqi
2+
# Fetch detail data for all monitoring stations in one city
3+
class CityPro < Base
4+
self.method = :aqi_details
5+
end
6+
end

lib/china_aqi/aqi/co.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class CO < Base
3+
self.method = :co
4+
end
5+
end

lib/china_aqi/aqi/global.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module ChinaAqi
2+
# All data
3+
# This class is used for fetch data for all cities in china.
4+
# It will return a very big array, don't use it unless you have to!
5+
class Global < Base
6+
self.method = :all_cities
7+
8+
def initialize
9+
raise TokenMissingError, 'Token is missing, please set token first!' if ChinaAqi.token.blank?
10+
@token = ChinaAqi.token
11+
@parmas = { token: ChinaAqi.token }
12+
end
13+
end
14+
end

lib/china_aqi/aqi/no2.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class NO2 < Base
3+
self.method = :no2
4+
end
5+
end

lib/china_aqi/aqi/o3.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class O3 < Base
3+
self.method = :o3
4+
end
5+
end

lib/china_aqi/aqi/pm10.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class PM10 < Base
3+
self.method = :pm10
4+
end
5+
end

lib/china_aqi/aqi/pm25.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class PM25 < Base
3+
self.method = :pm2_5
4+
end
5+
end

lib/china_aqi/aqi/so2.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ChinaAqi
2+
class SO2 < Base
3+
self.method = :so2
4+
end
5+
end

lib/china_aqi/aqi/station.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module ChinaAqi
2+
# Fetch data for one monitoring station
3+
#
4+
# example:
5+
#
6+
# ChinaAqi::Station.new('1141A').get
7+
#
8+
# You can get all station codes for one city like this:
9+
#
10+
# ChinaAqi::CityStations.new(city_name).get
11+
#
12+
# or like this:
13+
#
14+
# ChinaAqi.get_stations_for_city(city)
15+
class Station < Base
16+
self.method = :aqis_by_station
17+
attr_accessor :station_code
18+
19+
def initialize(station_code)
20+
raise TokenMissingError, 'Token is missing, please set token first!' if ChinaAqi.token.blank?
21+
@station_code = station_code
22+
@token = ChinaAqi.token
23+
@parmas = { station_code: station_code, token: ChinaAqi.token }
24+
end
25+
end
26+
end

lib/china_aqi/base.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'china_aqi/utility'
2+
module ChinaAqi
3+
# Fetch data with different method
4+
#
5+
# Pass city name and params those API accept.
6+
#
7+
# shanghai = ChinaAqi::PM25.new('city_name', params = { avg: true/false, stations: :yes/:no })
8+
#
9+
# params:
10+
# avg: if true,return average for all monitoring stations, default is true
11+
# stations: if yes, return data for all monitoring stations; if no, just return average.
12+
#
13+
# checkout explanation in http://www.pm25.in/api_doc
14+
#
15+
# examples
16+
#
17+
# shanghai = ChinaAqi::PM25.new('上海') # #<ChinaAqi::PM25:0x007fe2a631aef8 @city="上海"...
18+
# shanghai.get # [{"aqi"=>74, "area"=>"上海", "pm2_5"=>48,...
19+
#
20+
# # Get so2 data for 上海
21+
# ChinaAqi::SO2.new('上海', avg: false, stations: :no).get # [{"aqi"=>74, "area"=>"上海", "pm2_5"=>48,...
22+
#
23+
# # Get all data for one stations with code "1141A"
24+
# ChinaAqi::Station.new('1141A').get # [{"aqi"=>75, "area"=>"上海", "co"=>0.0, "co_24h"=>0.0
25+
#
26+
# # Get all data that the API provide
27+
# ChinaAqi::Global.new.get
28+
#
29+
class Base
30+
include ChinaAqi::Utility
31+
32+
attr_accessor :city
33+
34+
def initialize(city, querys = {avg: true, stations: :yes})
35+
raise TokenMissingError, 'Token is missing, please set token first!' if ChinaAqi.token.blank?
36+
@city = city
37+
@parmas = querys.merge(city: city, token: ChinaAqi.token)
38+
@token = ChinaAqi.token
39+
end
40+
end
41+
end
42+
43+
require 'china_aqi/aqi/city'
44+
require 'china_aqi/aqi/city_pro'
45+
require 'china_aqi/aqi/co'
46+
require 'china_aqi/aqi/global'
47+
require 'china_aqi/aqi/no2'
48+
require 'china_aqi/aqi/o3'
49+
require 'china_aqi/aqi/pm10'
50+
require 'china_aqi/aqi/pm25'
51+
require 'china_aqi/aqi/so2'
52+
require 'china_aqi/aqi/station'

lib/china_aqi/city_stations.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'china_aqi/utility'
2+
module ChinaAqi
3+
# All monitoring station names in one city
4+
class CityStations
5+
include ChinaAqi::Utility
6+
7+
self.method = :station_names
8+
9+
def initialize(city)
10+
raise TokenMissingError, 'Token is missing, please set token first!' if ChinaAqi.token.blank?
11+
@city = city
12+
@token = ChinaAqi.token
13+
@parmas = { city: city, token: ChinaAqi.token }
14+
end
15+
end
16+
17+
# create a helper in ChinaAqi module
18+
def get_stations_for_city(city)
19+
CityStations.new(city).get
20+
end
21+
22+
module_function :get_stations_for_city
23+
end

lib/china_aqi/exceptions.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ChinaAqi
2+
class TokenMissingError < StandardError; end
3+
class NotImplementedError < StandardError; end
4+
end

0 commit comments

Comments
 (0)