geocoding_lite 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.
@@ -0,0 +1,15 @@
1
+ class ::String
2
+ def geocoding_lookup
3
+ GeocodingLite.lookup(self)
4
+ end
5
+ end
6
+
7
+ class ::Array
8
+ def geocoding_lookup
9
+ raise RuntimeError, 'Wrong array size (expected 2 elements)' unless size == 2
10
+ raise RuntimeError, 'Wrong coordinates (Float expected)' unless
11
+ self[0].kind_of?(Numeric) && self[1].kind_of?(Numeric)
12
+
13
+ GeocodingLite.lookup(self)
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module GeocodingLite
2
+ class Geocode
3
+ def initialize(service = nil)
4
+ @service = service || GeocodingService.new
5
+ end
6
+
7
+ def lookup(location)
8
+ @service.lookup(location)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require 'net/http'
2
+ require 'rubygems'
3
+ require 'json'
4
+
5
+ module GeocodingLite
6
+ class GeocodingService
7
+ SERVICE_URL = %q(http://maps.googleapis.com/maps/api/geocode/json)
8
+
9
+ def initialize(http_object = nil, uri_object = nil, service_url = nil, json_parser = nil)
10
+ @http_object = http_object || Net::HTTP
11
+ @uri_object = uri_object || URI
12
+ @json_parser = json_parser || JSON
13
+ @service_url = service_url || SERVICE_URL
14
+ end
15
+
16
+ def lookup(location)
17
+ load_response(send_request(location))
18
+ end
19
+
20
+ private
21
+
22
+ def load_response(raw_response)
23
+ json = @json_parser.parse(raw_response)
24
+
25
+ status = json['status']
26
+ raise RuntimeError, "API returned #{status}" if
27
+ status != 'OK' && status != 'ZERO_RESULTS'
28
+
29
+ json['results'].reduce([]) do |output, result|
30
+ output << {
31
+ :address => result['formatted_address'],
32
+ :lat => result['geometry']['location']['lat'],
33
+ :lng => result['geometry']['location']['lng'],
34
+ :types => result['types']
35
+ }
36
+ end
37
+ end
38
+
39
+ def prepare_params(location)
40
+ url = "#{@service_url}?"
41
+
42
+ url << "address=#{@uri_object.encode(location)}" if
43
+ location.kind_of? String
44
+
45
+ url << "latlng=#{location.join(',')}" if
46
+ location.kind_of? Array
47
+
48
+ url << '&sensor=false'
49
+ end
50
+
51
+ def send_request(location)
52
+ uri = @uri_object.parse(prepare_params(location))
53
+
54
+ @http_object.get_response(uri).body
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module GeocodingLite
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'geocoding_lite/geocode'
2
+ require 'geocoding_lite/geocoding_service'
3
+ require 'geocoding_lite/core_extensions'
4
+ require 'geocoding_lite/version'
5
+
6
+ module GeocodingLite
7
+ def self.lookup(location)
8
+ @geocode ||= Geocode.new
9
+ @geocode.lookup(location)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geocoding_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vladimir Ivic
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: The simplest geocoding lookup tool - ever!
15
+ email: vladimir.ivic@icloud.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/geocoding_lite.rb
21
+ - lib/geocoding_lite/geocode.rb
22
+ - lib/geocoding_lite/core_extensions.rb
23
+ - lib/geocoding_lite/geocoding_service.rb
24
+ - lib/geocoding_lite/version.rb
25
+ homepage: https://github.com/mancmelou/geocoding_lite
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.25
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Geocoding Lite
50
+ test_files: []