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.
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_authorize ⇒ Object
Returns the value of attribute on_authorize.
53 54 55 |
# File 'lib/eloqua_api/client.rb', line 53 def @on_authorize end |
#on_refresh_token ⇒ Object
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_changed ⇒ Object
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 |
#opts ⇒ Object (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 (={}) 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
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(={}) 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
167 168 169 |
# File 'lib/eloqua_api/client.rb', line 167 def get(path, query={}) request(:get, build_path(path), :query => query) end |
#login ⇒ Object
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 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
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
121 122 123 |
# File 'lib/eloqua_api/client.rb', line 121 def .is_a? Proc end |
#on_refresh_token? ⇒ 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
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
141 142 143 |
# File 'lib/eloqua_api/client.rb', line 141 def token_refreshed? @token_refreshed end |
#url ⇒ Object
133 134 135 |
# File 'lib/eloqua_api/client.rb', line 133 def url @opts[:url] end |
#url_changed? ⇒ Boolean
137 138 139 |
# File 'lib/eloqua_api/client.rb', line 137 def url_changed? @url_changed end |
#version ⇒ Object
65 66 67 |
# File 'lib/eloqua_api/client.rb', line 65 def version @opts[:version] end |