lib/geocoder/lookups/esri.rb in geocoder-1.5.0 vs lib/geocoder/lookups/esri.rb in geocoder-1.5.1
- old
+ new
@@ -38,33 +38,58 @@
if query.reverse_geocode?
params[:location] = query.coordinates.reverse.join(',')
else
params[:text] = query.sanitized_text
end
- params[:token] = token
- params[:forStorage] = configuration[:for_storage] if configuration[:for_storage]
+
+ params[:token] = token(query)
+
+ if for_storage_value = for_storage(query)
+ params[:forStorage] = for_storage_value
+ end
params[:sourceCountry] = configuration[:source_country] if configuration[:source_country]
params.merge(super)
end
- def token
- create_and_save_token! if !valid_token_configured? and configuration.api_key
- configuration[:token].to_s unless configuration[:token].nil?
+ def for_storage(query)
+ if query.options.has_key?(:for_storage)
+ query.options[:for_storage]
+ else
+ configuration[:for_storage]
+ end
end
- def valid_token_configured?
- !configuration[:token].nil? and configuration[:token].active?
+ def token(query)
+ token_instance = if query.options[:token]
+ 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
- def create_and_save_token!
- save_token!(create_token)
+ def valid_token_configured?(token_instance)
+ !token_instance.nil? && token_instance.active?
end
- def create_token
- Geocoder::EsriToken.generate_token(*configuration.api_key)
+ def create_and_save_token!(query)
+ 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
- def save_token!(token_instance)
- Geocoder.merge_into_lookup_config(:esri, token: token_instance)
+ def create_token
+ Geocoder::EsriToken.generate_token(*configuration.api_key)
end
end
end