Class: AppnexusApi::Connection
- Inherits:
-
Object
- Object
- AppnexusApi::Connection
- Defined in:
- lib/appnexusapi/connection.rb
Instance Method Summary collapse
- #delete(route, body = nil, headers = {}) ⇒ Object
- #get(route, params = {}, headers = {}) ⇒ Object
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #is_authorized? ⇒ Boolean
- #login ⇒ Object
- #logout ⇒ Object
- #post(route, body = nil, headers = {}) ⇒ Object
- #put(route, body = nil, headers = {}) ⇒ Object
- #run_request(method, route, body, headers) ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/appnexusapi/connection.rb', line 6 def initialize(config) config["uri"] = "http://api.adnxs.com/" unless config.has_key?("uri") @config = config @connection = Faraday::Connection.new(:url => config["uri"]) do |builder| builder.use AppnexusApi::Faraday::Request::JsonEncode builder.use AppnexusApi::Faraday::Response::RaiseHttpError builder.use AppnexusApi::Faraday::Response::ParseJson builder.adapter Faraday.default_adapter end end |
Instance Method Details
#delete(route, body = nil, headers = {}) ⇒ Object
43 44 45 |
# File 'lib/appnexusapi/connection.rb', line 43 def delete(route, body=nil, headers={}) run_request(:delete, route, body, headers).body end |
#get(route, params = {}, headers = {}) ⇒ Object
30 31 32 33 |
# File 'lib/appnexusapi/connection.rb', line 30 def get(route, params={}, headers={}) params = params.delete_if {|key, value| value.nil? } run_request(:get, @connection.build_url(route, params), nil, headers).body end |
#is_authorized? ⇒ Boolean
17 18 19 |
# File 'lib/appnexusapi/connection.rb', line 17 def !@token.nil? end |
#login ⇒ Object
21 22 23 24 |
# File 'lib/appnexusapi/connection.rb', line 21 def login response = @connection.run_request(:post, 'auth', { "auth" => { "username" => @config["username"], "password" => @config["password"] } }, {}) @token = response.body["token"] end |
#logout ⇒ Object
26 27 28 |
# File 'lib/appnexusapi/connection.rb', line 26 def logout @token = nil end |
#post(route, body = nil, headers = {}) ⇒ Object
35 36 37 |
# File 'lib/appnexusapi/connection.rb', line 35 def post(route, body=nil, headers={}) run_request(:post, route, body, headers).body end |
#put(route, body = nil, headers = {}) ⇒ Object
39 40 41 |
# File 'lib/appnexusapi/connection.rb', line 39 def put(route, body=nil, headers={}) run_request(:put, route, body, headers).body end |
#run_request(method, route, body, headers) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/appnexusapi/connection.rb', line 47 def run_request(method, route, body, headers) login if ! begin @connection.run_request(method, route, body, { "Authorization" => @token }.merge(headers)) rescue AppnexusApi::Unauthorized => e if @retry == true raise AppnexusApi::Unauthorized, e else @retry = true logout run_request(method, route, body, headers) end rescue Faraday::Error::TimeoutError => e raise AppnexusApi::Timeout, "Timeout" ensure @retry = false end end |