mapcoder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mapcoder.rb +58 -0
  3. metadata +61 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84a797e258a7e508e8a635db87b57406b1d3c73f
4
+ data.tar.gz: bcd0d83139fe66db5b119f90864686e45195ad6b
5
+ SHA512:
6
+ metadata.gz: acae4fcaac0bd319b1a82371a539d8637c292d0f314dd4e88cbebb71bb36806b01d7d4b50c172b010907d48305c9d31c05c80aaab746be899506de5d7e9f973e
7
+ data.tar.gz: 59b11b4e2c59a1ee9c38acd526ae4aa5524e7b4fa6d429497f9d517026dda74b99ce730e5a7390b601d5504254474de05c86a16b948d7f2d0317a3635b2ab0bf
@@ -0,0 +1,58 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+
4
+ module MapCoder
5
+
6
+ private
7
+
8
+ class EmptyAddressError < RuntimeError; end
9
+ class UnknownGoogleAPIResponseError < RuntimeError; end
10
+
11
+ REQUEST_URL = 'http://maps.google.com/maps/api/geocode/json'
12
+ GOOGLE_API_SUCCESSFUL_RESPONSE = 'OK'
13
+ GOOGLE_API_NOT_FOUND_RESPONSE = 'ZERO_RESULTS'
14
+ GOOGLE_API_OVER_QUERY_LIMIT_RESPONSE = 'OVER_QUERY_LIMIT'
15
+
16
+ public
17
+
18
+ def self.coordinates(address)
19
+ raise EmptyAddressError if address.nil? || address.empty?
20
+ escaped_address = CGI.escape(address)
21
+ url = "#{REQUEST_URL}?address=#{escaped_address}&sensor=false"
22
+ attempt_number = 0
23
+
24
+ loop do
25
+ attempt_number += 1
26
+
27
+ begin
28
+ raw_response = RestClient.get(url)
29
+ rescue
30
+ return { status: :api_unavailable }
31
+ end
32
+
33
+ response = JSON.parse(raw_response)
34
+ status = response['status']
35
+
36
+ if status == GOOGLE_API_SUCCESSFUL_RESPONSE
37
+ coordinates = response['results'].map { |result| result['geometry']['location'] }
38
+ coordinates.map! { |result| { latitude: result['lat'], longitude: result['lng'] } }
39
+ return { status: :ok, coordinates: coordinates }
40
+
41
+ elsif status == GOOGLE_API_NOT_FOUND_RESPONSE
42
+ return { status: :ok, coordinates: [] }
43
+
44
+ elsif status == GOOGLE_API_OVER_QUERY_LIMIT_RESPONSE
45
+ if attempt_number == 1
46
+ # Failed for the first time, sleep for 2 seconds and retry
47
+ sleep 2
48
+ else
49
+ # Failed again, we are limited for the day
50
+ return { status: :over_daily_limit }
51
+ end
52
+
53
+ else
54
+ raise UnknownGoogleAPIResponseError
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mapcoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Gubitskiy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Simple geocoding gem that queries Google Maps API for coordinates given
28
+ an address. Knows about API limits, can sleep to get through too-many-requests-per-second
29
+ errors, and will let you know when you've reached your daily query limit
30
+ email: d.gubitskiy@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/mapcoder.rb
36
+ homepage: https://github.com/enthrops/mapcoder
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.1
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Simplistic geocoding using Google Maps API
60
+ test_files: []
61
+ has_rdoc: