Class: PPC::API::Baidu

Inherits:
Object
  • Object
show all
Defined in:
lib/ppc/api/baidu.rb,
lib/ppc/api/baidu/bulk.rb,
lib/ppc/api/baidu/plan.rb,
lib/ppc/api/baidu/group.rb,
lib/ppc/api/baidu/report.rb,
lib/ppc/api/baidu/account.rb,
lib/ppc/api/baidu/keyword.rb,
lib/ppc/api/baidu/creative.rb

Direct Known Subclasses

Account, Bulk, Creative, Group, Keyword, Plan, Report

Defined Under Namespace

Classes: Account, Bulk, Creative, Group, Keyword, Plan, Report

Class Method Summary collapse

Class Method Details

.make_type(params, map = @map) ⇒ Object

process



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ppc/api/baidu.rb', line 67

def self.make_type( params, map = @map)
  '''
  tranfesr ppc api to search engine api
  @ input
    params : list of hash complying with PPC gem api  
    map : list of pairs(lists) of symbol complying with following api
    map = [ 
                  [ ppc_key, api_key ],
                  [ ppc_key, api_key ],
                  [ ppc_key, api_key ],
                                  ...
                  ]
    Ex: 
    baidu_group_map = [ [ :id, :adgroupId],
                                            [ :price, :maxPrice],
                                                    ...                 ]
  ===================
  @ output:
  : types : list of hash that complying with search engine api
  '''
  params = [ params ] unless params.is_a? Array

  types = []
  params.each do |param|
    type = {}

      map.each do |key|
        value = param[ key[0] ]
        type[ key[1] ] = value if value
      end

    types << type
  end
  return types
end

.process(response, key, debug = false, &func) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ppc/api/baidu.rb', line 47

def self.process( response, key , debug = false ,  &func)
  '''
  Process Http response. If operation successes, return value of given keys.
  You can process the result using function &func, or do nothing by passing 
  block {|x|x}
  =========================== 
  @Output: resultType{ desc: boolean, failure: Array,  result: Array }
  failure is the failures part of response\'s header
  result is the processed response body.
  '''
  # 保留 debug 功能
  return response if debug

  result = {}
  result[:succ] = response['header']['desc']=='success'? true : false
  result[:failure] = response['header']['failures']
  result[:result] = func[ response['body'][key] ]
  return result
end

.request(auth, service, method, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ppc/api/baidu.rb', line 19

def self.request( auth, service, method, params = {} )
  '''
  request should return whole http response including header
  '''
  uri = URI("https://api.baidu.com/json/sms/v3/#{service}Service/#{method}")
  http_body = {
    header: {
      username:   auth[:username],
      password:   auth[:password],
      token:      auth[:token]
    },
    body: params
  }.to_json

  http_header = {
    'Content-Type' => 'application/json; charset=UTF-8'
  }

  http = Net::HTTP.new(uri.host, 443)
  http.set_debug_output $stderr
  http.use_ssl = true

  response = http.post(uri.path, http_body, http_header)
  response = JSON.parse response.body

  # return response if with_header else response['body']
end

.reverse_type(types, map = @map) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ppc/api/baidu.rb', line 103

def self.reverse_type( types, map = @map )
  '''
  transfer search engine api to ppc api
  @ input
    types : list of hash that complying with search engine api
    map : list of pairs(lists) of symbol, for more details please 
                read docs of make_type()
  ===================
  @ output:
    params : list of hash complying with PPC gem api  
  '''
  types = [ types ] unless types.is_a? Array

  params = []
  types.each do |type|
    param = {}
    
      map.each do |key|
          value = type[ key[1].to_s ]
          param[ key[0] ] = value if value
      end

    params << param
  end
  return params
end