Skip to content
Snippets Groups Projects
Commit 749c1d8f authored by Tim Neems's avatar Tim Neems
Browse files

Allow esri token to be set at call time

parent 1658b655
No related branches found
No related tags found
No related merge requests found
...@@ -40,7 +40,9 @@ module Geocoder::Lookup ...@@ -40,7 +40,9 @@ module Geocoder::Lookup
else else
params[:text] = query.sanitized_text params[:text] = query.sanitized_text
end end
params[:token] = token
params[:token] = token(query)
if for_storage_value = for_storage(query) if for_storage_value = for_storage(query)
params[:forStorage] = for_storage_value params[:forStorage] = for_storage_value
end end
...@@ -56,25 +58,38 @@ module Geocoder::Lookup ...@@ -56,25 +58,38 @@ module Geocoder::Lookup
end end
end end
def token def token(query)
create_and_save_token! if !valid_token_configured? and configuration.api_key token_instance = if query.options[:token]
configuration[:token].to_s unless configuration[:token].nil? query.options[:token]
else
configuration[:token]
end
if !valid_token_configured?(token_instance) && configuration.api_key
token_instance = create_and_save_token!(query)
end
token_instance.to_s unless token_instance.nil?
end end
def valid_token_configured? def valid_token_configured?(token_instance)
!configuration[:token].nil? and configuration[:token].active? !token_instance.nil? && token_instance.active?
end end
def create_and_save_token! def create_and_save_token!(query)
save_token!(create_token) token_instance = create_token
if query.options[:token]
query.options[:token] = token_instance
else
Geocoder.merge_into_lookup_config(:esri, token: token_instance)
end
token_instance
end end
def create_token def create_token
Geocoder::EsriToken.generate_token(*configuration.api_key) Geocoder::EsriToken.generate_token(*configuration.api_key)
end end
def save_token!(token_instance)
Geocoder.merge_into_lookup_config(:esri, token: token_instance)
end
end end
end end
...@@ -34,6 +34,24 @@ class EsriTest < GeocoderTestCase ...@@ -34,6 +34,24 @@ class EsriTest < GeocoderTestCase
assert_match %r{token=xxxxx}, url assert_match %r{token=xxxxx}, url
end end
def test_token_from_options
options_token = Geocoder::EsriToken.new('options_token', Time.now + 60*60*24)
query = Geocoder::Query.new("Bluffton, SC", token: options_token)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{token=options_token}, url
end
def test_token_from_options_overrides_configuration
config_token = Geocoder::EsriToken.new('config_token', Time.now + 60*60*24)
options_token = Geocoder::EsriToken.new('options_token', Time.now + 60*60*24)
Geocoder.configure(esri: { token: config_token })
query = Geocoder::Query.new("Bluffton, SC", token: options_token)
lookup = Geocoder::Lookup.get(:esri)
url = lookup.query_url(query)
assert_match %r{token=options_token}, url
end
def test_query_for_geocode_with_config_for_storage_false def test_query_for_geocode_with_config_for_storage_false
Geocoder.configure(esri: {for_storage: false}) Geocoder.configure(esri: {for_storage: false})
......
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