Skip to content
Snippets Groups Projects
Commit 5de28ddc authored by Tamas Erdos's avatar Tamas Erdos
Browse files

Separate out token logic

- public token generation
- esri token object
parent 961c95c1
Branches
Tags
No related merge requests found
......@@ -576,7 +576,7 @@ The [Google Places Details API](https://developers.google.com/places/documentati
* **Documentation**: https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm
* **Terms of Service**: http://www.esri.com/legal/software-license
* **Limitations**: Requires API key if results will be stored. Using API key will also remove rate limit.
* **Notes**: You can specify which projection you want to use by setting, for example: `Geocoder.configure(:esri => {:outSR => 102100})`. If you will store results, set the flag and provide API key: `Geocoder.configure(:esri => {:api_key => ["client_id", "client_secret"], :for_storage => true})`
* **Notes**: You can specify which projection you want to use by setting, for example: `Geocoder.configure(:esri => {:outSR => 102100})`. If you will store results, set the flag and provide API key: `Geocoder.configure(:esri => {:api_key => ["client_id", "client_secret"], :for_storage => true})`. If you want to, you can also supply an ESRI token directly: `Geocoder.configure(:esri => {:token => Geocoder::EsriToken.new('TOKEN', Time.now + 1.day})`
#### Mapzen (`:mapzen`)
......
......@@ -62,6 +62,7 @@ module Geocoder
:distances,
:basic_auth,
:for_storage,
:token,
:logger,
:kernel_logger_level
]
......@@ -106,6 +107,7 @@ module Geocoder
@data[:cache_prefix] = "geocoder:" # prefix (string) to use for all cache keys
@data[:basic_auth] = {} # user and password for basic auth ({:user => "user", :password => "password"})
@data[:for_storage] = nil # will the result be stored for non-caching purposes (boolean)
@data[:token] = nil # token object for authentication
@data[:logger] = :kernel # :kernel or Logger instance
@data[:kernel_logger_level] = ::Logger::WARN # log level, if kernel logger is used
......
module Geocoder::Token
class EsriToken
attr_accessor :value, :expires_at
def initialize(value, expires_at)
@value = value
@expires_at = expires_at
end
def to_s
@value
end
def valid?
@expires_at > Time.now
end
end
end
require 'geocoder/lookups/base'
require "geocoder/results/esri"
require 'geocoder/esri_token'
module Geocoder::Lookup
class Esri < Base
......@@ -15,6 +16,24 @@ module Geocoder::Lookup
url_query_string(query)
end
def generate_token(expires=1440)
# creates a new token that will expire in 1 day by default
getToken = Net::HTTP.post_form URI('https://www.arcgis.com/sharing/rest/oauth2/token'),
f: 'json',
client_id: configuration.api_key[0],
client_secret: configuration.api_key[1],
grant_type: 'client_credentials',
expiration: expires # (minutes) max: 20160, default: 1 day
if JSON.parse(getToken.body)['error']
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Couldn't generate ESRI token: invalid API key.")
else
token_value = JSON.parse(getToken.body)['access_token']
expires_at = Time.now + expires.minutes
Geocoder::EsriToken.new(token_value, expires_at)
end
end
private # ---------------------------------------------------------------
def results(query)
......@@ -41,28 +60,19 @@ module Geocoder::Lookup
else
params[:text] = query.sanitized_text
end
params[:token] = token if configuration.api_key
params[:token] = token
params[:forStorage] = configuration.for_storage if configuration.for_storage
params.merge(super)
end
def token
unless token_is_valid
getToken = Net::HTTP.post_form URI('https://www.arcgis.com/sharing/rest/oauth2/token'),
f: 'json',
client_id: configuration.api_key[0],
client_secret: configuration.api_key[1],
grant_type: 'client_credentials',
expiration: 1440 # valid for one day,
@token = JSON.parse(getToken.body)['access_token']
@token_expires = Time.now + 1.day
if configuration.token && configuration.token.valid? # if we have a token, use it
configuration.token.to_s
elsif configuration.api_key # generate a new token if we have credentials
token_instance = generate_token
Geocoder.configure(:esri => {:token => token_instance})
token_instance.to_s
end
return @token
end
def token_is_valid
@token && @token_expires > Time.now
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment