weather_hacker 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fda08dc8d56cf2c30b93319960bee84a46913e3
4
+ data.tar.gz: a1540b074c12bdd2b81acc8495d4388a28b6b14f
5
+ SHA512:
6
+ metadata.gz: a1775d1e8922cb4883ab2b96747f009ffdf3025b74da890d362a0897854290fe825b942875f3c8e336f0cec88e20698c6a6cfb30cc9080ab25774891a1a34d59
7
+ data.tar.gz: 0a631c08a76ddcc95c43aebbae9567f672bcffac22bfe703cb133a1b53cda6982c046c48733574608943f8f718f6a62b227898c109cb3e14ffccb98fdeb565a1
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in weather_hacker.gemspec
4
4
  gemspec
5
+ gem 'json', '~> 1.7.7'
data/README.md CHANGED
@@ -6,19 +6,45 @@ Library for [Livedoor Weather Web Service](http://weather.livedoor.com/weather_h
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
10
-
11
- gem 'weather_hacker'
12
-
13
- And then execute:
14
-
15
- $ bundle
9
+ ```
10
+ $ gem install weather_hacker
11
+ ```
16
12
 
17
- Or install it yourself as:
13
+ ## Usage
14
+ ### Command
15
+ WeatherHacker provides ```weather``` command to watch the weather forecast:
18
16
 
19
- $ gem install weather_hacker
17
+ ```
18
+ $ weather 690-0261
19
+ 2012-04-15
20
+ {
21
+ "weather" => "晴れ",
22
+ "temperature" => {
23
+ "max" => "18",
24
+ "min" => "5"
25
+ }
26
+ }
27
+
28
+ 2012-04-16
29
+ {
30
+ "weather" => "晴のち曇",
31
+ "temperature" => {
32
+ "max" => "18",
33
+ "min" => "2"
34
+ }
35
+ }
36
+
37
+ 2012-04-17
38
+ {
39
+ "weather" => "曇り",
40
+ "temperature" => {
41
+ "max" => "17",
42
+ "min" => "9"
43
+ }
44
+ }
45
+ ```
20
46
 
21
- ## Usage
47
+ ### Example
22
48
  Here's an example script to watch the weather forecast:
23
49
 
24
50
  ```ruby
@@ -43,12 +69,12 @@ $ ruby forecast.rb
43
69
  weather: 晴れ
44
70
  temperature:
45
71
  max: '18'
46
- min: '1'
72
+ min: '5'
47
73
  ---
48
74
  weather: 晴れ
49
75
  temperature:
50
76
  max: '18'
51
- min: '1'
77
+ min: '5'
52
78
  ---
53
79
  weather: 晴時々曇
54
80
  temperature:
@@ -57,14 +83,6 @@ temperature:
57
83
  ---
58
84
  weather: 曇り
59
85
  temperature:
60
- max: '21'
86
+ max: '17'
61
87
  min: '9'
62
88
  ```
63
-
64
- ## Contributing
65
-
66
- 1. Fork it
67
- 2. Create your feature branch (`git checkout -b my-new-feature`)
68
- 3. Commit your changes (`git commit -am 'Added some feature'`)
69
- 4. Push to the branch (`git push origin my-new-feature`)
70
- 5. Create new Pull Request
@@ -3,148 +3,148 @@
3
3
  class WeatherHacker
4
4
  module AreaData
5
5
  ID_BY_CITY = {
6
- "稚内" => 1,
7
- "旭川" => 2,
8
- "留萌" => 3,
9
- "札幌" => 4,
10
- "岩見沢" => 5,
11
- "倶知安" => 6,
12
- "網走" => 7,
13
- "北見" => 8,
14
- "紋別" => 9,
15
- "根室" => 10,
16
- "釧路" => 11,
17
- "帯広" => 12,
18
- "室蘭" => 13,
19
- "浦河" => 14,
20
- "函館" => 15,
21
- "江差" => 16,
22
- "青森" => 17,
23
- "むつ" => 18,
24
- "八戸" => 19,
25
- "秋田" => 20,
26
- "横手" => 21,
27
- "盛岡" => 22,
28
- "宮古" => 23,
29
- "大船渡" => 24,
30
- "仙台" => 25,
31
- "白石" => 26,
32
- "山形" => 27,
33
- "米沢" => 28,
34
- "酒田" => 29,
35
- "新庄" => 30,
36
- "福島" => 31,
37
- "小名浜" => 32,
38
- "若松" => 33,
39
- "水戸" => 54,
40
- "土浦" => 55,
41
- "宇都宮" => 56,
42
- "大田原" => 57,
43
- "前橋" => 58,
44
- "みなかみ" => 59,
45
- "さいたま" => 60,
46
- "熊谷" => 61,
47
- "秩父" => 62,
48
- "東京" => 63,
49
- "大島" => 64,
50
- "八丈島" => 65,
51
- "父島" => 66,
52
- "千葉" => 67,
53
- "銚子" => 68,
54
- "館山" => 69,
55
- "横浜" => 70,
56
- "小田原" => 71,
57
- "甲府" => 75,
58
- "河口湖" => 76,
59
- "富山" => 44,
60
- "伏木" => 45,
61
- "金沢" => 46,
62
- "輪島" => 47,
63
- "福井" => 48,
64
- "敦賀" => 49,
65
- "新潟" => 50,
66
- "長岡" => 51,
67
- "高田" => 52,
68
- "相川" => 53,
69
- "長野" => 72,
70
- "松本" => 73,
71
- "飯田" => 74,
72
- "静岡" => 34,
73
- "網代" => 35,
74
- "三島" => 36,
75
- "浜松" => 37,
76
- "名古屋" => 38,
77
- "豊橋" => 39,
78
- "岐阜" => 40,
79
- "高山" => 41,
80
- "津" => 42,
81
- "尾鷲" => 43,
82
- "大津" => 77,
83
- "彦根" => 78,
84
- "京都" => 79,
85
- "舞鶴" => 80,
86
- "大阪" => 81,
87
- "神戸" => 82,
88
- "豊岡" => 83,
89
- "奈良" => 84,
90
- "風屋" => 85,
91
- "和歌山" => 86,
92
- "潮岬" => 87,
93
- "岡山" => 88,
94
- "津山" => 89,
95
- "広島" => 90,
96
- "庄原" => 91,
97
- "松江" => 92,
98
- "浜田" => 93,
99
- "西郷" => 94,
100
- "鳥取" => 95,
101
- "米子" => 96,
102
- "下関" => 97,
103
- "山口" => 98,
104
- "柳井" => 99,
105
- "萩" => 100,
106
- "徳島" => 101,
107
- "日和佐" => 102,
108
- "高松" => 103,
109
- "松山" => 104,
110
- "新居浜" => 105,
111
- "宇和島" => 106,
112
- "高知" => 107,
113
- "室戸" => 108,
114
- "清水" => 109,
115
- "福岡" => 110,
116
- "八幡" => 111,
117
- "飯塚" => 112,
118
- "久留米" => 113,
119
- "大分" => 114,
120
- "中津" => 115,
121
- "日田" => 116,
122
- "佐伯" => 117,
123
- "長崎" => 118,
124
- "佐世保" => 119,
125
- "厳原" => 120,
126
- "福江" => 121,
127
- "佐賀" => 122,
128
- "伊万里" => 123,
129
- "熊本" => 124,
130
- "阿蘇乙姫" => 125,
131
- "牛深" => 126,
132
- "人吉" => 127,
133
- "宮崎" => 128,
134
- "延岡" => 129,
135
- "都城" => 130,
136
- "高千穂" => 131,
137
- "鹿児島" => 132,
138
- "鹿屋" => 133,
139
- "種子島" => 134,
140
- "名瀬" => 135,
141
- "那覇" => 136,
142
- "名護" => 137,
143
- "久米島" => 138,
144
- "南大東島" => 139,
145
- "宮古島" => 140,
146
- "石垣島" => 141,
147
- "与那国島" => 142
6
+ "稚内" => '011000',
7
+ "旭川" => '012010',
8
+ "留萌" => '012020',
9
+ "網走" => '013010',
10
+ "北見" => '013020',
11
+ "紋別" => '013030',
12
+ "根室" => '014010',
13
+ "釧路" => '014020',
14
+ "帯広" => '014030',
15
+ "室蘭" => '015010',
16
+ "浦河" => '015020',
17
+ "札幌" => '016010',
18
+ "岩見沢" => '016020',
19
+ "倶知安" => '016030',
20
+ "函館" => '017010',
21
+ "江差" => '017020',
22
+ "青森" => '020010',
23
+ "むつ" => '020020',
24
+ "八戸" => '020030',
25
+ "盛岡" => '030010',
26
+ "宮古" => '030020',
27
+ "大船渡" => '030030',
28
+ "仙台" => '040010',
29
+ "白石" => '040020',
30
+ "秋田" => '050010',
31
+ "横手" => '050020',
32
+ "山形" => '060010',
33
+ "米沢" => '060020',
34
+ "酒田" => '060030',
35
+ "新庄" => '060040',
36
+ "福島" => '070010',
37
+ "小名浜" => '070020',
38
+ "若松" => '070030',
39
+ "水戸" => '080010',
40
+ "土浦" => '080020',
41
+ "宇都宮" => '090010',
42
+ "大田原" => '090020',
43
+ "前橋" => '100010',
44
+ "みなかみ" => '100020',
45
+ "さいたま" => '110010',
46
+ "熊谷" => '110020',
47
+ "秩父" => '110030',
48
+ "千葉" => '120010',
49
+ "銚子" => '120020',
50
+ "館山" => '120030',
51
+ "東京" => '130010',
52
+ "大島" => '130020',
53
+ "八丈島" => '130030',
54
+ "父島" => '130040',
55
+ "横浜" => '140010',
56
+ "小田原" => '140020',
57
+ "新潟" => '150010',
58
+ "長岡" => '150020',
59
+ "高田" => '150030',
60
+ "相川" => '150040',
61
+ "富山" => '160010',
62
+ "伏木" => '160020',
63
+ "金沢" => '170010',
64
+ "輪島" => '170020',
65
+ "福井" => '180010',
66
+ "敦賀" => '180020',
67
+ "甲府" => '190010',
68
+ "河口湖" => '190020',
69
+ "長野" => '200010',
70
+ "松本" => '200020',
71
+ "飯田" => '200030',
72
+ "岐阜" => '210010',
73
+ "高山" => '210020',
74
+ "静岡" => '220010',
75
+ "網代" => '220020',
76
+ "三島" => '220030',
77
+ "浜松" => '220040',
78
+ "名古屋" => '230010',
79
+ "豊橋" => '230020',
80
+ "津" => '240010',
81
+ "尾鷲" => '240020',
82
+ "大津" => '250010',
83
+ "彦根" => '250020',
84
+ "京都" => '260010',
85
+ "舞鶴" => '260020',
86
+ "大阪" => '270000',
87
+ "神戸" => '280010',
88
+ "豊岡" => '280020',
89
+ "奈良" => '290010',
90
+ "風屋" => '290020',
91
+ "和歌山" => '300010',
92
+ "潮岬" => '300020',
93
+ "鳥取" => '310010',
94
+ "米子" => '310020',
95
+ "松江" => '320010',
96
+ "浜田" => '320020',
97
+ "西郷" => '320030',
98
+ "岡山" => '330010',
99
+ "津山" => '330020',
100
+ "広島" => '340010',
101
+ "庄原" => '340020',
102
+ "下関" => '350010',
103
+ "山口" => '350020',
104
+ "柳井" => '350030',
105
+ "萩" => '350040',
106
+ "徳島" => '360010',
107
+ "日和佐" => '360020',
108
+ "高松" => '370000',
109
+ "松山" => '380010',
110
+ "新居浜" => '380020',
111
+ "宇和島" => '380030',
112
+ "高知" => '390010',
113
+ "室戸岬" => '390020',
114
+ "清水" => '390030',
115
+ "福岡" => '400010',
116
+ "八幡" => '400020',
117
+ "飯塚" => '400030',
118
+ "久留米" => '400040',
119
+ "佐賀" => '410010',
120
+ "伊万里" => '410020',
121
+ "長崎" => '420010',
122
+ "佐世保" => '420020',
123
+ "厳原" => '420030',
124
+ "福江" => '420040',
125
+ "熊本" => '430010',
126
+ "阿蘇乙姫" => '430020',
127
+ "牛深" => '430030',
128
+ "人吉" => '430040',
129
+ "大分" => '440010',
130
+ "中津" => '440020',
131
+ "日田" => '440030',
132
+ "佐伯" => '440040',
133
+ "宮崎" => '450010',
134
+ "延岡" => '450020',
135
+ "都城" => '450030',
136
+ "高千穂" => '450040',
137
+ "鹿児島" => '460010',
138
+ "鹿屋" => '460020',
139
+ "種子島" => '460030',
140
+ "名瀬" => '460040',
141
+ "那覇" => '471010',
142
+ "名護" => '471020',
143
+ "久米島" => '471030',
144
+ "南大東" => '472000',
145
+ "宮古島" => '473000',
146
+ "石垣島" => '474010',
147
+ "与那国島" => '474020'
148
148
  }
149
149
 
150
150
  PREF_BY_CITY = {
@@ -4,8 +4,8 @@ class WeatherHacker
4
4
  class Client
5
5
  include HTTParty
6
6
 
7
- WEATHER_URL = "http://weather.livedoor.com/forecast/webservice/rest/v1"
8
- AREA_TABLE_URL = "http://weather.livedoor.com/forecast/rss/forecastmap.xml"
7
+ WEATHER_URL = "http://weather.livedoor.com/forecast/webservice/json/v1"
8
+ AREA_TABLE_URL = "http://weather.livedoor.com/forecast/rss/primary_area.xml"
9
9
  ZIPCODE_URL = "http://zip.cgis.biz/xml/zip.php"
10
10
 
11
11
  # @city_id_by_zipcode is cached
@@ -15,17 +15,23 @@ class WeatherHacker
15
15
 
16
16
  # get weather data by zipcode
17
17
  def get_weather(zipcode, opts = {})
18
- city_id = city_id_by_zipcode(zipcode) or return
19
- query = { :city => city_id, :day => :today }.merge(opts)
20
- hash = get WEATHER_URL, :query => query
18
+ city_id = city_id_by_zipcode(zipcode) or return
19
+ query = { :city => city_id }.merge(opts)
20
+ hash = get WEATHER_URL, :query => query
21
+ forecast = get_forecast(hash, opts)
22
+
23
+ weather = forecast["telop"]
24
+ max = forecast["temperature"]["max"]
25
+ min = forecast["temperature"]["min"]
26
+ max_celsius = max["celsius"] unless max.nil?
27
+ min_celsius = min["celsius"] unless min.nil?
21
28
 
22
29
  {
23
- "weather" => hash["lwws"]["telop"],
30
+ "weather" => weather,
24
31
  "temperature" => {
25
- "max" => hash["lwws"]["temperature"]["max"]["celsius"],
26
- "min" => hash["lwws"]["temperature"]["min"]["celsius"],
27
- },
28
- }
32
+ "max" => max_celsius,
33
+ "min" => min_celsius,
34
+ }}
29
35
  rescue ParseError
