geocoder_jp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +11 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/bin/geojp +52 -0
- data/geocoder_jp.gemspec +23 -0
- data/lib/geocoder_jp.rb +61 -0
- data/lib/geocoder_jp/version.rb +3 -0
- data/spec/geocoder_jp_spec.rb +52 -0
- data/spec/spec_helper.rb +6 -0
- metadata +72 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 yukinoraru
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# GeocoderJp
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'geocoder_jp'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install geocoder_jp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
task :default => :spec
|
5
|
+
|
6
|
+
desc "Run all specs in spec directory"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
9
|
+
spec.rspec_opts = ['--backtrace']
|
10
|
+
end
|
data/bin/geojp
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
require 'optparse'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
require 'pp'
|
6
|
+
require_relative '../lib/geocoder_jp'
|
7
|
+
::Version = GeocoderJP::VERSION
|
8
|
+
|
9
|
+
puts_green = lambda{|msg| puts "\e[32m#{msg}\e[0m"}
|
10
|
+
puts_red = lambda{|msg| puts "\e[31m#{msg}\e[0m"}
|
11
|
+
def show_help
|
12
|
+
puts "Usage: [-o] geojp [spot_name]"
|
13
|
+
puts "Example: geojp -o 東京スカイツリー"
|
14
|
+
puts "Options:"
|
15
|
+
puts " -o : open the spot with Google Maps on www.geocoding.jp"
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
# parse ARGV using 'optparse'
|
20
|
+
is_open = false
|
21
|
+
opt = OptionParser.new
|
22
|
+
opt.on('-o') do
|
23
|
+
is_open = true
|
24
|
+
end
|
25
|
+
opt.on('-h', '--help') do
|
26
|
+
show_help
|
27
|
+
end
|
28
|
+
opt.parse!
|
29
|
+
|
30
|
+
#
|
31
|
+
query = ARGV.first
|
32
|
+
show_help if query.blank?
|
33
|
+
|
34
|
+
response = nil
|
35
|
+
begin
|
36
|
+
response = GeocoderJP.get(query)
|
37
|
+
puts_green.call "Success!"
|
38
|
+
pp response
|
39
|
+
rescue => exception
|
40
|
+
puts_red.call "[ERROR] #{exception.to_s}"
|
41
|
+
end
|
42
|
+
|
43
|
+
if is_open && response
|
44
|
+
system("open #{response['url']}")
|
45
|
+
# 以下Windowsとかでやる場合にリダイレクト専用のHTML作って
|
46
|
+
# やろっかなー、とか考えたけどめんどいからやめた!
|
47
|
+
# f = Tempfile.new(['redirect', '.html'])
|
48
|
+
# f.puts '<meta http-equiv="Refresh" content="0; URL=%s">'%[response["url"]]
|
49
|
+
# puts_green.call "opening..."
|
50
|
+
# system("open #{f.path}")
|
51
|
+
# f.close
|
52
|
+
end
|
data/geocoder_jp.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'geocoder_jp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "geocoder_jp"
|
8
|
+
gem.version = GeocoderJP::VERSION
|
9
|
+
gem.authors = ["yukinoraru"]
|
10
|
+
gem.email = ["yukinoraru@gmail.com"]
|
11
|
+
gem.description = %q{Geocoding.jp API client for Ruby}
|
12
|
+
gem.summary = %q{Geocoding.jp API client for Ruby}
|
13
|
+
gem.homepage = "https://github.com/yukinoraru/geocoder_jp"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# describe dpendency
|
21
|
+
gem.add_dependency "activesupport", "~>3.2.8"
|
22
|
+
|
23
|
+
end
|
data/lib/geocoder_jp.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative 'geocoder_jp/version'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'uri'
|
5
|
+
require 'pp'
|
6
|
+
require 'net/http'
|
7
|
+
require 'kconv'
|
8
|
+
|
9
|
+
module GeocoderJP
|
10
|
+
RequestError = Class.new(StandardError)
|
11
|
+
NotFound = Class.new(StandardError)
|
12
|
+
TooManyRequests = Class.new(StandardError)
|
13
|
+
|
14
|
+
def self.get(address)
|
15
|
+
address = address.toutf8 unless address.nil?
|
16
|
+
raise ArgumentError, "Please input address." if address.blank?
|
17
|
+
url = "http://www.geocoding.jp/api/?v=1.1&q=#{address}".toutf8
|
18
|
+
|
19
|
+
uri = URI.parse(URI.encode(url))
|
20
|
+
|
21
|
+
# GETリクエスト送出
|
22
|
+
#Net::HTTP.get_print(uri); exit
|
23
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
24
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
25
|
+
response = http.request(request)
|
26
|
+
#pp response, response.body
|
27
|
+
raise RequestError if response.code != "200"
|
28
|
+
|
29
|
+
#レスポンスの解析
|
30
|
+
xml = Hash.from_xml(response.body)
|
31
|
+
result = xml["result"]
|
32
|
+
|
33
|
+
# エラーが存在する場合は例外を発生
|
34
|
+
case result["error"]
|
35
|
+
when "003"
|
36
|
+
raise TooManyRequests, "DO NOT request one or more times in 5 seconds."
|
37
|
+
when "004"
|
38
|
+
raise NotFound
|
39
|
+
else
|
40
|
+
# それ以外はエラーコードと共にRequestErrorを返却
|
41
|
+
raise RequestError, "Request failed. Error code=#{result['error']}" if result.include?("error")
|
42
|
+
end
|
43
|
+
|
44
|
+
return result
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.escape_xml(input)
|
48
|
+
result = input.dup
|
49
|
+
{
|
50
|
+
'&' => '&',
|
51
|
+
"'" => ''',
|
52
|
+
'"' => '"',
|
53
|
+
#'<' => '<',
|
54
|
+
#'>' => '>',
|
55
|
+
}.each{ |key, val|
|
56
|
+
result.gsub!(key, val)
|
57
|
+
}
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative 'spec_helper'
|
3
|
+
require_relative '../lib/geocoder_jp'
|
4
|
+
|
5
|
+
# STDOUT.should_receive(:puts).with("foo")
|
6
|
+
describe "GeocoderJP module" do
|
7
|
+
|
8
|
+
describe "誤入力に対する動作" do
|
9
|
+
it "空入力したときエラーが返るか" do
|
10
|
+
[
|
11
|
+
nil,
|
12
|
+
"",
|
13
|
+
" ",
|
14
|
+
" ",
|
15
|
+
" ",
|
16
|
+
" ",
|
17
|
+
].each{ |input|
|
18
|
+
expect {
|
19
|
+
GeocoderJP.get(input)
|
20
|
+
}.to raise_exception(ArgumentError)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
it "存在しないものに対してエラーが返るか" do
|
24
|
+
expect {
|
25
|
+
GeocoderJP.get("404")
|
26
|
+
}.to raise_exception
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "正しい入力に対する動作" do
|
31
|
+
it "東京タワー" do
|
32
|
+
response = GeocoderJP.get("東京タワー")
|
33
|
+
response["coordinate"]["lat"].should_not be_empty
|
34
|
+
end
|
35
|
+
|
36
|
+
it "'東京タワー'を様々な文字コードで入力する(時間かかります)" do
|
37
|
+
[
|
38
|
+
Kconv::JIS,
|
39
|
+
Kconv::EUC,
|
40
|
+
Kconv::SJIS,
|
41
|
+
Kconv::UTF8,
|
42
|
+
].each do |encoding|
|
43
|
+
spot_name = Kconv.kconv("東京タワー", encoding)
|
44
|
+
response = GeocoderJP.get(spot_name)
|
45
|
+
response["coordinate"]["lat"].should_not be_empty
|
46
|
+
sleep 5
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geocoder_jp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- yukinoraru
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70299899721240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.8
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70299899721240
|
25
|
+
description: Geocoding.jp API client for Ruby
|
26
|
+
email:
|
27
|
+
- yukinoraru@gmail.com
|
28
|
+
executables:
|
29
|
+
- geojp
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rspec
|
35
|
+
- Gemfile
|
36
|
+
- Guardfile
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- bin/geojp
|
41
|
+
- geocoder_jp.gemspec
|
42
|
+
- lib/geocoder_jp.rb
|
43
|
+
- lib/geocoder_jp/version.rb
|
44
|
+
- spec/geocoder_jp_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
homepage: https://github.com/yukinoraru/geocoder_jp
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.10
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Geocoding.jp API client for Ruby
|
70
|
+
test_files:
|
71
|
+
- spec/geocoder_jp_spec.rb
|
72
|
+
- spec/spec_helper.rb
|