class Eloqua::Client

Constants

AUTHORIZE_PATH
BASE_LOGIN_URI
BASE_PATH
BASE_URI
BASE_VERSION
SITE
TOKEN_PATH
TOKEN_PATH_HEADERS

Attributes

on_authorize[RW]
on_refresh_token[RW]
on_url_changed[RW]
opts[R]

Public Class Methods

new(opts={}) click to toggle source
# 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

Public Instance Methods

authorize_url(options={}) click to toggle source
# 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) click to toggle source
# File lib/eloqua_api/client.rb, line 146
def build_path(*segments)
  File.join(BASE_PATH, *segments.shift, version, *segments)
end
delete(path) click to toggle source
# File lib/eloqua_api/client.rb, line 184
def delete(path)
  request(:delete, build_path(path))
end
exchange_token(options={}) click to toggle source
# 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={}) click to toggle source
# File lib/eloqua_api/client.rb, line 168
def get(path, query={})
  request(:get, build_path(path), :query => query)
end
login() click to toggle source
# File lib/eloqua_api/client.rb, line 150
def login
  @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={}) click to toggle source
# File lib/eloqua_api/client.rb, line 176
def multipart_post(path, body={})
  request(:post, build_path(path), :body => body)
end
on_authorize?() click to toggle source
# File lib/eloqua_api/client.rb, line 122
def on_authorize?
  on_authorize.is_a? Proc
end
on_refresh_token?() click to toggle source
# File lib/eloqua_api/client.rb, line 126
def on_refresh_token?
  on_refresh_token.is_a? Proc
end
on_url_changed?() click to toggle source
# File lib/eloqua_api/client.rb, line 130
def on_url_changed?
  on_url_changed.is_a? Proc
end
post(path, body={}) click to toggle source
# 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={}) click to toggle source
# File lib/eloqua_api/client.rb, line 180
def put(path, body={})
  request(:put, build_path(path), :body => body.to_json)
end
token_refreshed?() click to toggle source
# File lib/eloqua_api/client.rb, line 142
def token_refreshed?
  @token_refreshed
end
url() click to toggle source
# File lib/eloqua_api/client.rb, line 134
def url
  @opts[:url]
end
url_changed?() click to toggle source
# File lib/eloqua_api/client.rb, line 138
def url_changed?
  @url_changed
end
version() click to toggle source
# File lib/eloqua_api/client.rb, line 66
def version
  @opts[:version]
end

Protected Instance Methods

escape_uri(url) click to toggle source
# File lib/eloqua_api/client.rb, line 195
def escape_uri(url)
  URI.escape(URI.unescape(url))
end
http(url=nil, auth=nil) click to toggle source
# File lib/eloqua_api/client.rb, line 216
def http(url=nil, auth=nil)
  url ||= @opts[:url]
  auth ||= begin
    if @opts[:access_token]
      @opts[:access_token]
    elsif (site=@opts[:site]) and (username=@opts[:username]) and (password=@opts[:password])
      ["%s\\%s" % [site, username], password]
    else
      nil
    end
  end
  timeout ||= @opts[:timeout]

  Class.new(HTTPClient) do |klass|
    klass.base_uri(url.to_s) if url
    klass.default_timeout(timeout) if timeout

    if auth.is_a?(String) and auth.size > 0
      klass.headers("Authorization" => "Bearer %s" % auth)
    elsif auth.is_a?(Array) and auth.size == 2
      klass.basic_auth(*auth)
    end
  end
end
refresh_token?() click to toggle source
# File lib/eloqua_api/client.rb, line 190
def refresh_token?
  @opts[:refresh_token] and 
  @opts[:redirect_uri]
end
request(method, path, params={}, login_fallback=true) click to toggle source
# File lib/eloqua_api/client.rb, line 199
def request(method, path, params={}, login_fallback=true)
  @http ||= http

  result = @http.send(method, path, params)
  if result.code == 401 and login_fallback
    exchange_token if refresh_token?
    
    if login
      request(method, path, params, false)
    else
      result
    end
  else
    result
  end
end