30
36
  nil
31
37
  end
@@ -37,6 +43,14 @@ class WeatherHacker
37
43
  self.class.get(*args)
38
44
  end
39
45
 
46
+ # return forecast hash
47
+ def get_forecast(hash, opts)
48
+ days = ['today', 'tomorrow', 'dayaftertomorrow']
49
+ day_num = 0
50
+ day_num = days.find_index(opts['day']) unless opts['day'].nil?
51
+ hash['forecasts'][day_num]
52
+ end
53
+
40
54
  # return city id via zipcode API
41
55
  def city_id_by_zipcode(zipcode)
42
56
  zipcode = canonical_zipcode(zipcode)
@@ -46,7 +60,7 @@ class WeatherHacker
46
60
  hash = info_by_zipcode(zipcode)
47
61
  city = canonical_city(hash["city"])
48
62
  pref = canonical_pref(hash["state"])
49
- city_id_by_area(city, pref)
63
+ city_id_by_area(city, pref).to_i
50
64
  end
51
65
  end
52
66
 
@@ -134,19 +148,15 @@ class WeatherHacker
134
148
  def parse_area_table(hash)
135
149
  @pref_by_city ||= {}
136
150
  @id_by_city ||= {}
137
-
138
- hash["rss"]["channel"]["source"]["area"].each do |area|
139
- prefs = [area["pref"]].flatten
140
- prefs.each do |pref|
141
- pref_name = canonical_pref(pref["title"])
142
-
143
- cities = [pref["city"]].flatten
144
- cities.each do |city|
145
- id = city["id"].to_i
146
- title = city["title"]
147
- @id_by_city[title] = id
148
- @pref_by_city[title] = pref_name
149
- end
151
+ hash["rss"]["channel"]["source"]["pref"].each do |pref|
152
+ pref_name = canonical_pref(pref["title"])
153
+
154
+ cities = [pref["city"]].flatten
155
+ cities.each do |city|
156
+ id = city["id"].to_i
157
+ title = city["title"]
158
+ @id_by_city[title] = id
159
+ @pref_by_city[title] = pref_name
150
160
  end
