Class: AppnexusApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/appnexusapi/connection.rb

Constant Summary collapse

RATE_EXCEEDED_DEFAULT_TIMEOUT =
15
RATE_LIMIT_WAITER =
Proc.new do |exception, try, elapsed_time, next_interval|
  if (match = /Retry after (\d+)s/.match(exception.message))
    seconds = match[1].to_i
  else
    seconds = RATE_EXCEEDED_DEFAULT_TIMEOUT
  end
  seconds += RATE_EXCEEDED_DEFAULT_TIMEOUT # Just to be sure we waited long enough
  AppnexusApi.config.logger.warn("WARN: Rate limit exceeded; retrying after #{seconds}s...")
  sleep(seconds)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/appnexusapi/connection.rb', line 20

def initialize(config)
  @config = config
  @config['uri'] ||= 'https://api.appnexus.com/'
  @logger = @config['logger'] || AppnexusApi.config.logger
  @connection = ::Faraday.new(@config['uri']) do |conn|
    conn.response :logger, @logger, bodies: true if AppnexusApi.config.api_debug
    conn.request :json
    conn.response :json, :content_type => /\bjson$/
    conn.use AppnexusApi::Faraday::Response::RaiseHttpError
    conn.adapter ::Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/appnexusapi/connection.rb', line 18

def connection
  @connection
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/appnexusapi/connection.rb', line 18

def logger
  @logger
end

Instance Method Details

#build_url(route, params) ⇒ Object



53
54
55
# File 'lib/appnexusapi/connection.rb', line 53

def build_url(route, params)
  @connection.build_url(route, params)
end

#delete(route, body = nil, headers = {}) ⇒ Object



65
66
67
# File 'lib/appnexusapi/connection.rb', line 65

def delete(route, body=nil, headers={})
  run_request(:delete, route, body, headers)
end

#get(route, params = {}, headers = {}) ⇒ Object



48
49
50
51
# File 'lib/appnexusapi/connection.rb', line 48

def get(route, params={}, headers={})
  params = params.delete_if { |_, v| v.nil? }
  run_request(:get, build_url(route, params), nil, headers)
end

#loginObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/appnexusapi/connection.rb', line 33

def 
  response = @connection.run_request(
    :post,
    'auth',
    { 'auth' => { 'username' => @config['username'], 'password' => @config['password'] } },
    {}
  )

  if response.body['response']['error_code']
    fail "#{response.body['response']['error_code']}/#{response.body['response']['error_description']}"
  end

  @token = response.body['response']['token']
end

#post(route, body = nil, headers = {}) ⇒ Object



57
58
59
# File 'lib/appnexusapi/connection.rb', line 57

def post(route, body=nil, headers={})
  run_request(:post, route, body, headers)
end

#put(route, body = nil, headers = {}) ⇒ Object



61
62
63
# File 'lib/appnexusapi/connection.rb', line 61

def put(route, body=nil, headers={})
  run_request(:put, route, body, headers)
end