lib/3scale/client.rb in 3scale_client-2.3.3 vs lib/3scale/client.rb in 3scale_client-2.3.4

- old
+ new

@@ -156,13 +156,14 @@ # # == Parameters # # Hash with options: # - # app_id:: id of the application to authorize. This is required. - # app_key:: secret key assigned to the application. Required only if application has - # a key defined. + # app_id:: id of the application to authorize. This is required. + # app_key:: secret key assigned to the application. Required only if application has + # a key defined. + # service_id:: id of the service (required if you have more than one service) # # == Return # # An ThreeScale::AuthorizeResponse object. It's +success?+ method returns true if # the authorization is successful, false otherwise. It contains additional information @@ -180,14 +181,11 @@ # if response.success? # # All good. Proceed... # end # def authorize(options) - path = "/transactions/authorize.xml" + - "?provider_key=#{CGI.escape(provider_key)}" + - "&app_id=#{CGI.escape(options[:app_id].to_s)}" - path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key] + path = "/transactions/authorize.xml" + options_to_params(options, ALL_PARAMS) uri = URI.parse("http://#{host}#{path}") http_response = Net::HTTP.get_response(uri) case http_response @@ -205,10 +203,11 @@ # == Parameters # # Hash with options: # # app_id:: id of the application to authorize. This is required. + # service_id:: id of the service (required if you have more than one service) # # == Return # # A ThreeScale::AuthorizeResponse object. It's +success?+ method returns true if # the authorization is successful, false otherwise. It contains additional information @@ -229,15 +228,11 @@ # if response.success? # # All good. Proceed... # end # def oauth_authorize(options) - path = "/transactions/oauth_authorize.xml" + - "?provider_key=#{CGI.escape(provider_key)}" + - "&app_id=#{CGI.escape(options[:app_id].to_s)}" - path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key] - path += "&redirect_url=#{CGI.escape(options[:redirect_url])}" if options[:redirect_url] + path = "/transactions/oauth_authorize.xml" + options_to_params(options, OAUTH_PARAMS) uri = URI.parse("http://#{host}#{path}") http_response = Net::HTTP.get_response(uri) case http_response @@ -249,9 +244,26 @@ raise ServerError.new(http_response) end end private + + OAUTH_PARAMS = [:app_id, :app_key, :service_id, :redirect_url] + ALL_PARAMS = [:user_key, :app_id, :app_key, :service_id, :redirect_url] + + def options_to_params(options, allowed_keys) + params = { :provider_key => provider_key } + + allowed_keys.each do |key| + params[key] = options[key] if options.has_key?(key) + end + + tuples = params.map do |key, value| + "#{key}=#{CGI.escape(value.to_s)}" + end + + '?' + tuples.join('&') + end def encode_transactions(transactions) result = {} transactions.each_with_index do |transaction, index|