Skip to content
Snippets Groups Projects
Unverified Commit 34458da3 authored by Tamas Erdos's avatar Tamas Erdos
Browse files

refactoring and adding test

parent 78a576e3
No related branches found
No related tags found
No related merge requests found
...@@ -11,8 +11,28 @@ module Geocoder ...@@ -11,8 +11,28 @@ module Geocoder
@value @value
end end
def valid? def active?
@expires_at > Time.now @expires_at > Time.now
end end
def self.generate_token(client_id, client_secret, 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: client_id,
client_secret: client_secret,
grant_type: 'client_credentials',
expiration: expires # (minutes) max: 20160, default: 1 day
response = JSON.parse(getToken.body)
if response['error']
Geocoder.log(:warn, response['error'])
else
token_value = response['access_token']
expires_at = Time.now + expires.minutes
new(token_value, expires_at)
end
end
end end
end end
...@@ -16,24 +16,6 @@ module Geocoder::Lookup ...@@ -16,24 +16,6 @@ module Geocoder::Lookup
url_query_string(query) url_query_string(query)
end 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 # --------------------------------------------------------------- private # ---------------------------------------------------------------
def results(query) def results(query)
...@@ -66,10 +48,10 @@ module Geocoder::Lookup ...@@ -66,10 +48,10 @@ module Geocoder::Lookup
end end
def token def token
if configuration.token && configuration.token.valid? # if we have a token, use it if configuration.token && configuration.token.active? # if we have a token, use it
configuration.token.to_s configuration.token.to_s
elsif configuration.api_key # generate a new token if we have credentials elsif configuration.api_key # generate a new token if we have credentials
token_instance = generate_token token_instance = EsriToken.generate_token(*configuration.api_key)
Geocoder.configure(:esri => {:token => token_instance}) Geocoder.configure(:esri => {:token => token_instance})
token_instance.to_s token_instance.to_s
end end
......
...@@ -15,6 +15,16 @@ class EsriTest < GeocoderTestCase ...@@ -15,6 +15,16 @@ class EsriTest < GeocoderTestCase
res res
end end
def test_query_for_geocode_with_token_for_storage
token = 'xxxxx'
Geocoder.configure(token: token, for_storage: true)
query = Geocoder::Query.new("Bluffton, SC")
lookup = Geocoder::Lookup.get(:esri)
res = lookup.query_url(query)
assert_equal "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find?f=pjson&outFields=%2A&text=Bluffton%2C+SC&forStorage=true&token=xxxxx",
res
end
def test_query_for_reverse_geocode def test_query_for_reverse_geocode
query = Geocoder::Query.new([45.423733, -75.676333]) query = Geocoder::Query.new([45.423733, -75.676333])
lookup = Geocoder::Lookup.get(:esri) lookup = Geocoder::Lookup.get(:esri)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment