Class: PPC::API::Baidu::Bulk

Inherits:
PPC::API::Baidu show all
Defined in:
lib/ppc/api/baidu/bulk.rb

Constant Summary collapse

Service =
'BulkJob'

Class Method Summary collapse

Methods inherited from PPC::API::Baidu

process, request_uri

Methods included from PPC::API

#make_type, #process, #request, #request_http_body, #request_http_header, #request_uri, #reverse_type

Class Method Details

.download(auth, params = {}) ⇒ Object

interface for operation #



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ppc/api/baidu/bulk.rb', line 43

def self.download( auth, params = {} )
  """
  """
  params[:extended] = params[:extended] || 2
  begin
    file_id = get_all_object( auth, params )
    if file_id[:succ]
      file_id = file_id[:result]
    else
      raise file_id[:failure][0]['message']
    end

    puts "file_id: #{file_id}" if ENV["DEBUG"]

    loop do
      state = state( auth, file_id )[:result].to_s
      raise "invalid file state: #{state}" unless %w(1 2 3 null).include? state
      break if state == '3'
      puts "waiting for #{file_id} to be ready. current state:#{state}" if ENV["DEBUG"]
      sleep 3
    end

    puts "#{file_id} is ready" if ENV["DEBUG"]
    return path( auth, file_id )

  rescue => e
    p "Error encounter:#{e.to_s}"
  end  
  false
end

.get_all_object(auth, params = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ppc/api/baidu/bulk.rb', line 7

def self.get_all_object( auth,params = {})
  
  plan_ids = params[:plan_ids]

  unless plan_ids.nil?
    plan_ids = plan_ids.class == Array ? plan_ids : [plan_ids]
  end

  options = {
    campaignIds:              plan_ids              || []      ,
    includeQuality:           params[:quality]      || true    ,
    includeTemp:              params[:temp]         || false   ,
    format:                   params[:format]       || 1       ,
    newCreativeFiles:         params[:adcopy]       || 0       ,
    includeTempNewCreatives:  params[:temp_adcopy]  || 0       ,
    includePhraseType:        params[:phrase]       || 0       ,
    extended:                 params[:extended]     || 0
  }
  response = request( auth, Service, 'getAllObjects',options )
  process( response, 'fileId'){ |x| x }
end

.path(auth, id) ⇒ Object



35
36
37
38
# File 'lib/ppc/api/baidu/bulk.rb', line 35

def self.path( auth, id)
  response = request( auth, Service, 'getFilePath',{fileId:id})
  process( response, 'filePaths' ){ |x| x }       
end

.state(auth, id) ⇒ Object



29
30
31
32
33
# File 'lib/ppc/api/baidu/bulk.rb', line 29

def self.state( auth, id)
  raise "empty id" if id.nil? or id.empty?
  response = request(auth, Service, 'getFileState',{fileId:id})
  process( response, 'isGenerated'){ |x| x }
end