iarea 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -9,6 +9,8 @@ A library for DoCoMo Open Iarea.
9
9
  area = Iarea::Area.find_by_lat_lng(43.0568397222222,141.3478975)
10
10
  area.name # => "すすきの"
11
11
  area.areacode # => "00208"
12
+ area.zone.name # => "北海道"
13
+ area.prefecture.name # => "北海道"
12
14
 
13
15
  = References
14
16
 
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require 'psych'
1
2
  load 'lib/tasks/iarea.rake'
2
3
 
3
4
  begin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.1.0
data/db/iareadata.sqlite3 CHANGED
Binary file
data/iarea.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{iarea}
8
- s.version = "0.0.3"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Yoji Shidara"]
12
- s.date = %q{2011-04-04}
12
+ s.date = %q{2011-04-07}
13
13
  s.description = %q{A library for DoCoMo Open Iarea.}
14
14
  s.email = %q{dara@shidara.net}
15
15
  s.extra_rdoc_files = [
@@ -24,17 +24,23 @@ Gem::Specification.new do |s|
24
24
  "iarea.gemspec",
25
25
  "lib/iarea.rb",
26
26
  "lib/tasks/iarea.rake",
27
- "spec/iarea/iarea_spec.rb",
27
+ "spec/iarea/area_spec.rb",
28
+ "spec/iarea/prefecture_spec.rb",
29
+ "spec/iarea/util_spec.rb",
30
+ "spec/iarea/zone_spec.rb",
28
31
  "spec/spec_helper.rb",
29
32
  "tools/import_areas.rb",
30
33
  "tools/import_meshes.rb"
31
34
  ]
32
35
  s.homepage = %q{http://github.com/darashi/iarea}
33
36
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.5.2}
37
+ s.rubygems_version = %q{1.6.1}
35
38
  s.summary = %q{A library for DoCoMo Open Iarea.}
36
39
  s.test_files = [
37
- "spec/iarea/iarea_spec.rb",
40
+ "spec/iarea/area_spec.rb",
41
+ "spec/iarea/prefecture_spec.rb",
42
+ "spec/iarea/util_spec.rb",
43
+ "spec/iarea/zone_spec.rb",
38
44
  "spec/spec_helper.rb"
39
45
  ]
40
46
 
data/lib/iarea.rb CHANGED
@@ -1,24 +1,84 @@
1
1
  require 'sequel'
2
+ require 'ostruct'
2
3
 
3
4
  module Iarea
4
5
  DB = Sequel.sqlite(File.expand_path("../../db/iareadata.sqlite3", __FILE__))
5
6
 
6
- class Area
7
- attr_reader :areaid, :subareaid, :areacode, :name, :west, :south, :east, :north
7
+ class Area < OpenStruct
8
+ @@areas = {}
9
+ def prefecture
10
+ Prefecture.find(self.prefecture_id)
11
+ end
12
+
13
+ def zone
14
+ Zone.find(self.zone_id)
15
+ end
8
16
 
9
- def initialize(data)
10
- data.each do |k, v|
11
- instance_variable_set("@#{k}", v)
17
+ def neighbors
18
+ DB[:neighbors].where(:areacode => areacode).select(:neighbor_areacode).map do |neighbor|
19
+ Area.find neighbor[:neighbor_areacode]
12
20
  end
13
21
  end
14
22
 
15
23
  class << self
24
+ def find(areacode)
25
+ @@areas[areacode] ||= new DB[:areas].where(:areacode => areacode).first
26
+ end
27
+
16
28
  def find_by_lat_lng(lat, lng)
17
29
  meshcodes = Utils.expand_meshcode(Utils.lat_lng_to_meshcode(lat, lng))
18
- area = DB[:meshes].where(:meshcode => meshcodes).join(:areas, :areacode => :areacode).first
30
+ area = DB[:meshes].where(:meshcode => meshcodes).select(:areacode).first
19
31
  return nil if area.nil?
