geogle 0.2.5 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 353a109106208ca895390a03fa8fe2caaaefeacb
4
- data.tar.gz: a2fa5961b9c53d53a598327a5eb5f76b7f7e246f
3
+ metadata.gz: fc312835c798185f9b4ecc05e853c727e25f0597
4
+ data.tar.gz: 0bde3ef341b88d5667ed7166a9f252cae58857d7
5
5
  SHA512:
6
- metadata.gz: aaeda50b03cc403e5dc157f75a04d73ecdf79301a7818de014890e97f12d3e248f910d67d1590a168bf08b998ee91dfe5c2f3115bb81c7cdf2e36f2c532dc58c
7
- data.tar.gz: 87c18ca1df52e7a1eeece15bcb3d94b4b41d531442b8ff34b4606a2a117965794f53e12db1dd17e5cb8335a50749e94ceeefce8abca7282db493dd43460baeae
6
+ metadata.gz: bae6ad2a7a4bb5b2f7f82b413a9af61ffd5585e4de15acad2e0dc22406e2b6d3460002e3b5cfaab4b17e22ece3787821d4bf4c938bf971016e34de5dc7aae173
7
+ data.tar.gz: f78db15efbafcd71331f36617a3e0d1a2876f71a6bb542b5ae6b737ccd99991737553d115881526cee3959ef8c64f8dcf01ebd5618a33fd9c63eb99d29f39375
data/.gitignore CHANGED
@@ -16,4 +16,6 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  spec/integration/business_geocoder_spec.rb
19
- spec/fixtures/cassettes/business_geocode_by_address.yml
19
+ spec/integration/business_directions_spec.rb
20
+ spec/fixtures/cassettes/business_geocode_by_address.yml
21
+ spec/fixtures/cassettes/business_directions.yml
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  # Geogle
2
2
 
