Class: AdMobApi::Client

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/admob-api/client.rb

Constant Summary

Constants included from Validator

Validator::AUTH_ERROR_MSG, Validator::UNAUTHORIZED_ERROR_MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validator

#validate

Constructor Details

#initialize(&block) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
39
40
41
# File 'lib/admob-api/client.rb', line 32

def initialize(&block)
  block.call(self)

  @agent = HTTPClient.new
  res = post('/v2/auth/login', {
    :email    => @email,
    :password => @password
  })
  @token = res['token']
end

Instance Attribute Details

#client_key=(value) ⇒ Object (writeonly)

Sets the attribute client_key

Parameters:

  • value

    the value to set the attribute client_key to.



10
11
12
# File 'lib/admob-api/client.rb', line 10

def client_key=(value)
  @client_key = value
end

#email=(value) ⇒ Object (writeonly)

Sets the attribute email

Parameters:

  • value

    the value to set the attribute email to.



10
11
12
# File 'lib/admob-api/client.rb', line 10

def email=(value)
  @email = value
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



10
11
12
# File 'lib/admob-api/client.rb', line 10

def password=(value)
  @password = value
end

Instance Method Details

#logoutObject



43
44
45
46
# File 'lib/admob-api/client.rb', line 43

def logout
  post('/v2/auth/logout')
  @client_key, @email, @password, @token = nil
end

#sites(include_deleted = false) ⇒ Object



48
49
50
51
52
# File 'lib/admob-api/client.rb', line 48

def sites(include_deleted = false)
  params = include_deleted ? {:include_deleted => 1} : {}
  res    = get('/v2/site/search', params)
  res.map {|s| AdMobApi::Site.new(s) }
end

#stats(site_id, date_range = :today, opt = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/admob-api/client.rb', line 54

def stats(site_id, date_range = :today, opt = {})
  if date_range.is_a?(Symbol)
    date_range = AdMobApi::DateRange.send(date_range)
  end
  site_id_key = site_id.is_a?(Array) ? 'site_id[]' : :site_id
  params = {
    site_id_key => site_id,
    :start_date => date_range.begin,
    :end_date   => date_range.end
  }
  params[:object_dimention] = opt[:object_dimention] unless opt[:object_dimention].nil?
  params[:time_dimention]   = opt[:time_dimention] unless opt[:time_dimention].nil?
  unless opt[:order_by].nil?
    params["order_by[#{opt[:order_by]}]"] = opt[:order] || :desc       
  end
  res = get('/v2/site/stats', params)
  AdMobApi::Stats.new(res.first)
end