20
- area[:name].force_encoding("UTF-8")
21
- new(area)
32
+ find(area[:areacode])
33
+ end
34
+ end
35
+ end
36
+
37
+ class Zone < OpenStruct
38
+ @@zones = []
39
+
40
+ def prefectures
41
+ DB[:prefectures].where(:zone_id => self.id).select(:id).order(:id).map do |prefecture|
42
+ Prefecture.find(prefecture[:id])
43
+ end
44
+ end
45
+
46
+ def areas
47
+ DB[:areas].where(:zone_id => self.id).select(:areacode).order(:areacode).map do |area|
48
+ Area.find(area[:areacode])
49
+ end
50
+ end
51
+
52
+ class << self
53
+ def find(id)
54
+ @@zones[id] ||= new DB[:zones].where(:id => id).first
55
+ end
56
+
57
+ def all
58
+ DB[:zones].select(:id).order(:id).all.map{ |z| find(z[:id]) }
59
+ end
60
+ end
61
+ end
62
+
63
+ class Prefecture < OpenStruct
64
+ @@prefectures = []
65
+ def zone
66
+ Zone.find(self.zone_id)
67
+ end
68
+
69
+ def areas
70
+ DB[:areas].where(:prefecture_id => self.id).select(:areacode).order(:areacode).map do |area|
71
+ Area.find(area[:areacode])
72
+ end
73
+ end
74
+
75
+ class << self
76
+ def find(id)
77
+ @@prefectures[id] ||= new DB[:prefectures].where(:id => id).first
78
+ end
79
+
80
+ def all
81
+ DB[:prefectures].select(:id).order(:id).all.map{ |pr| find(pr[:id]) }
22
82
  end
23
83
  end
24
84
  end
@@ -58,10 +118,3 @@ module Iarea
58
118
  end
59
119
  end
60
120
  end
