google-geocoder 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,10 @@
1
+
2
+ = Google-Geocode Changelog
3
+
4
+ == Version 0.0.1
5
+
6
+ * Initial release.
7
+
8
+ == Version 0.0.2
9
+
10
+ * Small bug fixes.
data/README ADDED
@@ -0,0 +1,11 @@
1
+ = Google-Translate -- Client for Google Translate API
2
+
3
+ = Installing Google-Translate
4
+
5
+ == Installing the Gem
6
+ $ gem install google-geocoder
7
+
8
+ == Usage
9
+
10
+ geo russia moscow - returns geocode information for the provided location
11
+
data/Rakefile ADDED
@@ -0,0 +1,89 @@
1
+ # Rakefile for google-geocoder
2
+
3
+ require 'rubygems'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/testtask'
6
+ require 'spec/rake/spectask'
7
+ require 'rake/rdoctask'
8
+ require 'rcov/rcovtask'
9
+
10
+ task :default => :gemspec
11
+
12
+ begin
13
+ require 'bundler'
14
+
15
+ begin
16
+ require 'jeweler'
17
+
18
+ Jeweler::Tasks.new do |gemspec|
19
+ gemspec.name = "google-geocoder"
20
+ gemspec.summary = "Simple client for Google Geocode API (Summary)."
21
+ gemspec.description = "Simple client for Google Geocode API."
22
+ gemspec.email = "alexander.shvets@gmail.com"
23
+ gemspec.homepage = "http://github.com/shvets/google-geocoder"
24
+ gemspec.authors = ["Alexander Shvets"]
25
+ gemspec.files = FileList["CHANGES", "google-geocoder.gemspec", "Rakefile", "README", "VERSION",
26
+ "lib/**/*", "bin/**/*"]
27
+ gemspec.add_dependency "json_pure", ">= 1.1.4"
28
+
29
+ gemspec.executables = ['geo', 'g']
30
+ gemspec.requirements = ["none"]
31
+ gemspec.bindir = "bin"
32
+
33
+ gemspec.add_bundler_dependencies
34
+ end
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it s with: [sudo] gem install jeweler"
37
+ end
38
+ rescue LoadError
39
+ puts "Bundler not available. Install it s with: [sudo] gem install bundler"
40
+ end
41
+
42
+ desc "Release the gem"
43
+ task :"release:gem" do
44
+ %x(
45
+ rake gemspec
46
+ rake build
47
+ rake install
48
+ git add .
49
+ )
50
+ puts "Commit message:"
51
+ message = STDIN.gets
52
+
53
+ version = "#{File.open(File::dirname(__FILE__) + "/VERSION").readlines().first}"
54
+
55
+ %x(
56
+ git commit -m "#{message}"
57
+
58
+ git push origin master
59
+
60
+ gem push pkg/google-geocoder-#{version}.gem
61
+ )
62
+ end
63
+
64
+ desc "Run gem code locally"
65
+ task :"run:gem" do
66
+ command = "bin/geo " + (ENV['params'].nil? ? '' : ENV['params'])
67
+ puts ruby("#{command}")
68
+ end
69
+
70
+ Spec::Rake::SpecTask.new do |task|
71
+ task.libs << 'lib'
72
+ task.pattern = 'spec/**/*_spec.rb'
73
+ task.verbose = false
74
+ end
75
+
76
+ Rake::RDocTask.new do |rdoc|
77
+ rdoc.rdoc_dir = 'rdoc'
78
+ rdoc.title = 'teststuff'
79
+ rdoc.options << '--line-numbers' << '--inline-source'
80
+ rdoc.rdoc_files.include('README*')
81
+ rdoc.rdoc_files.include('lib/**/*.rb')
82
+ end
83
+
84
+ Rcov::RcovTask.new do |task|
85
+ task.libs << 'test'
86
+ task.test_files = FileList['test/**/*_test.rb']
87
+ task.verbose = true
88
+ end
89
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/bin/g ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
4
+
5
+ load File.dirname(__FILE__) + '/geo'
6
+
data/bin/g.bat ADDED
@@ -0,0 +1 @@
1
+ @CALL geo.bat
data/bin/geo ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File::join(File::dirname(File::dirname(__FILE__)), "lib"))
4
+
5
+ require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
6
+ require 'google_geocoder'
7
+
8
+ #$KCODE='u'
9
+
10
+ class Geo
11
+
12
+ USAGE= <<-TEXT
13
+ Usage:
14
+ geo russia moscow - returns geocode information for the provided location
15
+ TEXT
16
+
17
+ def initialize
18
+ @geocoder = Google::Geocoder.new
19
+ end
20
+
21
+ def run
22
+ if(ARGV.size == 0)
23
+ puts USAGE and return
24
+ end
25
+
26
+ query = ARGV.join(' ')
27
+
28
+ case query
29
+
30
+ when /(-v)|(--version)/ then
31
+ puts "Version: #{File.open(File::dirname(__FILE__) + "/../VERSION").readlines().first}"
32
+ when /(.*)/ then
33
+ begin
34
+ @geocoder.query(query)
35
+ rescue Exception => e
36
+ puts "Error: " + e.message
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ Geo.new.run
data/bin/geo.bat ADDED
@@ -0,0 +1,6 @@
1
+ @ECHO OFF
2
+ IF NOT "%~f0" == "~f0" GOTO :WinNT
3
+ @"ruby.exe" "C:/Ruby/ruby-1.9.1/bin/geo" %1 %2 %3 %4 %5 %6 %7 %8 %9
4
+ GOTO :EOF
5
+ :WinNT
6
+ @"ruby.exe" "%~dpn0" %*
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google-geocoder}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alexander Shvets"]
12
+ s.date = %q{2010-09-29}
13
+ s.description = %q{Simple client for Google Geocode API.}
14
+ s.email = %q{alexander.shvets@gmail.com}
15
+ s.executables = ["geo", "g"]
16
+ s.extra_rdoc_files = [
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ "CHANGES",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "bin/g",
25
+ "bin/g.bat",
26
+ "bin/geo",
27
+ "bin/geo.bat",
28
+ "google-geocoder.gemspec",
29
+ "lib/google_geocoder.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/shvets/google-geocoder}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.requirements = ["none"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Simple client for Google Geocode API (Summary).}
37
+ s.test_files = [
38
+ "spec/geocoder_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<json_pure>, [">= 1.1.4"])
48
+ s.add_runtime_dependency(%q<json_pure>, [">= 0"])
49
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
50
+ s.add_development_dependency(%q<gemcutter>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<json_pure>, [">= 1.1.4"])
53
+ s.add_dependency(%q<json_pure>, [">= 0"])
54
+ s.add_dependency(%q<jeweler>, [">= 0"])
55
+ s.add_dependency(%q<gemcutter>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<json_pure>, [">= 1.1.4"])
59
+ s.add_dependency(%q<json_pure>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, [">= 0"])
61
+ s.add_dependency(%q<gemcutter>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,104 @@
1
+ # google_geocoder.rb
2
+
3
+ require 'open-uri'
4
+ require 'cgi'
5
+ require 'json'
6
+
7
+ # Example:
8
+ # {
9
+ # "results"=> [
10
+ # {"address_components"=>
11
+ # [{"long_name"=>"Paris", "types"=>["locality", "political"], "short_name"=>"Paris"},
12
+ # {"long_name"=>"Paris", "types"=>["administrative_area_level_2", "political" ], "short_name"=>"75"},
13
+ # {"long_name"=>"Ile-de-France", "types"=>["administrative_area_level_1", "political"], "short_name"=>"IDF"},
14
+ # {"long_name"=>"France", "types"=>["country", "political"], "short_name"=>"FR"}
15
+ # ],
16
+ # "geometry"=>{
17
+ # "bounds"=>{"northeast"=>{"lng"=>2.4699099, "lat"=>48.9021461}, "southwest"=>{"lng"=>2.2241006, "lat"=>48.8155414}},
18
+ # "location"=>{"lng"=>2.3509871, "lat"=>48.8566667},
19
+ # "location_type"=>"APPROXIMATE",
20
+ # "viewport"=>{"northeast"=>{"lng"=>2.4790465, "lat"=>48.915363}, "southwest"=>{"lng"=>2.2229277, "lat"=>48.7979015}}
21
+ # },
22
+ # "types"=>["locality", "political"],
23
+ # "formatted_address"=>"Paris, France"}
24
+ # ],
25
+ # "status"=>"OK"
26
+ # }
27
+
28
+ module Google
29
+ class Geocoder
30
+ def self.Exception(*names)
31
+ cl = Module === self ? self : Object
32
+ names.each {|n| cl.const_set(n, Class.new(Exception))}
33
+ end
34
+
35
+ Exception :MissingLocation, :GeocoderServerIsDown, :InvalidResponse
36
+
37
+ URL_STRING = "http://maps.googleapis.com/maps/api/geocode"
38
+
39
+ def query(location)
40
+ raise(MissingLocation) if location.nil?
41
+
42
+ request = "#{URL_STRING}/json?address=#{CGI.escape(location)}&sensor=false"
43
+
44
+ begin
45
+ response = call_service(request)
46
+
47
+ raise(GeocoderServerIsDown) if response.empty?
48
+
49
+ raise(InvalidResponse, response['results']) if response['status'] != "OK" # success
50
+
51
+ root = response["results"][0]
52
+ address_components = root["address_components"]
53
+ geometry = root["geometry"]
54
+
55
+ country = address_component(address_components, "country")
56
+ locality = address_component(address_components, "locality")
57
+ administrative_area_l1 = address_component(address_components, "administrative_area_level_1")
58
+ administrative_area_l2 = address_component(address_components, "administrative_area_level_2")
59
+
60
+ latitude = geometry["location"]["lat"]
61
+ longitude = geometry["location"]["lng"]
62
+
63
+ puts "Country: #{country['long_name']} (#{country['short_name']})"
64
+ puts "Locality: #{locality['long_name']} (#{locality['short_name']})"
65
+ puts "Administrative Area (level 1): #{administrative_area_l1['long_name']} (#{administrative_area_l1['short_name']})"
66
+ puts "Administrative Area (level 2): #{administrative_area_l2['long_name']} (#{administrative_area_l2['short_name']})" unless administrative_area_l2.nil?
67
+
68
+ puts "Latitude: #{latitude}"
69
+ puts "Longitude: #{longitude}"
70
+
71
+ [latitude, longitude]
72
+ rescue OpenURI::HTTPError
73
+ raise(GeocoderServerIsDown)
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def call_service(request)
80
+ response = {}
81
+
82
+ open(request) do |stream|
83
+ content = stream.read
84
+
85
+ unless content.nil?
86
+ response = JSON.parse(content)
87
+ end
88
+ end
89
+
90
+ response
91
+ end
92
+
93
+ def address_component(parent, type)
94
+ parent.each do |child|
95
+ if child["types"].include? type
96
+ return child
97
+ end
98
+ end
99
+
100
+ nil
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,29 @@
1
+ # geocoder_spec.rb
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+
5
+ require 'google_geocoder'
6
+
7
+ module Google
8
+
9
+ describe Geocoder do
10
+ before :each do
11
+ @geocoder = Geocoder.new
12
+ end
13
+
14
+ it "should raise an error if parameter is missing" do
15
+ lambda {
16
+ @geocoder.query(nil)
17
+ }.should raise_error
18
+ end
19
+
20
+ it "should return geocode coordinates" do
21
+ q = @geocoder.query("moscow russia")
22
+
23
+ puts q
24
+
25
+ q.size.should be == 2
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,11 @@
1
+ # spec_helper.rb
2
+
3
+ require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
4
+ require 'spec'
5
+
6
+ # add lib directory
7
+ $:.unshift File.dirname(__FILE__) + '/../lib'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :mocha
11
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-geocoder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Alexander Shvets
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-29 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: json_pure
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 27
31
+ segments:
32
+ - 1
33
+ - 1
34
+ - 4
35
+ version: 1.1.4
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ prerelease: false
40
+ name: json_pure
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ prerelease: false
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirement: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ type: :development
67
+ prerelease: false
68
+ name: gemcutter
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirement: *id004
79
+ description: Simple client for Google Geocode API.
80
+ email: alexander.shvets@gmail.com
81
+ executables:
82
+ - geo
83
+ - g
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - README
88
+ files:
89
+ - CHANGES
90
+ - README
91
+ - Rakefile
92
+ - VERSION
93
+ - bin/g
94
+ - bin/g.bat
95
+ - bin/geo
96
+ - bin/geo.bat
97
+ - google-geocoder.gemspec
98
+ - lib/google_geocoder.rb
99
+ - spec/geocoder_spec.rb
100
+ - spec/spec_helper.rb
101
+ has_rdoc: true
102
+ homepage: http://github.com/shvets/google-geocoder
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options:
107
+ - --charset=UTF-8
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements:
129
+ - none
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.7
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Simple client for Google Geocode API (Summary).
135
+ test_files:
136
+ - spec/geocoder_spec.rb
137
+ - spec/spec_helper.rb