Class: PPC::API::Sm::Bulk

Inherits:
PPC::API::Sm show all
Defined in:
lib/ppc/api/sm/bulk.rb

Constant Summary collapse

Service =
'bulkJob'

Class Method Summary collapse

Methods inherited from PPC::API::Sm

process, request_uri

Methods included from PPC::API

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

Class Method Details

.do_download(auth, id) ⇒ Object



30
31
32
# File 'lib/ppc/api/sm/bulk.rb', line 30

def self.do_download(auth, id)
  request(auth, 'file', 'download',{fileId: id})
end

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

interface for operation #



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ppc/api/sm/bulk.rb', line 37

def self.download( auth, params = {} )
  """
  """
  begin
    result = get_all_object( auth, params )
    if result[:succ]
      task_id = result[:result]
    else
      raise "获取task id 失败"
    end

    puts "task_id: #{task_id}" if @debug

    loop do
      file_id = get_file_id( auth, task_id )[:result]
      if file_id.nil?
        sleep 15
        next
      end
      File.open("sm_#{file_id}.zip", "w") do |f|
        f.puts do_download(auth, file_id)
      end
      return
    end
  rescue => e
    p "Error encounter:#{e.to_s}"
  end  
end

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ppc/api/sm/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 = {
    bulkJobRequestType: {
      campaignIds:              plan_ids              || []      
    }
  }
  response = request( auth, Service, 'getAllObjects',options )
  process( response, 'taskId'){ |x| x }
end

.get_file_id(auth, id) ⇒ Object



24
25
26
27
28
# File 'lib/ppc/api/sm/bulk.rb', line 24

def self.get_file_id( auth, id)
  raise "empty id" if id.nil?
  response = request(auth, 'task', 'getTaskState',{taskId: id})
  process( response, 'fileId'){ |x| x }
end