Class: Eloqua::Client

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

Direct Known Subclasses

BulkClient, RESTClient

Constant Summary collapse

SITE =
'eloqua.com'
BASE_URI =
"https://secure.#{SITE}"
BASE_LOGIN_URI =
"https://login.#{SITE}"
BASE_VERSION =
'1.0'
BASE_PATH =
'/API/'
AUTHORIZE_PATH =
'/auth/oauth2/authorize'
TOKEN_PATH =
'/auth/oauth2/token'
TOKEN_PATH_HEADERS =
{
  'Accept'          => 'application/json, text/javascript, */*; q=0.01',
  'Accept-Language' => 'en-US,en;q=0.5',
  'Content-Type'    => 'application/x-www-form-urlencoded; charset=UTF-8'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



57
58
59
60
61
62
63
# File 'lib/eloqua_api/client.rb', line 57

def initialize(opts={})
  @opts = opts.is_a?(Hash) ? opts.dup : {}
  @opts[:url] ||= BASE_URI
  @opts[:version] ||= BASE_VERSION
  @url_changed = false
  @token_refreshed = false
end

Instance Attribute Details

#on_authorizeObject

Returns the value of attribute on_authorize.



53
54
55
# File 'lib/eloqua_api/client.rb', line 53

def on_authorize
  @on_authorize
end

#on_refresh_tokenObject

Returns the value of attribute on_refresh_token.



54
55
56
# File 'lib/eloqua_api/client.rb', line 54

def on_refresh_token
  @on_refresh_token
end

#on_url_changedObject

Returns the value of attribute on_url_changed.



55
56
57
# File 'lib/eloqua_api/client.rb', line 55

def on_url_changed
  @on_url_changed
end

#optsObject (readonly)

Returns the value of attribute opts.



51
52
53
# File 'lib/eloqua_api/client.rb', line 51

def opts
  @opts
end

Instance Method Details

#authorize_url(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eloqua_api/client.rb', line 69

def authorize_url(options={})
  query = {}
  query[:response_type] = 'code'
  query[:client_id]     = @opts[:client_id]
  query[:scope]         = options[:scope] || @opts[:scope] || 'full'

  if (state=(options[:state] || @opts[:state]))
    query[:state] = state
  end

  query[:redirect_uri]  = escape_uri(options[:redirect_uri] || @opts[:redirect_uri])

  "#{BASE_LOGIN_URI}#{AUTHORIZE_PATH}?#{query.map { |k,v| [k, v].join('=') }.join('&')}"
end

#build_path(*segments) ⇒ Object



145
146
147
# File 'lib/eloqua_api/client.rb', line 145

def build_path(*segments)
  File.join(BASE_PATH, *segments.shift, version, *segments)
end

#delete(path) ⇒ Object



183
184
185
# File 'lib/eloqua_api/client.rb', line 183

def delete(path)
  request(:delete, build_path(path))
end

#exchange_token(options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/eloqua_api/client.rb', line 84

def exchange_token(options={})
  auth = [@opts[:client_id], @opts[:client_secret]]

  body = {}
  if options[:code] and @opts[:redirect_uri]
    body[:grant_type]   = 'authorization_code'
    body[:code]         = options[:code]
    body[:redirect_uri] = escape_uri(@opts[:redirect_uri])
  elsif refresh_token?
    body[:grant_type]   = 'refresh_token'
    body[:refresh_token] = @opts[:refresh_token]
    body[:redirect_uri] = escape_uri(@opts[:redirect_uri])
  else
    raise ArgumentError, 'code and redirect_uri or refresh_token and redirect_uri is required'
  end

  result = http(BASE_LOGIN_URI, auth).post(TOKEN_PATH, :body => body, :headers => TOKEN_PATH_HEADERS)
  return result unless result.code == 200 and result.parsed_response.is_a? Hash

  response = result.parsed_response
  return result unless response['access_token'] and response['refresh_token']

  @opts[:access_token] = response['access_token']
  @opts[:refresh_token] = response['refresh_token']
    
  @http = nil
  @token_refreshed = true

  if refresh_token? and on_refresh_token?
    on_refresh_token.call(response)
  elsif options[:code] and @opts[:redirect_uri] and on_authorize?
    on_authorize.call(response)
  end

  result
end

#get(path, query = {}) ⇒ Object



167
168
169
# File 'lib/eloqua_api/client.rb', line 167

def get(path, query={})
  request(:get, build_path(path), :query => query)
end

#loginObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/eloqua_api/client.rb', line 149

def 
  @http = nil

  uri = BASE_URI
  result = http(BASE_LOGIN_URI).get('/id')
  if result.code == 200 and result.parsed_response.is_a? Hash
    uri = result.parsed_response["urls"]["base"]
  end

  @url_changed = (uri != @opts[:url])
  if @url_changed
    @opts[:url] = uri
    on_url_changed.call(uri) if on_url_changed?
  end

  result
end

#multipart_post(path, body = {}) ⇒ Object



175
176
177
# File 'lib/eloqua_api/client.rb', line 175

def multipart_post(path, body={})
  request(:post, build_path(path), :body => body)
end

#on_authorize?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/eloqua_api/client.rb', line 121

def on_authorize?
  on_authorize.is_a? Proc
end

#on_refresh_token?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/eloqua_api/client.rb', line 125

def on_refresh_token?
  on_refresh_token.is_a? Proc
end

#on_url_changed?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/eloqua_api/client.rb', line 129

def on_url_changed?
  on_url_changed.is_a? Proc
end

#post(path, body = {}) ⇒ Object



171
172
173
# File 'lib/eloqua_api/client.rb', line 171

def post(path, body={})
  request(:post, build_path(path), :body => body.to_json)
end

#put(path, body = {}) ⇒ Object



179
180
181
# File 'lib/eloqua_api/client.rb', line 179

def put(path, body={})
  request(:put, build_path(path), :body => body.to_json)
end

#token_refreshed?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/eloqua_api/client.rb', line 141

def token_refreshed?
  @token_refreshed
end

#urlObject



133
134
135
# File 'lib/eloqua_api/client.rb', line 133

def url
  @opts[:url]
end

#url_changed?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/eloqua_api/client.rb', line 137

def url_changed?
  @url_changed
end

#versionObject



65
66
67
# File 'lib/eloqua_api/client.rb', line 65

def version
  @opts[:version]
end