simple_geocode 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "simple_cache"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "bundler"
12
+ gem "jeweler"
13
+
14
+ # vcr stuff: this needs psych or else will crash
15
+ gem "vcr"
16
+ gem "webmock"
17
+ gem "psych"
18
+
19
+ gem 'simplecov', :require => false
20
+ gem "ruby-debug19"
21
+ end
22
+
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.7)
5
+ archive-tar-minitar (0.5.2)
6
+ columnize (0.3.6)
7
+ crack (0.3.1)
8
+ git (1.2.5)
9
+ jeweler (1.8.3)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.6.6)
15
+ linecache19 (0.5.12)
16
+ ruby_core_source (>= 0.1.4)
17
+ multi_json (1.2.0)
18
+ psych (1.3.1)
19
+ rake (0.9.2.2)
20
+ rdoc (3.12)
21
+ json (~> 1.4)
22
+ ruby-debug-base19 (0.11.25)
23
+ columnize (>= 0.3.1)
24
+ linecache19 (>= 0.5.11)
25
+ ruby_core_source (>= 0.1.4)
26
+ ruby-debug19 (0.11.6)
27
+ columnize (>= 0.3.1)
28
+ linecache19 (>= 0.5.11)
29
+ ruby-debug-base19 (>= 0.11.19)
30
+ ruby_core_source (0.1.5)
31
+ archive-tar-minitar (>= 0.5.2)
32
+ simple_cache (0.8.1)
33
+ sqlite3
34
+ simplecov (0.6.1)
35
+ multi_json (~> 1.0)
36
+ simplecov-html (~> 0.5.3)
37
+ simplecov-html (0.5.3)
38
+ sqlite3 (1.3.5)
39
+ vcr (2.0.1)
40
+ webmock (1.8.6)
41
+ addressable (>= 2.2.7)
42
+ crack (>= 0.1.7)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler
49
+ jeweler
50
+ psych
51
+ ruby-debug19
52
+ simple_cache
53
+ simplecov
54
+ vcr
55
+ webmock
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 radiospiel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = app
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to app
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 radiospiel. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple_geocode"
18
+ gem.homepage = "http://github.com/radiospiel/simple_geocode"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Application base code}
21
+ gem.description = %Q{Some basics that your application could benefit from.}
22
+ gem.email = "eno@open-lab.org"
23
+ gem.authors = ["radiospiel"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "micro_sql #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
@@ -0,0 +1,62 @@
1
+ require "cgi"
2
+ require "json"
3
+ require "simple_cache"
4
+
5
+ # A basic, caching, geocoder
6
+ module SimpleGeocoder
7
+ extend self
8
+
9
+ def cache=(cache)
10
+ @cache = cache
11
+ end
12
+
13
+ def cache
14
+ @cache ||= SimpleCache.new("simple_geocoder")
15
+ end
16
+
17
+ # 30 days
18
+ TIME_TO_LIVE = 30 * 24 * 3600
19
+
20
+ def geocode(address)
21
+ cache.cached(address, TIME_TO_LIVE) {
22
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(address)}&output=json&oe=utf-8"
23
+
24
+ data = JSON.parse(get(url))
25
+
26
+ status = data["Status"]
27
+ if status["code"] != 200
28
+ STDERR.puts "Geocoding failed for '#{address}' with status #{status["code"]}"
29
+ return
30
+ end
31
+
32
+ data["Placemark"].first["Point"]["coordinates"]
33
+ }
34
+ end
35
+
36
+ private
37
+
38
+ def get(uri_str, limit = 10)
39
+ raise 'too many redirections' if limit == 0
40
+
41
+ uri = URI.parse(uri_str)
42
+
43
+ http = Net::HTTP.new(uri.host, uri.port)
44
+ if uri.scheme == "https"
45
+ http.use_ssl = true
46
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
47
+ end
48
+ request = Net::HTTP::Get.new(uri.request_uri)
49
+ response = http.request(request)
50
+
51
+ case response
52
+ when Net::HTTPSuccess then
53
+ response.body
54
+ when Net::HTTPRedirection then
55
+ location = response['location']
56
+ App.logger.debug "redirected to #{location}"
57
+ get(location, limit - 1)
58
+ else
59
+ response.value
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,57 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: http://maps.google.com/maps/geo?oe=utf-8&output=json&q=Berlin
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Content-Type:
20
+ - !binary |-
21
+ dGV4dC9qYXZhc2NyaXB0OyBjaGFyc2V0PVVURi04
22
+ !binary "VmFyeQ==":
23
+ - !binary |-
24
+ QWNjZXB0LUxhbmd1YWdl
25
+ !binary "RGF0ZQ==":
26
+ - !binary |-
27
+ V2VkLCAwNCBBcHIgMjAxMiAxNToxMTo0MyBHTVQ=
28
+ !binary "U2VydmVy":
29
+ - !binary |-
30
+ bWFmZQ==
31
+ Cache-Control:
32
+ - !binary |-
33
+ cHJpdmF0ZQ==
34
+ X-Xss-Protection:
35
+ - !binary |-
36
+ MTsgbW9kZT1ibG9jaw==
37
+ X-Frame-Options:
38
+ - !binary |-
39
+ U0FNRU9SSUdJTg==
40
+ Transfer-Encoding:
41
+ - !binary |-
42
+ Y2h1bmtlZA==
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ! "{\n \"name\": \"Berlin\",\n \"Status\": {\n \"code\": 200,\n
46
+ \ \"request\": \"geocode\"\n },\n \"Placemark\": [ {\n \"id\": \"p1\",\n
47
+ \ \"address\": \"Berlin, Germany\",\n \"AddressDetails\": {\n \"Accuracy\"
48
+ : 4,\n \"Country\" : {\n \"AdministrativeArea\" : {\n \"AdministrativeAreaName\"
49
+ : \"Berlin\",\n \"Locality\" : {\n \"LocalityName\" : \"Berlin\"\n
50
+ \ }\n },\n \"CountryName\" : \"Deutschland\",\n \"CountryNameCode\"
51
+ : \"DE\"\n }\n},\n \"ExtendedData\": {\n \"LatLonBox\": {\n \"north\":
52
+ 52.7409729,\n \"south\": 52.3064887,\n \"east\": 13.9185276,\n
53
+ \ \"west\": 12.8940524\n }\n },\n \"Point\": {\n \"coordinates\":
54
+ [ 13.4062900, 52.5242680, 0 ]\n }\n } ]\n}\n"
55
+ http_version:
56
+ recorded_at: Wed, 04 Apr 2012 15:11:44 GMT
57
+ recorded_with: VCR 2.0.1
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative './test_helper'
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = "#{File.dirname(__FILE__)}/fixtures-vcr"
7
+ c.hook_into :webmock
8
+ end
9
+
10
+ class GeocoderTest < Test::Unit::TestCase
11
+ def test_geocoder
12
+ expected = [13.40629, 52.524268, 0]
13
+
14
+ SimpleGeocoder.cache.clear
15
+
16
+ VCR.use_cassette('geocoder') do
17
+ assert_equal(expected, SimpleGeocoder.geocode("Berlin"))
18
+ end
19
+
20
+ assert_equal(expected, SimpleGeocoder.geocode("Berlin"))
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'ruby-debug'
12
+ require 'simplecov'
13
+ require 'vcr'
14
+ require 'test/unit'
15
+ SimpleCov.start do
16
+ add_filter "test/*"
17
+ end
18
+
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
21
+
22
+ require 'simple_geocode'
23
+
24
+ class Test::Unit::TestCase
25
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_geocode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simple_cache
16
+ requirement: &70111395165180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70111395165180
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70111395164440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70111395164440
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70111395163840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70111395163840
47
+ - !ruby/object:Gem::Dependency
48
+ name: vcr
49
+ requirement: &70111395163140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70111395163140
58
+ - !ruby/object:Gem::Dependency
59
+ name: webmock
60
+ requirement: &70111395162560 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70111395162560
69
+ - !ruby/object:Gem::Dependency
70
+ name: psych
71
+ requirement: &70111395161700 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70111395161700
80
+ - !ruby/object:Gem::Dependency
81
+ name: simplecov
82
+ requirement: &70111395160580 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70111395160580
91
+ - !ruby/object:Gem::Dependency
92
+ name: ruby-debug19
93
+ requirement: &70111395159320 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70111395159320
102
+ description: Some basics that your application could benefit from.
103
+ email: eno@open-lab.org
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files:
107
+ - LICENSE.txt
108
+ - README.rdoc
109
+ files:
110
+ - .document
111
+ - Gemfile
112
+ - Gemfile.lock
113
+ - LICENSE.txt
114
+ - README.rdoc
115
+ - Rakefile
116
+ - VERSION
117
+ - lib/simple_geocode.rb
118
+ - test/fixtures-vcr/geocoder.yml
119
+ - test/geocoder_test.rb
120
+ - test/test_helper.rb
121
+ homepage: http://github.com/radiospiel/simple_geocode
122
+ licenses:
123
+ - MIT
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ segments:
135
+ - 0
136
+ hash: 473508081662866573
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.17
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: Application base code
149
+ test_files: []