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/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/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, Sm, Sogou

Instance Method Summary collapse

Instance Method Details

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ppc/api.rb', line 83

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 ].flatten.map do |item| 
    item.select!{|key| maps.any?{|m| m[0] == key} }
    maps.each{|key_new, key_old| item[key_old] = (key_new == :match_type && @match_types ? @match_types[item.delete(key_new)] : item.delete(key_new)) if item[key_new] }
    item
  end
end

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



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ppc/api.rb', line 69

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



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
65
66
67
# File 'lib/ppc/api.rb', line 39

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 ENV["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
  qihu_quota = begin response.to_hash["quotaremain"][0].to_i rescue nil end
  begin JSON.parse(response.body).merge(qihu_quota: qihu_quota) rescue response.body end
end

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ppc/api.rb', line 23

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



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

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

#request_uri(param = {}) ⇒ Object



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

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

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ppc/api.rb', line 110

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 ].flatten.map do |item| 
    maps.each{|key_new, key_old| 
      value = item.delete(key_old)
      value = item.delete(key_old.to_s) if value.nil?
      next if value.nil?
      if key_new == :pause && (key_old == :status || key_old == :sysStatus)
        if value[/(pause|enable)/]
          value = value[/pause/] ? true : false
        else
          key_new = :status
        end
      end
      item[key_new.to_sym] = (key_new == :match_type && @match_types ? @match_types[value] : value)
    }
    item
  end
end