3
- Still in development!
4
-
5
- [![wercker status](https://app.wercker.com/status/6122e4f0966ee255949e2eb465aefd10/m "wercker status")](https://app.wercker.com/project/bykey/6122e4f0966ee255949e2eb465aefd10)
6
-
7
3
  Ruby wrapper for the Geocoding and Directions services provided from the Google Maps API.
8
4
 
5
+ [![wercker status](https://app.wercker.com/status/6122e4f0966ee255949e2eb465aefd10/m "wercker status")](https://app.wercker.com/project/bykey/6122e4f0966ee255949e2eb465aefd10)
6
+ [![Gem Version](https://badge.fury.io/rb/geogle.svg)](http://badge.fury.io/rb/geogle)
9
7
 
10
8
  ## Installation
11
9
 
@@ -23,46 +21,45 @@ Or install it yourself as:
23
21
 
24
22
  ## Usage
25
23
 
26
- ### Geocoding
24
+ ## Geocoding
27
25
  Google geocoding documentation:
28
26
  https://developers.google.com/maps/documentation/geocoding/
29
27
 
30
- When creating the Geogle::Geocoder instance you can pass:
28
+ ### Setting parameters
29
+
30
+ When creating the Geogle::Geocoder these are the setting parameters:
31
31
 
32
- * **sensor**: true or false (false by default)
33
- * **language**: ("en" by default).
32
+ * **sensor**:
33
+ * **true**
34
+ * **false** (default)
35
+ * **language**: ar, eu, bg, bn, ca, cs, da, de, el, en, en-AU, en-GB, es, eu, fa, fi, fil, fr, gl, gu, hi, hr, hu, id, it, iw, ja, kn, ko, lt, lv, ml, mr, nl, no, pl, pt, pt-BR, pt-PT, ro, ru, sk, sl, sr, sv, tl, ta, te, th, tr, uk, vi, zh-CN, zh-TW
36
+ * **raw**:
37
+ * **true**: returns the raw json that comes in the body from the response.
38
+ * **false** (default): returns the object created with auxiliar funtions.
34
39
 
35
- In case you wanna use Google Maps API for Business, you'll need to pass the following attributes in order to sign the URL.
36
- * **client_id**: ID of the client. It starts with "gme-" prefix.
37
- * **crypto_key**: Criptographic key.
40
+ * **client_id** (required for business API): ID of the client. It starts with "gme-" prefix.
41
+ * **crypto_key** (required for business API): Criptographic key.
38
42
 
39
43
  Here's more information about Google Maps API for Business:
40
44
  https://developers.google.com/maps/documentation/business/webservices
41
45
 
42
- There are two methods that can be called:
43
- * **address**: Geocoding by name of the location.
44
- * **latlng**: Reverse geocode.
46
+ #### Data model
45
47
 
46
48
  Both methods return an array of Geogle::Model::Place. Each place is composed by:
47
- * **geometry**:
48
- * **location**:
49
- * **lat**: Float
50
- * **lng**: Float
49
+
50
+ * **Coordinates**:
51
+ * **lat**: Float
52
+ * **lng**: Float
53
+
54
+ * **Area**:
55
+ * **northeast**: Coordinates
56
+ * **southwest**: Coordinates
57
+
58
+ * **Geometry**:
59
+ * **location**: Coordinates
51
60
  * **location_type**: String
52
- * **bounds**:
53
- * **northeast**:
54
- * **lat**: Float
55
- * **lng**: Float
56
- * **southwest**:
57
- * **lat**: Float
58
- * **lng**: Float
59
- * **viewport**:
60
- * **northeast**:
61
- * **lat**: Float
62
- * **lng**: Float
63
- * **southwest**:
64
- * **lat**: Float
65
- * **lng**: Float
61
+ * **bounds**: Area
62
+ * **viewport**: Area
66
63
  * **address**:
67
64
  * **street_number**: String
68
65
  * **street**: String
@@ -75,33 +72,92 @@ Both methods return an array of Geogle::Model::Place. Each place is composed by:
75
72
  * **country_code**: String
76
73
  * **formatted**: String
77
74
 
75
+
78
76
  ### By address without an account
79
77
 
80
- $ client = Geogle::Geocoder.new({ sensor: false, language: "es" })
81
- $ client.address("Blasco Ibañez, Valencia")
78
+ ```ruby
79
+ client = Geogle::Geocoder.new({ sensor: false, language: "es" })
80
+ client.address("Blasco Ibañez, Valencia")
81
+ ```
82
82
 
83
83
  ### By address making use of the components
84
84
 
85
- $ client = Geogle::Geocoder.new({ sensor: false, language: "es" })
86
- $ components = { country: 'ES' }
87
- $ client.address("Blasco Ibañez, Valencia", components)
85
+ ```ruby
86
+ client = Geogle::Geocoder.new({ sensor: false, language: "es" })
87
+ components = { country: 'ES' }
88
+ client.address("Blasco Ibañez, Valencia", components)
89
+ ```
88
90
 
89
91
  Available components to be used can be found here:
90
92
  https://developers.google.com/maps/documentation/geocoding/#ComponentFiltering
91
93
 
92
94
  ### Reverse geocoding (by latitude and longitude)
93
95
 
94
- $ Geogle::Geocoder.new.latlng(39.5073225, -0.2914778)
96
+ ```ruby
97
+ Geogle::Geocoder.new.latlng(39.5073225, -0.2914778)
98
+ ```
95
99
 
96
100
  ### Using a business account
97
101
 
98
- $ client = Geogle::Geocoder.new({ client_id: "gme-client-id", crypto_key: "crypto-key" })
99
- $ client.latlng(39.5073225, -0.2914778)
102
+ ```ruby
103
+ client = Geogle::Geocoder.new({ client_id: "gme-client-id", crypto_key: "crypto-key" })
104
+ client.latlng(39.5073225, -0.2914778)
105
+ ```
106
+
107
+ The signature required to do the request will be appended in the URL.
108
+
109
+ ## Directions
110
+
111
+ ### Setting parameters
112
+
113
+ The same as with **Geocode**.
114
+
115
+ #### Data model
116
+
117
+ Return an array of Geogle::Model::Route. Each route is composed by:
118
+
119
+ * **Time**:
120
+ * **value**: Integer
121
+ * **text**: String
122
+ * **time_zone**: String
123
+
124
+ * **TextValue**:
125
+ * **value**: Integer
126
+ * **text**: String
127
+
128
+ * **Leg**:
129
+ * **steps**: Step
130
+ * **distance**: TextValue
131
+ * **duration**: TextValue
132
+ * **arrival_time**: Time
133
+ * **departure_time**: Time
134
+ * **start_address**: String
135
+ * **end_address**: String
136
+ * **start_location**: Coordinates
137
+ * **end_location**: Coordinates
138
+
139
+ * **Route**:
140
+ * **summary**: String
141
+ * **legs**: Array[Leg]
142
+ * **waypoint_order**: Array[Integer]
143
+ * **bounds**: Area
144
+ * **copyrights**: String
145
+ * **warnings**: Array[String]
146
+
147
+
148
+ ### Search using address names for origin and destination
149
+
150
+ ```ruby
151
+ client = Geogle::Directions.new
152
+ client.routes("Berlin", "Munich", { region: "de" })
153
+ ```
100
154
 
101
- The signature required to do the request will be included in the URL.
155
+ ### Search using geo-locations for origin and destination
102
156
 
103
- ### Directions
104
- Directions service is still in development
157
+ ```ruby
158
+ client = Geogle::Directions.new
159
+ client.routes("39.4699889,-0.3759178", "40.4167158,-3.7037799")
160
+ ```
105
161
 
106
162
 
107
163
  ## Contributing
data/lib/geogle.rb CHANGED
@@ -5,6 +5,7 @@ module Geogle
5
5
  require "geogle/model"
6
6
  require "geogle/base"
7
7
  require "geogle/geocoder"
8
+ require "geogle/directions"
8
9
  require "geogle/error"
9
10
  require "geogle/error_handler"
10
11
  require "geogle/parametizer"
data/lib/geogle/base.rb CHANGED
@@ -5,15 +5,16 @@ require "net/http"
5
5
 
6
6
  module Geogle
7
7
  class Base
8
- def initialize(args = {})
9
- @args = args
10
- @parametizer = Parametizer.new(args)
8
+ def initialize(settings = {})
9
+ @settings = settings
10
+ @parametizer = Parametizer.new(settings)
11
+ @raw = settings[:raw] || false
11
12
  end
12
13
 
13
14
  protected
14
15
 
15
16
  def request(url, params)
16
- uri = UrlBuilder.new(url, @args).build(params)
17
+ uri = UrlBuilder.new(url, @settings).build(params)
17
18
  response = Net::HTTP.get_response(uri)
18
19
  raise InvalidKeyError if response.code == "403"
19
20
  body = JSON.parse(response.body)
@@ -2,6 +2,11 @@
2
2
 
3
3
  module Geogle
4
4
  class Directions < Base
5
-
5
+ def routes(origin, destination, options = {})
6
+ params = @parametizer.directions(origin, destination, options)
7
+ body = request(URL::DIRECTIONS, params)
8
+ return body["routes"] if @raw
9
+ Parser.routes(body["routes"])
10
+ end
6
11
  end
7
12
  end
@@ -14,6 +14,7 @@ module Geogle
14
14
 
15
15
  def executor(method, arg1, arg2)
16
16
  body = request(URL::GEOCODE, @parametizer.send(method, arg1, arg2))
17
+ return body["results"] if @raw
17
18
  Parser.places(body["results"])
18
19
  end
19
20
  end
data/lib/geogle/model.rb CHANGED
@@ -2,10 +2,16 @@
2
2
 
3
3
  module Geogle
4
4
  module Model
5
+ require "geogle/model/place"
5
6
  require "geogle/model/address"
6
7
  require "geogle/model/area"
7
8
  require "geogle/model/coordinates"
8
9
  require "geogle/model/geometry"
9
- require "geogle/model/place"
10
+
11
+ require "geogle/model/route"
12
+ require "geogle/model/leg"
13
+ require "geogle/model/step"
14
+ require "geogle/model/time"
15
+ require "geogle/model/text_value"
10
16
  end
11
17
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'virtus'
4
- require_relative 'coordinates'
4
+ require 'geogle/model/coordinates'
5
5
 
6
6
  module Geogle
7
7
  module Model
@@ -1,8 +1,8 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'virtus'
4
- require_relative 'area'
5
- require_relative 'coordinates'
4
+ require 'geogle/model/area'
5
+ require 'geogle/model/coordinates'
6
6
 
7
7
  module Geogle
8
8
  module Model
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'virtus'
4
+ require 'geogle/model/step'
5
+ require 'geogle/model/time'
6
+ require 'geogle/model/text_value'
7
+ require 'geogle/model/coordinates'
8
+
9
+ module Geogle
10
+ module Model
11
+
12
+ class Leg
13
+ include Virtus.model
14
+
15
+ attribute :steps, Array[Step]
16
+ attribute :distance, TextValue
17
+ attribute :duration, TextValue
18
+ attribute :arrival_time, Time
19
+ attribute :departure_time, Time
20
+ attribute :start_address, String
21
+ attribute :end_address, String
22
+ attribute :start_location, Coordinates
23
+ attribute :end_location, Coordinates
24
+ end
25
+ end
26
+ end
@@ -1,8 +1,8 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require 'virtus'
4
- require_relative 'address'
5
- require_relative 'geometry'
4
+ require 'geogle/model/address'
5
+ require 'geogle/model/geometry'
6
6
 
7
7
  module Geogle
8
8
  module Model
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'virtus'
4
+ require 'geogle/model/leg'
5
+ require 'geogle/model/area'
6
+
7
+ module Geogle
8
+ module Model
9
+
10
+ class Route
11
+ include Virtus.model
12
+
13
+ attribute :summary, String
14
+ attribute :legs, Array[Leg]
15
+ attribute :waypoint_order, Array[Integer]
16
+ attribute :overview_polyline, String
17
+ attribute :bounds, Area
18
+ attribute :copyrights, String
19
+ attribute :warnings, Array[String]
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'virtus'
4
+ require 'geogle/model/coordinates'
5
+ require 'geogle/model/text_value'
6
+
7
+ module Geogle
8
+ module Model
9
+
10
+ class Step
11
+ include Virtus.model
12
+
13
+ attribute :html_instructions, String
14
+ attribute :distance, TextValue
15
+ attribute :duration, TextValue
16
+ attribute :start_location, Coordinates
17
+ attribute :end_location, Coordinates
18
+ attribute :travel_mode, String
19
+ # Only travel_mode is transit
20
+ # attribute :sub_steps, String
21
+ # attribute :transit_details, String
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'virtus'
4
+
5
+ module Geogle
6
+ module Model
7
+
8
+ class TextValue
9
+ include Virtus.model
10
+
11
+ attribute :text, String
12
+ attribute :value, Integer
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'virtus'
4
+
5
+ module Geogle
6
+ module Model
7
+
8
+ class Time
9
+ include Virtus.model
10
+
11
+ attribute :text, String
12
+ attribute :value, Integer
13
+ attribute :time_zone, String
14
+ end
15
+ end
16
+ end
@@ -8,20 +8,46 @@ module Geogle
8
8
  end
9
9
 
10
10
  def address(address, components = {})
11
- options = { }
12
- options[:address] = address
13
- options[:language] = @language
14
- options[:components] = components(components) unless components.empty?
15
- options[:sensor] = @sensor
16
- options
11
+ options = {
12
+ address: address,
13
+ language: @language,
14
+ components: components(components),
15
+ sensor: @sensor
16
+ }
17
+ compact_hash(options)
17
18
  end
18
19
 
19
20
  def latlng(lat, lng)
20
- { latlng: "#{lat},#{lng}", language: @language, sensor: @sensor }
21
+ {
22
+ latlng: "#{lat},#{lng}",
23
+ language: @language,
24
+ sensor: @sensor
25
+ }
26
+ end
27
+
28
+ def directions(origin, destination, params)
29
+ waypoints = params[:waypoints] || []
30
+ options = {
31
+ origin: origin,
32
+ destination: destination,
33
+ waypoints: waypoints.join("|"),
34
+ alternatives: params[:alternatives],
35
+ avoid: params[:avoid],
36
+ units: params[:units],
37
+ region: params[:region],
38
+ departure_time: params[:departure_time],
39
+ arrival_time: params[:arrival_time],
40
+ sensor: @sensor
41
+ }
42
+ compact_hash(options)
21
43
  end
22
44
 
23
45
  private
24
46
 
47
+ def compact_hash(hash)
48
+ hash.select { |k,v| !v.nil? && !v.to_s.empty? }
49
+ end
50
+
25
51
  def components(components)
26
52
  components.collect do |component, value|
27
53
  "#{component}:#{value}"