lib/3scale/client.rb in 3scale_client-2.2.7 vs lib/3scale/client.rb in 3scale_client-2.2.8
- old
+ new
@@ -6,11 +6,11 @@
require '3scale/response'
require '3scale/authorize_response'
module ThreeScale
Error = Class.new(RuntimeError)
-
+
class ServerError < Error
def initialize(response)
super('server error')
@response = response
end
@@ -59,15 +59,15 @@
# these elements:
#
# app_id:: ID of the application to report the transaction for. This parameter is
# required.
# usage:: Hash of usage values. The keys are metric names and values are
- # correspoding numeric values. Example: {'hits' => 1, 'transfer' => 1024}.
+ # correspoding numeric values. Example: {'hits' => 1, 'transfer' => 1024}.
# This parameter is required.
# timestamp:: Timestamp of the transaction. This can be either a object of the
# ruby's Time class, or a string in the "YYYY-MM-DD HH:MM:SS" format
- # (if the time is in the UTC), or a string in
+ # (if the time is in the UTC), or a string in
# the "YYYY-MM-DD HH:MM:SS ZZZZZ" format, where the ZZZZZ is the time offset
# from the UTC. For example, "US Pacific Time" has offset -0800, "Tokyo"
# has offset +0900. This parameter is optional, and if not provided, equals
# to the current time.
#
@@ -96,11 +96,11 @@
payload = encode_transactions(transactions)
payload['provider_key'] = CGI.escape(provider_key)
uri = URI.parse("http://#{host}/transactions.xml")
http_response = Net::HTTP.post_form(uri, payload)
-
+
case http_response
when Net::HTTPSuccess
build_report_response
when Net::HTTPClientError
build_error_response(http_response.body)
@@ -110,11 +110,11 @@
end
# Authorize an application.
#
# == 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.
@@ -155,15 +155,65 @@
else
raise ServerError.new(http_response)
end
end
+ # Authorize an application with OAuth.
+ #
+ # == Parameters
+ #
+ # Hash with options:
+ #
+ # app_id:: id of the application to authorize. This is required.
+ #
+ # == Return
+ #
+ # A ThreeScale::AuthorizeResponse object. It's +success?+ method returns true if
+ # the authorization is successful, false otherwise. It contains additional information
+ # about the status of the usage. See the ThreeScale::AuthorizeResponse for more information.
+ #
+ # It also returns the app_key that corresponds to the given app_id
+ #
+ # In case of error, the +error_code+ returns code of the error and +error_message+
+ # human readable error description.
+ #
+ # In case of unexpected internal server error, this method raises a ThreeScale::ServerError
+ # exception.
+ #
+ # == Examples
+ #
+ # response = client.authorize(:app_id => '1234')
+ #
+ # 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]
+
+ uri = URI.parse("http://#{host}#{path}")
+ http_response = Net::HTTP.get_response(uri)
+
+ case http_response
+ when Net::HTTPSuccess,Net::HTTPConflict
+ build_authorize_response(http_response.body)
+ when Net::HTTPClientError
+ build_error_response(http_response.body)
+ else
+ raise ServerError.new(http_response)
+ end
+ end
+
private
def encode_transactions(transactions)
result = {}
-
+
transactions.each_with_index do |transaction, index|
append_encoded_value(result, index, [:app_id], transaction[:app_id])
append_encoded_value(result, index, [:timestamp], transaction[:timestamp])
append_encoded_value(result, index, [:client_ip], transaction[:client_ip])
@@ -193,10 +243,15 @@
response.success!
else
response.error!(doc.at_css('reason').content)
end
+ if doc.at_css('application')
+ response.app_key = doc.at_css('application key').content.to_s.strip
+ response.redirect_url = doc.at_css('application redirect_url').content.to_s.strip
+ end
+
response.plan = doc.at_css('plan').content.to_s.strip
doc.css('usage_reports usage_report').each do |node|
response.add_usage_report(:metric => node['metric'].to_s.strip,
:period => node['period'].to_s.strip.to_sym,
@@ -210,10 +265,10 @@
end
def build_error_response(body)
doc = Nokogiri::XML(body)
node = doc.at_css('error')
-
+
response = Response.new
response.error!(node.content.to_s.strip, node['code'].to_s.strip)
response
end
end