Gem 写了两个关于天气查询的 gem - think_page (心知天气) 与 smart_weather (中国气象局)

neverlandxy_naix · 2014年09月04日 · 最后由 dddd1919 回复于 2014年09月04日 · 5391 次阅读

最近在对接一些外部的 API 服务,写了一个基于 rest-client 的包装器 restclient_api_base, 在这个基础上完成了两个天气查询的 gem 包 -- think_pagesmart_weather

大家有需求的可以试试,帮助我一起改善这几个 Gem.

ThinkPage

心知天气 (ThinkPage) Ruby SDK, 详细参数请见 天气数据接口 API, 心知天气的城市列表请见 城市列表

Installation:

Add this line to your application's Gemfile:

gem 'think_page'

And then execute:

$ bundle

Or install it yourself as:

$ gem install think_page

Usage:

require 'think_page'

ThinkPage.private_params = { key: 'Your key' }

# 获取指定城市的全部天气信息
ThinkPage.get_all(city: 'CHBJ000000'....)

# 获取指定城市的实时天气
ThinkPage.get_now(city: 'CHBJ000000'....)

# 获取指定城市的预报天气
ThinkPage.get_future(city: 'CHBJ000000'....)

# 获取指定城市的AQI、PM2.5、PM10、CO、NO2、O3等空气质量信息
ThinkPage.get_air(city: 'CHBJ000000'....)

# 获取指定城市的穿衣、紫外线、汽车、感冒、运动等生活建议指数信息
ThinkPage.get_suggestion(city: 'CHBJ000000'....)

# 获取过去24小时每小时天气和空气质量历史数据。仅支持大客户会员
ThinkPage.get_history24h(city: 'CHBJ000000'....)

# 获取每个城市分别根据历史数据计算的每天天气预报准确度。仅支持大客户会员
ThinkPage.get_accuracy(city: 'CHBJ000000'....)

SmartWeather

中国气象网 SmartWeatherAPI Ruby SDK

详细接口说明请见 SmartWeatherAPI_Lite_WebAPI 版产品使用说明书

Installation:

Add this line to your application's Gemfile:

gem 'smart_weather'

And then execute:

$ bundle

Or install it yourself as:

$ gem install smart_weather

Usage:

require 'smart_weather'

SmartWeather.app_id      = 'your app id'
SmartWeather.private_key = 'your private_key'

# 获取支持的省份
SmartWeather::Province.all

# 获取支持的城市的 areaid
SmartWeather::City.all

# 天气实况
SmartWeather.get_observe(areaid: '101010100')

# 常规预报(3d)
SmartWeather.get_forecast(areaid: '101010100')

# 天气指数
SmartWeather.get_index(areaid: '101010100')

Gem 中“以 REST 方式”指的是使用者需要以 REST 方式调用吗?

#1 楼 @creeek 可以不用,但默认是 json 格式

#2 楼 @neverlandxy_naix 有点晕~ REST, RESTful, JSON, 我已经傻傻分不清楚

要是能使用城市名 (中文或拼音) 来查询就好了,实际使用中还要查询城市码这点很麻烦

#3 楼 @creeek 我说错了,请求默认返回的格式是 json.....

#4 楼 @Teddy 这个两边的接口没有支持,我会再出个 json 文件,就可以直接导出到数据库中了

能统一成地区码就好了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号