Class: Agree2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/agree2/client.rb

Overview

If you haven’t done so already register your application at agree2.com/client_applications

The full authorization process works like this over 2 controller actions (This example is in Rails):

 def request_token
   @client = Agree2::Client.new "key","secret"
   @request_token = @client.get_request_token

   # Store the token in a model in your applications mapping it to your user
   Agree2RequestToken.create :user=>current_user,:token=>@request_token.token,:secret=>@request_token.secret
   redirect_to @request_token.authorize_url
 end

 # The user authorizes the token on the Agree2 web site and is redirected to the authorize_url you setup when you registered your application at:
 # https://agree2.com/client_applications
 def authorize
   @client = Agree2::Client.new "key","secret"

   # Load the request token from your Agree2RequestToken through your user model
   @request_token = current_user.agree2_request_token.request_token

   # Exchange the authorized request token for a more permanent User token on the Agree2 site
   @user_client=@client.user_from_request_token(@request_token)

   # Store the Agree2 user data in your own model
   @agree2_user=Agree2User.create :user=>current_user,:token=>@user_client.token,:secret=>@user_client.secret
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret) ⇒ Client

Initialize the Agree2 Client

Required Fields

  • key - The Consumer Key

  • secret - The Consumer Secret

To get these register your application at: agree2.com/client_applications



44
45
46
# File 'lib/agree2/client.rb', line 44

def initialize(key,secret)
  @consumer=OAuth::Consumer.new(key,secret,{:site=>AGREE2_URL})
end

Instance Attribute Details

#consumerObject

Returns the value of attribute consumer.



34
35
36
# File 'lib/agree2/client.rb', line 34

def consumer
  @consumer
end

Instance Method Details

#get_request_tokenObject

Start the process of authorizing a token for an Agree2 user.

Example:

@request_token = @client.get_request_token
redirect_to @request_token.authorize_url


66
67
68
# File 'lib/agree2/client.rb', line 66

def get_request_token
  consumer.get_request_token
end

#keyObject

Returns your consumer key



92
93
94
# File 'lib/agree2/client.rb', line 92

def key
  @consumer.key
end

#secretObject

Returns your consumer secret



97
98
99
# File 'lib/agree2/client.rb', line 97

def secret
  @consumer.secret
end

#user(token, token_secret) ⇒ Object

initialize a new user object with the given token and secret. The user object is what you use to do most of the work. Use this method when you need to create a user object from a token and secret you have stored in your applications database.

Required Fields

  • token - This is the authorized Token

  • secret - This is the authorized Token Secret



56
57
58
# File 'lib/agree2/client.rb', line 56

def user(token,token_secret)
  User.new(self,token,token_secret)
end

#user_from_request_token(request_token) ⇒ Object

Exchange an Authorized RequestToken for a working user object

Required Field

  • request_token The Request token created using get_request_token and authorized on Agree2 by your user

Example:

@user_client = @client.user_from_request_token(@request_token)


80
81
82
83
84
85
86
87
88
89
# File 'lib/agree2/client.rb', line 80

def user_from_request_token(request_token)
  access_token=request_token.get_access_token
  user(access_token.token,access_token.secret)
rescue Net::HTTPServerException=>e
  if e.response.code=='401'
    raise Agree2Exception,"The user has not authorized this request token",caller
  else
    raise
  end
end