Class: Freelancer::Client

Inherits:
Object
  • Object
show all
Extended by:
API
Defined in:
lib/freelancer/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

api, apis

Constructor Details

#initialize(consumer_token, consumer_secret, consumer_options = {}) ⇒ Client

Initialize a new Freelancer API client



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/freelancer/client.rb', line 21

def initialize(consumer_token, consumer_secret, consumer_options = {})
  
  default_options = {
    :request_token_path => "/RequestRequestToken/requestRequestToken.xml",
    :authorize_url => "http://www.sandbox.freelancer.com/users/api-token/auth.php",
    :access_token_path => "/RequestAccessToken/requestAccessToken.xml",
    :scheme => :query_string,
    :http_method => :get
  }
  
  @consumer_token, @consumer_secret, @consumer_options = consumer_token, consumer_secret, consumer_options.merge(default_options)
  @api_endpoint = "http://api.sandbox.freelancer.com"

end

Instance Attribute Details

#consumer_optionsObject (readonly)

Returns the value of attribute consumer_options.



18
19
20
# File 'lib/freelancer/client.rb', line 18

def consumer_options
  @consumer_options
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



18
19
20
# File 'lib/freelancer/client.rb', line 18

def consumer_secret
  @consumer_secret
end

#consumer_tokenObject (readonly)

Returns the value of attribute consumer_token.



18
19
20
# File 'lib/freelancer/client.rb', line 18

def consumer_token
  @consumer_token
end

Instance Method Details

#access_tokenObject

Get the current oAuth access token



62
63
64
# File 'lib/freelancer/client.rb', line 62

def access_token
  @access_token_instance ||= ::OAuth::AccessToken.new(consumer, @access_token, @access_secret)
end

#api_get(method, options = {}) ⇒ Object

Execute a GET-request for the specified API method



72
73
74
75
76
# File 'lib/freelancer/client.rb', line 72

def api_get(method, options = {})
  response = access_token.get(to_uri(method, options))
  raise_api_errors!(response)
  return response.body
end

#api_get_raw(method, options = {}) ⇒ Object

Execute a GET-request for the specified API method and return the raw result.



80
81
82
83
84
# File 'lib/freelancer/client.rb', line 80

def api_get_raw(method, options = {})
  response = access_token.get(to_uri(method, options))
  raise_api_errors!(response)
  response.body
end

#authorize_from_access(access_token, access_secret) ⇒ Object

Authorize a user from an oAuth access token



67
68
69
# File 'lib/freelancer/client.rb', line 67

def authorize_from_access(access_token, access_secret)
  @access_token, @access_secret = access_token, access_secret
end

#authorize_from_request(request_token, request_secret, verifier) ⇒ Object

Authorize a user from an oAuth request



53
54
55
56
57
58
59
# File 'lib/freelancer/client.rb', line 53

def authorize_from_request(request_token, request_secret, verifier)
  
  request_token = ::OAuth::RequestToken.new(consumer, request_token, request_secret)
  access_token = request_token.get_access_token(:oauth_verifier => verifier)
  @access_token, @access_secret = access_token.token, access_token.secret

end

#consumerObject

Get the current oAuth consumer instance



37
38
39
# File 'lib/freelancer/client.rb', line 37

def consumer
  @consumer ||= ::OAuth::Consumer.new(consumer_token, consumer_secret, { :site => @api_endpoint }.merge(consumer_options))
end

#request_token(options = {}) ⇒ Object

Get the current oAuth request token



48
49
50
# File 'lib/freelancer/client.rb', line 48

def request_token(options = {})
  @request_token_instance ||= consumer.get_request_token(options)
end

#set_callback_url(url) ⇒ Object

Set the callback URL for an oAuth request



42
43
44
45
# File 'lib/freelancer/client.rb', line 42

def set_callback_url(url)
  clear_request_token
  request_token :oauth_callback => url
end