Permalink
Browse files

Squashed commit of the following:

commit 2d4df6a0cb83f1ddc0299b3a70b6aaba938786df
Author: Tiago Macedo <tftfmacedo@gmail.com>
Date:   Mon Mar 5 20:40:43 2012 +0100

    the oauth_authorize call now supports both redirect_url and app_key
    parameters

commit 8addeda525f01259b7db0800fcb39fc4d06a618d
Author: Tiago Macedo <tftfmacedo@gmail.com>
Date:   Mon Mar 5 20:39:55 2012 +0100

    bundler is now using only the gemspec, removing .rvmrc and adding some
    stuff to the git ignore

commit 7a48a73
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Sat Mar 3 09:26:50 2012 +0100

    Add oauth_authorize to README

commit deb2e7b
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Sat Mar 3 09:17:42 2012 +0100

    Revert "Add test for failing authorize due to missing app_key"

    This reverts commit 5edfcfd.

commit 5edfcfd
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Fri Mar 2 17:08:14 2012 +0100

    Add test for failing authorize due to missing app_key

commit d10ad8e
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Thu Mar 1 17:20:44 2012 +0100

    Update .travis.yml to test only 1.9.2 and 1.9.3

commit d8e5eaf
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Thu Mar 1 17:15:20 2012 +0100

    Add .travis.yml to specify ruby versions to try

commit 94fcfd0
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Thu Mar 1 17:12:42 2012 +0100

    Update VERSION to match gemspec

commit 0614097
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Thu Mar 1 17:07:46 2012 +0100

    Bring back authorize method, now we have authorize and oauth_authorize

commit da8c940
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Thu Mar 1 16:54:47 2012 +0100

    Update remote_test for oauth_authorize

commit 6cf1bf4
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Wed Feb 29 18:21:47 2012 +0100

    AuthorizeResponse can read redirect_url when using OAuth

commit 16e8e81
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Wed Feb 29 18:17:56 2012 +0100

    AuthorizeResponse can read app_key when using OAuth

commit 16886a5
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Wed Feb 29 18:06:26 2012 +0100

    Rename authorize method in Client as oauth_authorize

commit 5230a46
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Wed Feb 29 17:41:35 2012 +0100

    Update Gemfile

commit 2c1d942
Author: Jaime Iniesta <jaimeiniesta@gmail.com>
Date:   Wed Feb 29 17:41:08 2012 +0100

    Add .rvmrc to set desired ruby version for development

commit aa5b264
Author: Sebastian Arcila Valenzuela <sebastianarcila@gmail.com>
Date:   Wed Feb 8 11:27:03 2012 -0500

    add oauth setup

commit 92330f4
Author: Sebastian Arcila Valenzuela <sebastianarcila@gmail.com>
Date:   Fri Feb 3 11:01:31 2012 -0500

    use bundle setup to run tests

commit 697c0da
Author: Sebastian Arcila Valenzuela <sebastianarcila@gmail.com>
Date:   Fri Feb 3 10:09:07 2012 -0500

    add gemfile
  • Loading branch information...
1 parent 9854621 commit 58418d9c8ffb545e5048d8fa15eaca49d73f4ac9 @tmacedo tmacedo committed Mar 5, 2012
Showing with 261 additions and 28 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 .travis.yml
  3. +5 −1 3scale_client.gemspec
  4. +2 −0 Gemfile
  5. +33 −0 Gemfile.lock
  6. +15 −4 README.rdoc
  7. +3 −2 Rakefile
  8. +1 −1 VERSION
  9. +3 −1 lib/3scale/authorize_response.rb
  10. +62 −7 lib/3scale/client.rb
  11. +115 −10 test/client_test.rb
  12. +17 −2 test/remote_test.rb
