Class: RubiconApiClient::RubiconClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rubicon-api-client.rb

Direct Known Subclasses

Seller

Constant Summary collapse

@@host =
'http://api.rubiconproject.com'

Instance Method Summary collapse

Constructor Details

#initialize(id, key, secret, format = :xml) ⇒ RubiconClient

Returns a new instance of RubiconClient.



18
19
20
21
22
23
# File 'lib/rubicon-api-client.rb', line 18

def initialize(id, key, secret, format=:xml)
  @id = id
  @key = key
  @secret = secret
  @response_format = response_formats[format]
end

Instance Method Details

#compose_arguments(hash) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rubicon-api-client.rb', line 45

def compose_arguments(hash)
  args = []
  hash.each_key do |key|
    hash[key] = [hash[key]] if !hash[key].is_a?(Array)
    args << "#{key}=#{hash[key].join(',')}" unless hash[key][0] == '' || hash[key][0].nil?
  end
  URI.escape '?'+args.join('&')
end

#execute(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubicon-api-client.rb', line 25

def execute(path)
  puts path

  uri = URI.parse @@host
  uri.user = @key
  uri.password = @secret

  net = Net::HTTP.new uri.host    
  req = Net::HTTP::Get.new path

  res = net.request req

  auth = Net::HTTP::DigestAuth.new.auth_header uri, res['www-authenticate'], 'GET'
  req = Net::HTTP::Get.new path
  req.add_field 'Authorization', auth
  req['Accept'] = @response_format
  res = net.request req
  res.read_body
end

#parse_date(date_range_splat) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubicon-api-client.rb', line 54

def parse_date(date_range_splat)
  args = {}
  if whens.include? date_range_splat[0].to_s
    args['when'] = date_range_splat[0].to_s
  elsif date_range_splat.length == 2
    args['start'] = Date.parse(date_range_splat[0].to_s).to_s
    args['end'] = Date.parse(date_range_splat[1].to_s).to_s
  end
  args
end

#response_formatsObject



13
14
15
# File 'lib/rubicon-api-client.rb', line 13

def response_formats
    { :xml => 'application/xml', :json => 'application/json', :csv => 'text/csv' }
end

#whensObject



9
10
11
# File 'lib/rubicon-api-client.rb', line 9

def whens
  ['today', 'yesterday', 'this week', 'last week', 'this month', 'last month', 'this year', 'last 7', 'last 30', 'all']
end