151
161
  end
152
162
  rescue
@@ -1,3 +1,3 @@
1
1
  class WeatherHacker
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,82 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_hacker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
5
- prerelease:
4
+ version: 0.1.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ryo NAKAMURA
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-04-14 00:00:00.000000000 Z
11
+ date: 2013-05-11 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
- requirement: &70327885780260 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.9.2
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70327885780260
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: httparty
27
- requirement: &70327885779840 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *70327885779840
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: awesome_print
38
- requirement: &70327885779380 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - '>='
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :runtime
45
49
  prerelease: false
46
- version_requirements: *70327885779380
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: rspec
49
- requirement: &70327885778960 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *70327885778960
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: simplecov
60
- requirement: &70327885778520 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - '>='
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *70327885778520
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: simplecov-vim
71
- requirement: &70327885778100 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
- - - ! '>='
87
+ - - '>='
75
88
  - !ruby/object:Gem::Version
76
89
  version: '0'
77
90
  type: :development
78
91
  prerelease: false
79
- version_requirements: *70327885778100
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
80
97
  description: Library for Livedoor Weather Web Service
81
98
  email:
82
99
  - r7kamura@gmail.com
@@ -105,33 +122,26 @@ files:
105
122
  - weather_hacker.gemspec
106
123
  homepage: http://r7kamura.github.com/weather_hacker/
107
124
  licenses: []
125
+ metadata: {}
108
126
  post_install_message:
109
127
  rdoc_options: []
110
128
  require_paths:
111
129
  - lib
112
130
  required_ruby_version: !ruby/object:Gem::Requirement
113
- none: false
114
131
  requirements:
115
- - - ! '>='
132
+ - - '>='
116
133
  - !ruby/object:Gem::Version
117
134
  version: '0'
118
- segments:
119
- - 0
120
- hash: -1410371417977522141
121
135
  required_rubygems_version: !ruby/object:Gem::Requirement
122
- none: false
123
136
  requirements:
124
- - - ! '>='
137
+ - - '>='
125
138
  - !ruby/object:Gem::Version
126
139
  version: '0'
127
- segments:
128
- - 0
129
- hash: -1410371417977522141
130
140
  requirements: []
131
141
  rubyforge_project:
132
- rubygems_version: 1.8.15
142
+ rubygems_version: 2.0.0
133
143
  signing_key:
134
- specification_version: 3
144
+ specification_version: 4
135
145
  summary: Weather Forecaster via Livedoor Weather Web Service
136
146
  test_files:
137
147
  - spec/fixtures/id_by_city.rb