61
-
62
- =begin
63
- require 'sequel'
64
- db = Sequel.sqlite("iareadata.sqlite3")
65
- meshcodes = Iarea::Utils.expand_meshcode(meshcode)
66
- puts db[:meshes].where(:meshcode => meshcodes).join(:areas, :areacode => :areacode).first[:name]
67
- =end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Iarea::Area do
5
+ context "at 43.0568397222222,141.3478975" do
6
+ subject { Iarea::Area.find_by_lat_lng(43.0568397222222,141.3478975) }
7
+
8
+ its(:name) { should == "すすきの" }
9
+ its(:areacode) { should == "00208" }
10
+ its(:areaid) { should == "002" }
11
+ its(:subareaid) { should == "08" }
12
+ it do
13
+ subject.prefecture.name.should == "北海道"
14
+ end
15
+ it do
16
+ subject.zone.name.should == "北海道"
17
+ end
18
+ it do
19
+ subject.neighbors.map(&:name).should == ["大通公園周辺", "大通東", "山鼻/藻岩周辺", "中島公園周辺"]
20
+ end
21
+ end
22
+
23
+ context "at 35.606022,139.734979" do
24
+ subject { Iarea::Area.find_by_lat_lng(35.606022,139.734979) }
25
+ its(:name) { should == "大井町" }
26
+ its(:areacode) { should == "06003" }
27
+ its(:areaid) { should == "060" }
28
+ its(:subareaid) { should == "03" }
29
+ it do
30
+ subject.prefecture.name.should == "東京"
31
+ end
32
+ it do
33
+ subject.zone.name.should == "関東甲信越"
34
+ end
35
+ end
36
+
37
+ context "finding by id" do
38
+ subject { Iarea::Area.find("06003") }
39
+ its(:name) { should == "大井町" }
40
+ end
41
+
42
+ context "at out of Japan" do
43
+ subject do
44
+ Iarea::Area.find_by_lat_lng(0,0)
45
+ end
46
+
47
+ it "should be nil" do
48
+ should be_nil
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Iarea::Prefecture do
5
+ describe 'Tokyo' do
6
+ subject { Iarea::Prefecture.find(12) }
7
+ its(:name) { should == "東京" }
8
+ it do
9
+ subject.zone.name.should == "関東甲信越"
10
+ end
11
+ end
12
+
13
+ describe 'Okinawa' do
14
+ subject { Iarea::Prefecture.find(46) }
15
+ its(:name) { should == "沖縄" }
16
+ it do
17
+ subject.areas.map(&:name).should == ["那覇/浦添", "沖縄/名護", "宮古/石垣"]
18
+ end
19
+ end
20
+
21
+ describe 'equivalence' do
22
+ before do
23
+ @a = Iarea::Prefecture.find(0)
24
+ @b = Iarea::Prefecture.find(0)
25
+ @c = Iarea::Prefecture.find(1)
26
+ end
27
+ it do
28
+ @a.should == @b
29
+ end
30
+ it do
31
+ @a.should_not == @c
32
+ end
33
+ end
34
+
35
+ it '#all' do
36
+ Iarea::Prefecture.all.should == (0..46).map {|i| Iarea::Prefecture.find(i)}
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Iarea::Utils do
5
+ it ".lat_lng_to_meshcode" do
6
+ Iarea::Utils.lat_lng_to_meshcode(42.8840971954764,141.571321907496).should == "64412430032"
7
+ end
8
+ it ".expand_meshcode" do
9
+ Iarea::Utils.expand_meshcode("64412430032").should == ["644124", "6441243", "64412430", "644124300", "6441243003", "64412430032", "64412430032"]
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Iarea::Zone do
5
+ describe 'Tokyo' do
6
+ subject { Iarea::Zone.find(2) }
7
+ its(:name) { should == "関東甲信越" }
8
+
9
+ it '#prefectures' do
10
+ subject.prefectures.map(&:name).should == ["栃木", "群馬", "茨城", "埼玉", "千葉", "東京", "神奈川", "山梨", "長野", "新潟"]
11
+ end
12
+ end
13
+
14
+ describe 'Shikoku' do
15
+ subject { Iarea::Zone.find(7) }
16
+ it '#areas' do
17
+ subject.areas.map(&:name).should == ["高松市周辺", "東讃", "中讃", "西讃", "徳島市", "徳島県北部", "徳島県西部", "徳島県南部", "中予", "東予", "南予北部", "南予南部", "高知市", "高知県中部", "高知県東部", "高知県西部"]
18
+ end
19
+ end
20
+
21
+ describe 'equivalence' do
22
+ before do
23
+ @a = Iarea::Zone.find(0)
24
+ @b = Iarea::Zone.find(0)
25
+ @c = Iarea::Zone.find(1)
26
+ end
27
+ it do
28
+ @a.should == @b
29
+ end
30
+ it do
31
+ @a.should_not == @c
32
+ end
33
+ end
34
+
35
+ it '#all' do
36
+ Iarea::Zone.all.should == (0..8).map {|i| Iarea::Zone.find(i)}
37
+ end
38
+ end
@@ -47,6 +47,13 @@ db.create_table :areas do
47
47
  index :prefecture_id
48
48
  end
49
49
 
50
+ db.drop_table :neighbors if db.table_exists?(:neighbors)
51
+ db.create_table :neighbors do
52
+ String :areacode
53
+ String :neighbor_areacode
54
+ index :areacode
55
+ end
56
+
50
57
  db.transaction do
51
58
  js = open(src_path, "r:cp932").read
