weather_hacker 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,11 @@ class WeatherHacker
8
8
  AREA_TABLE_URL = "http://weather.livedoor.com/forecast/rss/forecastmap.xml"
9
9
  ZIPCODE_URL = "http://zip.cgis.biz/xml/zip.php"
10
10
 
11
+ # @city_id_by_zipcode is cached
12
+ def initialize
13
+ @city_id_by_zipcode = {}
14
+ end
15
+
11
16
  # get weather data by zipcode
12
17
  def get_weather(zipcode, opts = {})
13
18
  city_id = city_id_by_zipcode(zipcode) or return
@@ -34,13 +39,26 @@ class WeatherHacker
34
39
 
35
40
  # return city id via zipcode API
36
41
  def city_id_by_zipcode(zipcode)
37
- zipcode = zipcode.to_s.tr("0-9", "0-9").tr("-", "").tr("-", "")
38
- return unless zipcode.match(/^\d{7}$/)
42
+ zipcode = canonical_zipcode(zipcode)
43
+ return unless valid_zipcode?(zipcode)
44
+
45
+ @city_id_by_zipcode[zipcode] ||= begin
46
+ hash = info_by_zipcode(zipcode)
47
+ city = canonical_city(hash["city"])
48
+ pref = canonical_pref(hash["state"])
49
+ city_id_by_area(city, pref)
50
+ end
51
+ end
39
52
 
40
- hash = info_by_zipcode(zipcode)
41
- city = canonical_city(hash["city"])
42
- pref = canonical_pref(hash["state"])
53
+ def canonical_zipcode(zipcode)
54
+ zipcode.to_s.tr("0-9", "0-9").tr("-", "").tr("-", "")
55
+ end
56
+
57
+ def valid_zipcode?(zipcode)
58
+ zipcode.match(/^\d{7}$/)
59
+ end
43
60
 
61
+ def city_id_by_area(city, pref)
44
62
  WeatherHacker::AreaData::ID_BY_CITY[city] ||
45
63
  WeatherHacker::AreaData::ID_BY_CITY[pref] ||
46
64
  WeatherHacker::AreaData::ID_BY_PREF[pref]
@@ -1,3 +1,3 @@
1
1
  class WeatherHacker
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_hacker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70362866214980 !ruby/object:Gem::Requirement
16
+ requirement: &70327885780260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70362866214980
24
+ version_requirements: *70327885780260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &70362866214560 !ruby/object:Gem::Requirement
27
+ requirement: &70327885779840 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70362866214560
35
+ version_requirements: *70327885779840
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: awesome_print
38
- requirement: &70362866214100 !ruby/object:Gem::Requirement
38
+ requirement: &70327885779380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70362866214100
46
+ version_requirements: *70327885779380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70362866213660 !ruby/object:Gem::Requirement
49
+ requirement: &70327885778960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70362866213660
57
+ version_requirements: *70327885778960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &70362866213240 !ruby/object:Gem::Requirement
60
+ requirement: &70327885778520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70362866213240
68
+ version_requirements: *70327885778520
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov-vim
71
- requirement: &70362866212780 !ruby/object:Gem::Requirement
71
+ requirement: &70327885778100 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70362866212780
79
+ version_requirements: *70327885778100
80
80
  description: Library for Livedoor Weather Web Service
81
81
  email:
82
82
  - r7kamura@gmail.com
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  segments:
119
119
  - 0
120
- hash: -3473716244803351994
120
+ hash: -1410371417977522141
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  none: false
123
123
  requirements:
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  segments:
128
128
  - 0
129
- hash: -3473716244803351994
129
+ hash: -1410371417977522141
130
130
  requirements: []
131
131
  rubyforge_project:
132
132
  rubygems_version: 1.8.15