Class: Eloqua::Client
- Inherits:
-
Object
- Object
- Eloqua::Client
- Defined in:
- lib/eloqua_api/client.rb
Direct Known Subclasses
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
-
#on_authorize ⇒ Object
Returns the value of attribute on_authorize.
-
#on_refresh_token ⇒ Object
Returns the value of attribute on_refresh_token.
-
#on_url_changed ⇒ Object
Returns the value of attribute on_url_changed.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #authorize_url(options = {}) ⇒ Object
- #build_path(*segments) ⇒ Object
- #delete(path) ⇒ Object
- #exchange_token(options = {}) ⇒ Object
- #get(path, query = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #login ⇒ Object
- #multipart_post(path, body = {}) ⇒ Object
- #on_authorize? ⇒ Boolean
- #on_refresh_token? ⇒ Boolean
- #on_url_changed? ⇒ Boolean
- #post(path, body = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
- #token_refreshed? ⇒ Boolean
- #url ⇒ Object
- #url_changed? ⇒ Boolean
- #version ⇒ Object
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_authorize ⇒ Object
Returns the value of attribute on_authorize.
54 55 56 |
# File 'lib/eloqua_api/client.rb', line 54 def @on_authorize end |
#on_refresh_token ⇒ Object
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_changed ⇒ Object
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 |
#opts ⇒ Object (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 (={}) query = {} query[:response_type] = 'code' query[:client_id] = @opts[:client_id] query[:scope] = [:scope] || @opts[:scope] || 'full' if (state=([:state] || @opts[:state])) query[:state] = state end query[:redirect_uri] = escape_uri([: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(={}) auth = [@opts[:client_id], @opts[:client_secret]] body = {} if [:code] and @opts[:redirect_uri] body[:grant_type] = 'authorization_code' body[:code] = [: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 [:code] and @opts[:redirect_uri] and .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 |
#login ⇒ Object
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 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 = {}) ⇒ 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
122 123 124 |
# File 'lib/eloqua_api/client.rb', line 122 def .is_a? Proc end |
#on_refresh_token? ⇒ 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
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
142 143 144 |
# File 'lib/eloqua_api/client.rb', line 142 def token_refreshed? @token_refreshed end |
#url ⇒ Object
134 135 136 |
# File 'lib/eloqua_api/client.rb', line 134 def url @opts[:url] end |
#url_changed? ⇒ Boolean
138 139 140 |
# File 'lib/eloqua_api/client.rb', line 138 def url_changed? @url_changed end |
#version ⇒ Object
66 67 68 |
# File 'lib/eloqua_api/client.rb', line 66 def version @opts[:version] end |