geoloco 1.0.0
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.
- checksums.yaml +7 -0
- data/.editorconfig +14 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/geoloco.gemspec +48 -0
- data/lib/geoloco.rb +66 -0
- data/lib/geoloco/adapters/fake.rb +26 -0
- data/lib/geoloco/adapters/google.rb +81 -0
- data/lib/geoloco/adapters/tomtom.rb +63 -0
- data/lib/geoloco/geometry.rb +9 -0
- data/lib/geoloco/location.rb +33 -0
- data/lib/geoloco/version.rb +5 -0
- metadata +138 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3c53324fe06fb58786ff555b0a11d3ca0fd2bda60f489b31fd20ab4975266d2c
|
|
4
|
+
data.tar.gz: 6d6ac9d5df99653e1e2ab13fd956966cde186b6e3da50a105f62c869c96a82e0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5165c03d95aa767769f1294b58135d29fba59e79ea88035dde945f3f95e410cf94643fcaaeeb58c0f92d897f8d46813adcc3d35ca88470ce1164b282604238ba
|
|
7
|
+
data.tar.gz: 8aaea202d406c55be223bef589a5e7c1dfcfa0d36e584cff04b68cb2c0af149647920eb2b9849beb248addf7c5a55afec0f5eab38fad3e0acbd4443f3b546493
|
data/.editorconfig
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# EditorConfig is awesome: https://EditorConfig.org
|
|
2
|
+
# top-most EditorConfig file
|
|
3
|
+
root = true
|
|
4
|
+
|
|
5
|
+
# Unix-style newlines with a newline ending every file
|
|
6
|
+
[*]
|
|
7
|
+
trim_trailing_whitespace = true
|
|
8
|
+
end_of_line = lf
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
|
|
11
|
+
# d space indentation
|
|
12
|
+
[*.{js,jsx,rb,yml,yaml,scss,json,md}]
|
|
13
|
+
indent_style = space
|
|
14
|
+
indent_size = 2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.5.0
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
sudo: false
|
|
3
|
+
language: ruby
|
|
4
|
+
cache: bundler
|
|
5
|
+
rvm:
|
|
6
|
+
- 2.5.0
|
|
7
|
+
- 2.6.3
|
|
8
|
+
before_install: gem install bundler -v 1.17.2
|
|
9
|
+
deploy:
|
|
10
|
+
provider: rubygems
|
|
11
|
+
api_key:
|
|
12
|
+
secure: aCipdg6IqKaxQmWQXFlqI2JqGOrCAxzYlutOn6nCSk9VCzmlRjDoEp1M99ASvmF5CP3KQgfRq8bxfLql8jss8tymK0u2ExvKaUglK0zb2KoLQDGDGuHw3RxCTnaxPBuO4/2PuS+nso6IcdqUaGOzh8FA7ePKPBmxl3kyNtArEDV88Eyx6tQYs1/1w153bAeBk57nTiu8CPS+dIWg+guQADTLPZ61fH4xKjPWXnr65pFF7YtU5YdPBHFkqL60X384OZj1c7ZTGvFj70+36oo617HigSg8HCE+E0R1N4JQ8/5xo2yHBdrtgsAqYyYWRgm1C6qu+T6yKMuokdoSd0Ji3Rigz6VRrOJfjmQQkwMnno9fCY3vND62zOb9Ow+MCNuQYSgNHc+YPURKmT05wadG0FpZdmo6hstPusleuG89NxnB/s2YjR0QWQK05MW1uhFdru1S2gBd1fMH1GLoThhdfGG1UJSkKOipUJyRupFaB9zimJO3HwaTP0Q+wP8MGZDAqbyzQ3bJSZaQmJC6loC2EtxxnOQxUIxaySLtNEU2LOr9IijExj1ldXkz8X1fIWwRr6BPnIeChFnuST+L9UDYtqk1WgQTUs+tOAxUjepAWhtIUL5h3Jg2NZd9RxPXq8IngDWZJwT8yA3E9wloQx5JzTIMas5wHwCQJuwjjk5ibss=
|
|
13
|
+
gem: consent
|
|
14
|
+
on:
|
|
15
|
+
tags: true
|
|
16
|
+
repo: powerhome/consent
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
geoloco (1.0.0)
|
|
5
|
+
httparty (>= 0.14.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
httparty (0.17.1)
|
|
12
|
+
mime-types (~> 3.0)
|
|
13
|
+
multi_xml (>= 0.5.2)
|
|
14
|
+
mime-types (3.3)
|
|
15
|
+
mime-types-data (~> 3.2015)
|
|
16
|
+
mime-types-data (3.2019.1009)
|
|
17
|
+
multi_xml (0.6.0)
|
|
18
|
+
rake (10.5.0)
|
|
19
|
+
rspec (3.9.0)
|
|
20
|
+
rspec-core (~> 3.9.0)
|
|
21
|
+
rspec-expectations (~> 3.9.0)
|
|
22
|
+
rspec-mocks (~> 3.9.0)
|
|
23
|
+
rspec-core (3.9.0)
|
|
24
|
+
rspec-support (~> 3.9.0)
|
|
25
|
+
rspec-expectations (3.9.0)
|
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
27
|
+
rspec-support (~> 3.9.0)
|
|
28
|
+
rspec-mocks (3.9.0)
|
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
+
rspec-support (~> 3.9.0)
|
|
31
|
+
rspec-support (3.9.0)
|
|
32
|
+
timecop (0.9.1)
|
|
33
|
+
|
|
34
|
+
PLATFORMS
|
|
35
|
+
ruby
|
|
36
|
+
|
|
37
|
+
DEPENDENCIES
|
|
38
|
+
bundler (~> 1.17)
|
|
39
|
+
geoloco!
|
|
40
|
+
rake (~> 10.0)
|
|
41
|
+
rspec (~> 3.0)
|
|
42
|
+
timecop (>= 0.9)
|
|
43
|
+
|
|
44
|
+
BUNDLED WITH
|
|
45
|
+
1.17.3
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Carlos Palhares
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Geoloco [](https://travis-ci.org/powerhome/geoloco)
|
|
2
|
+
|
|
3
|
+
Geoloco is a multi-adpater geolocation gem, with error handling and test stubs
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'geoloco'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install geoloco
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
TODO: Write usage instructions here
|
|
24
|
+
|
|
25
|
+
## Development
|
|
26
|
+
|
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
28
|
+
|
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
30
|
+
|
|
31
|
+
## Contributing
|
|
32
|
+
|
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/powerhome/geoloco.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "geoloco"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/geoloco.gemspec
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'geoloco/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'geoloco'
|
|
9
|
+
spec.version = Geoloco::VERSION
|
|
10
|
+
spec.authors = ['Carlos Palhares', 'Rafael Garcia']
|
|
11
|
+
spec.email = ['chjunior@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'Multi adapter geolocation tool'
|
|
14
|
+
spec.description = 'Multi adapter geolocation tool'
|
|
15
|
+
spec.homepage = 'https://github.com/powerhome/geoloco'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the
|
|
19
|
+
# 'allowed_push_host' to allow pushing to a single host or delete this section
|
|
20
|
+
# to allow pushing to any host.
|
|
21
|
+
if spec.respond_to?(:metadata)
|
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
23
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
|
24
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
|
25
|
+
else
|
|
26
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
|
27
|
+
'public gem pushes.'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
|
32
|
+
# into git.
|
|
33
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
34
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
35
|
+
f.match(%r{^(test|spec|features)/})
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
spec.bindir = 'exe'
|
|
39
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
40
|
+
spec.require_paths = ['lib']
|
|
41
|
+
|
|
42
|
+
spec.add_dependency 'httparty', '>= 0.14.0'
|
|
43
|
+
|
|
44
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
|
45
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
46
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
47
|
+
spec.add_development_dependency 'timecop', '>= 0.9'
|
|
48
|
+
end
|
data/lib/geoloco.rb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'httparty'
|
|
4
|
+
|
|
5
|
+
require 'geoloco/geometry'
|
|
6
|
+
require 'geoloco/location'
|
|
7
|
+
require 'geoloco/version'
|
|
8
|
+
require 'geoloco/adapters/fake'
|
|
9
|
+
require 'geoloco/adapters/google'
|
|
10
|
+
require 'geoloco/adapters/tomtom'
|
|
11
|
+
|
|
12
|
+
# Geoloco is a multi-adpater geolocation gem, with error
|
|
13
|
+
# handling and test stubs
|
|
14
|
+
module Geoloco
|
|
15
|
+
# Top level Geoloco's error class
|
|
16
|
+
class Error < StandardError; end
|
|
17
|
+
|
|
18
|
+
# Error when an unknown adapter is given
|
|
19
|
+
class UnknownAdapter < Error; end
|
|
20
|
+
|
|
21
|
+
# Forbidden error class. Raised by adapters when a forbidden
|
|
22
|
+
# error is received from the providers
|
|
23
|
+
class Forbidden < Geoloco::Error
|
|
24
|
+
def initialize(response)
|
|
25
|
+
super "#{response.code} - #{response.body}"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
attr_writer :config, :default_adapter, :http
|
|
31
|
+
|
|
32
|
+
def default_adapter
|
|
33
|
+
@default_adapter || :google
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def config
|
|
37
|
+
@config || {}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def http
|
|
41
|
+
@http || HTTParty
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Geocodes a given query using the given adapter and options
|
|
45
|
+
#
|
|
46
|
+
# @param query [String] the query to geocode
|
|
47
|
+
# @param adapter [String,Symbol] the adapter to use
|
|
48
|
+
# @returns [Array<Geocode::Location>]
|
|
49
|
+
# @raise [Geoloco::Error] if an error occurs
|
|
50
|
+
# @raise [Geoloco::UnknownAdapter] if an unknown adapter is given
|
|
51
|
+
# @raise [Geoloco::Forbidden] if the geocoder API returns a 403 error
|
|
52
|
+
def geocode(query, adapter: default_adapter, **options)
|
|
53
|
+
adapter_config = config.fetch(adapter, {}).merge(options)
|
|
54
|
+
geocoder(adapter).geocode(query, **adapter_config)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def geocoder(adapter)
|
|
60
|
+
camelized = adapter.to_s.split('_').map(&:capitalize).join
|
|
61
|
+
Geoloco::Adapters.const_get(camelized)
|
|
62
|
+
rescue NameError
|
|
63
|
+
raise Geoloco::UnknownAdapter
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Geoloco
|
|
4
|
+
module Adapters
|
|
5
|
+
# Fake geocoding adapter
|
|
6
|
+
module Fake
|
|
7
|
+
class << self
|
|
8
|
+
DEFAULT_GEO = Geoloco::Geometry.new(lat: -19.9191248, lng: -43.9386291)
|
|
9
|
+
|
|
10
|
+
def geocode(address, **_options)
|
|
11
|
+
location = @stub || Geoloco::Location.new(
|
|
12
|
+
geometry: DEFAULT_GEO,
|
|
13
|
+
full_address: address
|
|
14
|
+
)
|
|
15
|
+
[location]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def stubbing(location)
|
|
19
|
+
@stub = location
|
|
20
|
+
yield
|
|
21
|
+
@stub = nil
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'openssl'
|
|
5
|
+
|
|
6
|
+
module Geoloco
|
|
7
|
+
module Adapters
|
|
8
|
+
# Google geocoding adapter
|
|
9
|
+
module Google
|
|
10
|
+
API_HOST = 'https://maps.googleapis.com'
|
|
11
|
+
GEOCODE_PATH = '/maps/api/geocode/json'
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
def geocode(address, client_id:, key:)
|
|
15
|
+
response = Geoloco.http.get(geocode_url(address, client_id, key))
|
|
16
|
+
handle_errors(response, response.parsed_response)
|
|
17
|
+
map_results(response.parsed_response)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def handle_errors(response, parsed)
|
|
23
|
+
raise Geoloco::Forbidden, response if response.code == 403
|
|
24
|
+
|
|
25
|
+
status = parsed&.dig('status')
|
|
26
|
+
return if status.eql?('OK')
|
|
27
|
+
|
|
28
|
+
message = parsed&.dig('error_message')
|
|
29
|
+
|
|
30
|
+
raise Geoloco::Error, [status, message].compact.join(' - ')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def map_results(parsed)
|
|
34
|
+
parsed.fetch('results', [])
|
|
35
|
+
.map(&method(:map_result))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# rubocop:disable Metrics/MethodLength, Metrics/LineLength, Metrics/AbcSize
|
|
39
|
+
def map_result(result)
|
|
40
|
+
loc = result.dig('geometry', 'location') || {}
|
|
41
|
+
geometry = Geoloco::Geometry.new(lat: loc['lat'], lng: loc['lng'])
|
|
42
|
+
|
|
43
|
+
Geoloco::Location.new(
|
|
44
|
+
geometry: geometry,
|
|
45
|
+
full_address: result.dig('formatted_address'),
|
|
46
|
+
street: get_component(result, 'route'),
|
|
47
|
+
city: get_component(result, 'locality'),
|
|
48
|
+
district: get_component(result, 'administrative_area_level_2'),
|
|
49
|
+
municipality: get_component(result, 'administrative_area_level_3'),
|
|
50
|
+
number: get_component(result, 'street_number'),
|
|
51
|
+
state: get_component(result, 'administrative_area_level_1'),
|
|
52
|
+
state_code: get_component(result, 'administrative_area_level_1', 'short_name'),
|
|
53
|
+
zipcode: get_component(result, 'postal_code'),
|
|
54
|
+
country: get_component(result, 'country'),
|
|
55
|
+
country_code: get_component(result, 'country', 'short_name')
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
# rubocop:enable Metrics/MethodLength, Metrics/LineLength, Metrics/AbcSize
|
|
59
|
+
|
|
60
|
+
def get_component(result, type, data = 'long_name')
|
|
61
|
+
result.fetch('address_components', [])
|
|
62
|
+
.find { |component| component['types'].include?(type) }
|
|
63
|
+
&.dig(data)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def geocode_url(address, client_id, key)
|
|
67
|
+
query = URI.encode_www_form(client: client_id, address: address)
|
|
68
|
+
signed_url(GEOCODE_PATH, query, key)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def signed_url(path, query, key)
|
|
72
|
+
decoded_key = Base64.decode64(key.tr('-_', '+/'))
|
|
73
|
+
path_query = "#{path}?#{query}"
|
|
74
|
+
signature = OpenSSL::HMAC.digest('sha1', decoded_key, path_query)
|
|
75
|
+
base64_signature = Base64.strict_encode64(signature).tr('+/', '-_')
|
|
76
|
+
"#{API_HOST}#{path_query}&signature=#{base64_signature}"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Geoloco
|
|
4
|
+
module Adapters
|
|
5
|
+
# Tomtom geocoding adapter
|
|
6
|
+
module Tomtom
|
|
7
|
+
GEOCODE_URL = 'https://api.tomtom.com/search/2/geocode/%s.json'
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def geocode(address, key:, qps_limit: 5)
|
|
11
|
+
wait_qps_limit_time(qps_limit) unless qps_limit&.zero?
|
|
12
|
+
response = Geoloco.http.get(geocode_url(address), query: { key: key })
|
|
13
|
+
handle_errors(response)
|
|
14
|
+
map_results(response.parsed_response)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def geocode_url(address)
|
|
20
|
+
escaped_address = CGI.escape(address)
|
|
21
|
+
format(GEOCODE_URL, escaped_address)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def handle_errors(response)
|
|
25
|
+
raise Geoloco::Forbidden, response if response.code == 403
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def map_results(parsed)
|
|
29
|
+
parsed.fetch('results', [])
|
|
30
|
+
.map(&method(:map_result))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
34
|
+
def map_result(result)
|
|
35
|
+
address = result['address']
|
|
36
|
+
geometry = Geoloco::Geometry.new(
|
|
37
|
+
lat: result.dig('position', 'lat').to_f,
|
|
38
|
+
lng: result.dig('position', 'lon').to_f
|
|
39
|
+
)
|
|
40
|
+
Geoloco::Location.new(
|
|
41
|
+
geometry: geometry,
|
|
42
|
+
full_address: address['freeformAddress'],
|
|
43
|
+
street: address['streetName'], number: address['streetNumber'],
|
|
44
|
+
city: address['municipality'], zipcode: address['postalCode'],
|
|
45
|
+
district: address['countrySecondarySubdivision'],
|
|
46
|
+
municipality: address['countryTertiarySubdivision'],
|
|
47
|
+
state: address['countrySubdivisionName'],
|
|
48
|
+
state_code: address['countrySubdivision'],
|
|
49
|
+
country: address['country'], country_code: address['countryCode']
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
|
53
|
+
|
|
54
|
+
def wait_qps_limit_time(qps_limit)
|
|
55
|
+
time_since_last_call = @last_api_call ? Time.now - @last_api_call : 2
|
|
56
|
+
wait_time = 1.0 / qps_limit.to_f - time_since_last_call
|
|
57
|
+
sleep wait_time if wait_time.positive?
|
|
58
|
+
@last_api_call = Time.now
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'forwardable'
|
|
4
|
+
|
|
5
|
+
module Geoloco
|
|
6
|
+
Location = Struct.new(
|
|
7
|
+
:full_address, :street, :number, :zipcode,
|
|
8
|
+
:district, :city, :municipality, :geometry,
|
|
9
|
+
:state, :state_code, :country, :country_code,
|
|
10
|
+
keyword_init: true
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
# Location data
|
|
14
|
+
#
|
|
15
|
+
# @attr [String] full_address the fisical address
|
|
16
|
+
# @attr [String] street number on the street
|
|
17
|
+
# @attr [String] number number on the street
|
|
18
|
+
# @attr [String] zipcode the zipcode of the location
|
|
19
|
+
# @attr [String] district the district/county of the location
|
|
20
|
+
# @attr [String] city the city of the location
|
|
21
|
+
# @attr [String] state the full name of the state
|
|
22
|
+
# @attr [String] country the country
|
|
23
|
+
# @attr [String] country_code the 2-letter country code
|
|
24
|
+
# @attr [String] municipality the municipality which the location belongs
|
|
25
|
+
# @attr [Geoloco::Geometry] geometry latitude and longitude pair
|
|
26
|
+
# @attr [Float] lat latitude
|
|
27
|
+
# @attr [Float] lng longitude
|
|
28
|
+
class Location
|
|
29
|
+
extend Forwardable
|
|
30
|
+
|
|
31
|
+
def_delegators :geometry, :lat, :lng
|
|
32
|
+
end
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: geoloco
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Carlos Palhares
|
|
8
|
+
- Rafael Garcia
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: exe
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2019-11-22 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: httparty
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - ">="
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: 0.14.0
|
|
21
|
+
type: :runtime
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 0.14.0
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: bundler
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - "~>"
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '1.17'
|
|
35
|
+
type: :development
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - "~>"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '1.17'
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: rake
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - "~>"
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '10.0'
|
|
49
|
+
type: :development
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - "~>"
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '10.0'
|
|
56
|
+
- !ruby/object:Gem::Dependency
|
|
57
|
+
name: rspec
|
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - "~>"
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '3.0'
|
|
63
|
+
type: :development
|
|
64
|
+
prerelease: false
|
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - "~>"
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '3.0'
|
|
70
|
+
- !ruby/object:Gem::Dependency
|
|
71
|
+
name: timecop
|
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0.9'
|
|
77
|
+
type: :development
|
|
78
|
+
prerelease: false
|
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0.9'
|
|
84
|
+
description: Multi adapter geolocation tool
|
|
85
|
+
email:
|
|
86
|
+
- chjunior@gmail.com
|
|
87
|
+
executables: []
|
|
88
|
+
extensions: []
|
|
89
|
+
extra_rdoc_files: []
|
|
90
|
+
files:
|
|
91
|
+
- ".editorconfig"
|
|
92
|
+
- ".gitignore"
|
|
93
|
+
- ".rspec"
|
|
94
|
+
- ".ruby-version"
|
|
95
|
+
- ".travis.yml"
|
|
96
|
+
- Gemfile
|
|
97
|
+
- Gemfile.lock
|
|
98
|
+
- LICENSE.txt
|
|
99
|
+
- README.md
|
|
100
|
+
- Rakefile
|
|
101
|
+
- bin/console
|
|
102
|
+
- bin/setup
|
|
103
|
+
- geoloco.gemspec
|
|
104
|
+
- lib/geoloco.rb
|
|
105
|
+
- lib/geoloco/adapters/fake.rb
|
|
106
|
+
- lib/geoloco/adapters/google.rb
|
|
107
|
+
- lib/geoloco/adapters/tomtom.rb
|
|
108
|
+
- lib/geoloco/geometry.rb
|
|
109
|
+
- lib/geoloco/location.rb
|
|
110
|
+
- lib/geoloco/version.rb
|
|
111
|
+
homepage: https://github.com/powerhome/geoloco
|
|
112
|
+
licenses:
|
|
113
|
+
- MIT
|
|
114
|
+
metadata:
|
|
115
|
+
homepage_uri: https://github.com/powerhome/geoloco
|
|
116
|
+
source_code_uri: https://github.com/powerhome/geoloco
|
|
117
|
+
changelog_uri: https://github.com/powerhome/geoloco
|
|
118
|
+
post_install_message:
|
|
119
|
+
rdoc_options: []
|
|
120
|
+
require_paths:
|
|
121
|
+
- lib
|
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
123
|
+
requirements:
|
|
124
|
+
- - ">="
|
|
125
|
+
- !ruby/object:Gem::Version
|
|
126
|
+
version: '0'
|
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
requirements: []
|
|
133
|
+
rubyforge_project:
|
|
134
|
+
rubygems_version: 2.7.3
|
|
135
|
+
signing_key:
|
|
136
|
+
specification_version: 4
|
|
137
|
+
summary: Multi adapter geolocation tool
|
|
138
|
+
test_files: []
|