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.



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

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.



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

def on_authorize
  @on_authorize
end

#on_refresh_tokenObject

Returns the value of attribute on_refresh_token.



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

def on_refresh_token
  @on_refresh_token
end

#on_url_changedObject

Returns the value of attribute on_url_changed.



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

def on_url_changed
  @on_url_changed
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#authorize_url(options = {}) ⇒ Object



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

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



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

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

#delete(path) ⇒ Object



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

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

#exchange_token(options = {}) ⇒ Object



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
120
# File 'lib/eloqua_api/client.rb', line 85

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



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

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

#loginObject



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

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



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

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

#on_authorize?Boolean

Returns:

  • (Boolean)


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

def on_authorize?
  on_authorize.is_a? Proc
end

#on_refresh_token?Boolean

Returns:

  • (Boolean)


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

def on_refresh_token?
  on_refresh_token.is_a? Proc
end

#on_url_changed?Boolean

Returns:

  • (Boolean)


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

def on_url_changed?
  on_url_changed.is_a? Proc
end

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



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

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

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



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

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

#token_refreshed?Boolean

Returns:

  • (Boolean)


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

def token_refreshed?
  @token_refreshed
end

#urlObject



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

def url
  @opts[:url]
end

#url_changed?Boolean

Returns:

  • (Boolean)


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

def url_changed?
  @url_changed
end

#versionObject



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

def version
  @opts[:version]
end