52
59
  js.sub!(%r|^var IAreaDef=new function\(\)\{|, "")
@@ -68,7 +75,7 @@ db.transaction do
68
75
  data['areas'].each do |areacode, area|
69
76
  $stderr.puts " importing area %s %s" % [areacode, area['name']]
70
77
  # insert area
71
- area = {
78
+ hash = {
72
79
  :areaid => areacode[0..2],
73
80
  :subareaid => areacode[3..4],
74
81
  :areacode => areacode,
@@ -80,6 +87,10 @@ db.transaction do
80
87
  :zone_id => area['zone'],
81
88
  :prefecture_id => area['pref']
82
89
  }
83
- db[:areas].insert(area)
90
+ db[:areas].insert(hash)
91
+
92
+ area['neighbors'].each do |neighbor|
93
+ db[:neighbors].insert({:areacode => areacode, :neighbor_areacode => neighbor})
94
+ end
84
95
  end
85
96
  end
metadata CHANGED
@@ -1,60 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: iarea
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
4
5
  prerelease:
5
- version: 0.0.3
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Yoji Shidara
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-04-04 00:00:00 +09:00
12
+ date: 2011-04-07 00:00:00.000000000 +09:00
14
13
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: sequel
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &2160917720 !ruby/object:Gem::Requirement
20
18
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
24
22
  version: 3.22.0
25
23
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: sqlite3-ruby
29
24
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *2160917720
26
+ - !ruby/object:Gem::Dependency
27
+ name: sqlite3-ruby
28
+ requirement: &2160914320 !ruby/object:Gem::Requirement
31
29
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
35
33
  version: 1.3.3
36
34
  type: :runtime
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: rspec
40
35
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *2160914320
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &2160913380 !ruby/object:Gem::Requirement
42
40
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
46
44
  version: 2.5.0
47
45
  type: :development
48
- version_requirements: *id003
46
+ prerelease: false
47
+ version_requirements: *2160913380
49
48
  description: A library for DoCoMo Open Iarea.
50
49
  email: dara@shidara.net
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
- extra_rdoc_files:
52
+ extra_rdoc_files:
56
53
  - README.rdoc
57
- files:
54
+ files:
58
55
  - MIT-LICENSE
59
56
  - README.rdoc
60
57
  - Rakefile
@@ -63,38 +60,41 @@ files:
63
60
  - iarea.gemspec
64
61
  - lib/iarea.rb
65
62
  - lib/tasks/iarea.rake
66
- - spec/iarea/iarea_spec.rb
63
+ - spec/iarea/area_spec.rb
64
+ - spec/iarea/prefecture_spec.rb
65
+ - spec/iarea/util_spec.rb
66
+ - spec/iarea/zone_spec.rb
67
67
  - spec/spec_helper.rb
68
68
  - tools/import_areas.rb
69
69
  - tools/import_meshes.rb
70
70
  has_rdoc: true
71
71
  homepage: http://github.com/darashi/iarea
72
72
  licenses: []
73
-
74
73
  post_install_message:
75
74
  rdoc_options: []
76
-
77
- require_paths:
75
+ require_paths:
78
76
  - lib
79
- required_ruby_version: !ruby/object:Gem::Requirement
77
+ required_ruby_version: !ruby/object:Gem::Requirement
80
78
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- version: "0"
85
- required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
84
  none: false
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: "0"
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
91
89
  requirements: []
92
-
93
90
  rubyforge_project:
94
- rubygems_version: 1.5.2
91
+ rubygems_version: 1.6.1
95
92
  signing_key:
96
93
  specification_version: 3
97
94
  summary: A library for DoCoMo Open Iarea.
98
- test_files:
99
- - spec/iarea/iarea_spec.rb
95
+ test_files:
96
+ - spec/iarea/area_spec.rb
97
+ - spec/iarea/prefecture_spec.rb
98
+ - spec/iarea/util_spec.rb
99
+ - spec/iarea/zone_spec.rb
100
100
  - spec/spec_helper.rb
@@ -1,34 +0,0 @@
1
- # encoding: UTF-8
2
- require 'spec_helper'
3
-
4
- describe Iarea::Utils do
5
- it ".lat_lng_to_meshcode" do
6
- Iarea::Utils.lat_lng_to_meshcode(42.8840971954764,141.571321907496).should == "64412430032"
7
- end
8
- it ".expand_meshcode" do
9
- Iarea::Utils.expand_meshcode("64412430032").should == ["644124", "6441243", "64412430", "644124300", "6441243003", "64412430032", "64412430032"]
10
- end
11
- end
12
-
13
- describe Iarea::Area do
14
- context "at 43.0568397222222,141.3478975" do
15
- subject do
16
- Iarea::Area.find_by_lat_lng(43.0568397222222,141.3478975)
17
- end
18
-
19
- its(:name) { should == "すすきの" }
20
- its(:areacode) { should == "00208" }
21
- its(:areaid) { should == "002" }
22
- its(:subareaid) { should == "08" }
23
- end
24
-
25
- context "at out of Japan" do
26
- subject do
27
- Iarea::Area.find_by_lat_lng(0,0)
28
- end
29
-
30
- it "should be nil" do
31
- should be_nil
32
- end
33
- end
34
- end