Class: Odesk::Api::Client

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

Overview

Client for accessing API

Constant Summary collapse

DATA_FORMAT =
'json'
OVERLOAD_VAR =
"http_method"
URI_AUTH =
"/services/api/auth"
URI_RTOKEN =
"/auth/v1/oauth/token/request"
URI_ATOKEN =
"/auth/v1/oauth/token/access"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Init client

Arguments:

config: (Config)


36
37
38
39
40
41
# File 'lib/odesk/api/client.rb', line 36

def initialize(config)
  $LOG.i('initializing client')
  @config = config
  @epoint = Odesk::Api::DEFAULT_EPOINT
  @url_auth, @url_rtoken, @url_atoken = Odesk::Api::BASE_HOST + URI_AUTH, self.full_url(URI_RTOKEN), self.full_url(URI_ATOKEN)
end

Instance Attribute Details

#epointObject

Returns the value of attribute epoint.



29
30
31
# File 'lib/odesk/api/client.rb', line 29

def epoint
  @epoint
end

#url_atokenObject (readonly)

Returns the value of attribute url_atoken.



30
31
32
# File 'lib/odesk/api/client.rb', line 30

def url_atoken
  @url_atoken
end

#url_authObject (readonly)

Returns the value of attribute url_auth.



30
31
32
# File 'lib/odesk/api/client.rb', line 30

def url_auth
  @url_auth
end

#url_rtokenObject (readonly)

Returns the value of attribute url_rtoken.



30
31
32
# File 'lib/odesk/api/client.rb', line 30

def url_rtoken
  @url_rtoken
end

Instance Method Details

#delete(uri, params = {}) ⇒ Object

Run DELETE request

Arguments:

uri: (String)
param: (Hash)


109
110
111
# File 'lib/odesk/api/client.rb', line 109

def delete(uri, params = {})
  send_request(uri, :delete, params)
end

#full_url(uri) ⇒ Object

Get full URL



114
115
116
# File 'lib/odesk/api/client.rb', line 114

def full_url(uri) # :nodoc:
  Odesk::Api::BASE_HOST + '/' + (@epoint) + get_uri_with_format(uri);
end

#get(uri, params = {}) ⇒ Object

Run GET request

Arguments:

uri: (String)
param: (Hash)


82
83
84
# File 'lib/odesk/api/client.rb', line 82

def get(uri, params = {})
  send_request(uri, :get, params)
end

#get_access_token(verifier) ⇒ Object

Finish auth process and get access token

Arguments:

verifier: (String)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/odesk/api/client.rb', line 65

def get_access_token(verifier)
  $LOG.i "getting access token pair"
  @access_token = @request_token.get_access_token(:oauth_verifier => verifier)
  $LOG.i "got access token pair", @access_token
  $LOG.i "save access token data in config object"
  
  @config.access_token  = @access_token.token
  @config.access_secret = @access_token.secret
  
  @access_token
end

#get_authorization_urlObject

Start auth process and get authorization token



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/odesk/api/client.rb', line 44

def get_authorization_url
  $LOG.i "requesting autorization token"
  @consumer=OAuth::Consumer.new @config.consumer_key, 
                                @config.consumer_secret, 
                                {:site                => Odesk::Api::BASE_HOST,
                                 :request_token_path  => '/' + Odesk::Api::DEFAULT_EPOINT + URI_RTOKEN,
                                 :access_token_path   => '/' + Odesk::Api::DEFAULT_EPOINT + URI_ATOKEN,
                                 :authorize_path      => URI_AUTH,
                                 :signature_method    => @config.signature_method}
  
  @request_token = @consumer.get_request_token
  $LOG.i "got request token pair", @request_token
  
  $LOG.i "building authorization url, which is", @request_token.authorize_url
  @request_token.authorize_url
end

#get_uri_with_format(uri) ⇒ Object

Get URI with :format



119
120
121
# File 'lib/odesk/api/client.rb', line 119

def get_uri_with_format(uri) # :nodoc:
  uri + ((@epoint == 'api') ? '.' + DATA_FORMAT : '')
end

#post(uri, params = {}) ⇒ Object

Run POST request

Arguments:

uri: (String)
param: (Hash)


91
92
93
# File 'lib/odesk/api/client.rb', line 91

def post(uri, params = {})
  send_request(uri, :post, params)
end

#put(uri, params = {}) ⇒ Object

Run PUT request

Arguments:

uri: (String)
param: (Hash)


100
101
102
# File 'lib/odesk/api/client.rb', line 100

def put(uri, params = {})
  send_request(uri, :put, params)
end