Module: PPC::API

Included in:
Baidu, Qihu, Sm, Sogou
Defined in:
lib/ppc/api.rb,
lib/ppc/api/sm.rb,
lib/ppc/api/qihu.rb,
lib/ppc/api/baidu.rb,
lib/ppc/api/sogou.rb,
lib/ppc/api/shenma.rb,
lib/ppc/api/sm/bulk.rb,
lib/ppc/api/sm/plan.rb,
lib/ppc/api/sm/group.rb,
lib/ppc/api/qihu/bulk.rb,
lib/ppc/api/qihu/plan.rb,
lib/ppc/api/qihu/rank.rb,
lib/ppc/api/sm/report.rb,
lib/ppc/api/baidu/bulk.rb,
lib/ppc/api/baidu/plan.rb,
lib/ppc/api/baidu/rank.rb,
lib/ppc/api/qihu/group.rb,
lib/ppc/api/sm/account.rb,
lib/ppc/api/sm/keyword.rb,
lib/ppc/api/sogou/bulk.rb,
lib/ppc/api/sogou/plan.rb,
lib/ppc/api/baidu/group.rb,
lib/ppc/api/qihu/report.rb,
lib/ppc/api/sm/creative.rb,
lib/ppc/api/sogou/group.rb,
lib/ppc/api/baidu/report.rb,
lib/ppc/api/qihu/account.rb,
lib/ppc/api/qihu/keyword.rb,
lib/ppc/api/qihu/sublink.rb,
lib/ppc/api/sogou/report.rb,
lib/ppc/api/baidu/account.rb,
lib/ppc/api/baidu/keyword.rb,
lib/ppc/api/qihu/creative.rb,
lib/ppc/api/shenma/report.rb,
lib/ppc/api/sogou/account.rb,
lib/ppc/api/sogou/keyword.rb,
lib/ppc/api/baidu/creative.rb,
lib/ppc/api/sogou/creative.rb,
lib/ppc/api/sm/phone_new_creative.rb,
lib/ppc/api/baidu/phone_new_creative.rb

Defined Under Namespace

Classes: Baidu, Qihu, Shenma, Sm, Sogou

Instance Method Summary collapse

Instance Method Details

#debug_offObject



23
24
25
# File 'lib/ppc/api.rb', line 23

def debug_off
  @debug = false
end

#debug_onObject



19
20
21
# File 'lib/ppc/api.rb', line 19

def debug_on
  @debug = true
end

#is_no_quota(failure, code) ⇒ Object



90
91
92
93
94
95
# File 'lib/ppc/api.rb', line 90

def is_no_quota(failure, code)
  return false if failure.nil?
  failure = [failure].flatten
  return false if failure.size.zero?
  return failure[0]["code"] == code
end

#make_type(params, maps = @map) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ppc/api.rb', line 97

def make_type( params, maps = @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
  params.map do |item| 
    item.select!{|key| maps.map{|m| m[0]}.include? key}
    maps.each{|m| item.filter_and_replace_key(m[1],m[0])}
    item
  end
end

#process(response, key, &func) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ppc/api.rb', line 76

def process( response, key, &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.
  '''
  raise 'you need build the response result'
end

#request(auth, service, method, params = {}, http_method = 'post') ⇒ Object



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
73
74
# File 'lib/ppc/api.rb', line 47

def request(auth, service, method, params = {}, http_method = 'post')
  '''
    request should return whole http response including header
  '''
  uri = request_uri(service: service, method: method)
  http_body = request_http_body(auth, params)
  http_header = request_http_header(auth)

  # set request proxy
  if ENV["PROXY_HOST"]
    proxy_port = ENV["PROXY_PORT"] ? ENV["PROXY_PORT"].to_i : 80
    http = Net::HTTP.new(uri.host, 443, ENV["PROXY_HOST"], proxy_port)
  else
    http = Net::HTTP.new(uri.host, 443)
  end

  # 是否显示http通信输出
  http.set_debug_output( $stdout ) if @debug
  http.use_ssl = true
  if http_method == 'delete'
    req = Net::HTTP::Delete.new(uri.path, http_header)
    req.body = http_body
    response = http.request req
  else
    response = http.post(uri.path, http_body, http_header)
  end
  begin JSON.parse(response.body) rescue response.body end
end

#request_http_body(auth, params = {}) ⇒ Object



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

def request_http_body(auth, params = {})
  {
    header: {
      username:   auth[:username],
      password:   auth[:password],
      token:      auth[:token],
      target:     auth[:target]
    },
    body: params
  }.to_json
end

#request_http_header(auth) ⇒ Object



43
44
45
# File 'lib/ppc/api.rb', line 43

def request_http_header(auth)
  {'Content-Type' => 'application/json; charset=UTF-8'}
end

#request_uri(param = {}) ⇒ Object



27
28
29
# File 'lib/ppc/api.rb', line 27

def request_uri(param = {})
  raise 'you need build the uri'
end

#reverse_type(types, maps = @map) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ppc/api.rb', line 125

def reverse_type( types, maps = @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
  types.map do |item| 
    maps.each{|m| item.filter_and_replace_key(m[0],m[1])}
    item
  end
end