View
@@ -1 +1,3 @@
pkg
+Gemfile.lock
+.*.swp
View
@@ -0,0 +1,3 @@
+rvm:
+ - 1.9.2
+ - 1.9.3
View
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.date = %q{2011-01-27}
s.description = %q{This gem allows to easily connect an application that provides a Web Service with the 3scale API Management System to authorize it's users and report the usage.
}
- s.email = %q{adam@3scale.net}
+ s.email = %q{adam@3scale.net tiago@3scale.net}
s.extra_rdoc_files = [
"README.rdoc"
]
@@ -51,5 +51,9 @@ Gem::Specification.new do |s|
else
s.add_dependency(%q<nokogiri>, [">= 0"])
end
+
+ s.add_development_dependency 'fakeweb'
+ s.add_development_dependency 'jeweler'
+ s.add_development_dependency 'mocha'
end
View
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gemspec
View
@@ -0,0 +1,33 @@
+PATH
+ remote: .
+ specs:
+ 3scale_client (2.2.7)
+ nokogiri
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ fakeweb (1.3.0)
+ git (1.2.5)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.6.5)
+ metaclass (0.0.1)
+ mocha (0.10.5)
+ metaclass (~> 0.0.1)
+ nokogiri (1.5.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ 3scale_client!
+ fakeweb
+ jeweler
+ mocha
View
@@ -6,7 +6,7 @@ This library is distributed as a gem:
gem install 3scale_client
-Or alternatively, download the source code from github:
+Or alternatively, download the source code from github:
http://github.com/3scale/3scale_ws_api_for_ruby
If you are using Bundler, please add this to your Gemfile:
@@ -74,12 +74,23 @@ down by the metrics and usage limit periods.
# If the limit is exceeded, this will be true, otherwise false:
usage_report.exceeded? # false
-If the authorization failed, the +error_code+ returns system error code and +error_message+
+If the authorization failed, the +error_code+ returns system error code and +error_message+
human readable error description:
-
+
response.error_code # "usage_limits_exceeded"
response.error_message # "Usage limits are exceeded"
+=== OAuth Authorize
+
+To authorize an application with OAuth, call the +oauth_authorize+ method passing it the application's id.
+
+ response = client.authorize(:app_id => "the app id")
+
+If the authorization is successful, the response will contain the +app_key+ and +redirect_url+ defined for this application:
+
+ response.app_key
+ response.redirect_url
+
=== Report
To report usage, use the +report+ method. You can report multiple transaction at the same time:
@@ -99,7 +110,7 @@ string has to be in a format parseable by the Time.parse method. For example:
"2010-04-28 12:38:33 +0200"
-If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
+If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
(two hours ahead of the Universal Coordinate Time) in the example abowe.
Then call the +success?+ method on the returned response object to see if the report was
View
@@ -1,8 +1,10 @@
# encoding: utf-8
+require "rubygems"
+require "bundler/setup"
require 'rake'
require 'rake/testtask'
-require 'rake/rdoctask'
+require 'rdoc/task'
desc 'Default: run unit tests.'
task :default => :test
@@ -11,7 +13,6 @@ desc 'Run unit tests.'
Rake::TestTask.new(:test) do |t|
t.pattern = 'test/**/*_test.rb'
t.verbose = true
- t.ruby_opts << '-rubygems'
end
desc 'Generate documentation.'
View
@@ -1 +1 @@
-2.2.4
+2.2.7
@@ -8,6 +8,8 @@ def initialize
end
attr_accessor :plan
+ attr_accessor :app_key
+ attr_accessor :redirect_url
class UsageReport
attr_reader :metric
@@ -32,7 +34,7 @@ def period_end
def exceeded?
current_value > max_value
end
- end
+ end
attr_reader :usage_reports
View
@@ -8,7 +8,7 @@
module ThreeScale
Error = Class.new(RuntimeError)
-
+
class ServerError < Error
def initialize(response)
super('server error')
@@ -61,11 +61,11 @@ def initialize(options)
# 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
@@ -98,7 +98,7 @@ def report(*transactions)
uri = URI.parse("http://#{host}/transactions.xml")
http_response = Net::HTTP.post_form(uri, payload)
-
+
case http_response
when Net::HTTPSuccess
build_report_response
@@ -112,7 +112,7 @@ def report(*transactions)
# Authorize an application.
#
# == Parameters
- #
+ #
# Hash with options:
#
# app_id:: id of the application to authorize. This is required.
@@ -157,11 +157,61 @@ def authorize(options)
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])
@@ -195,6 +245,11 @@ def build_authorize_response(body)
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|
@@ -212,7 +267,7 @@ def build_authorize_response(body)
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
Oops, something went wrong.

0 comments on commit 58418d9

Please sign in to comment.