Module: PPC::Operation

Included in:
Account, Creative, Group, Keyword, Plan
Defined in:
lib/ppc/operation.rb,
lib/ppc/operation/plan.rb,
lib/ppc/operation/group.rb,
lib/ppc/operation/report.rb,
lib/ppc/operation/account.rb,
lib/ppc/operation/keyword.rb,
lib/ppc/operation/creative.rb

Defined Under Namespace

Modules: Creative_operation, Group_operation, Keyword_operation, Plan_operation, Report Classes: Account, Creative, Group, Keyword, Plan

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/ppc/operation.rb', line 4

def id
  @id
end

Instance Method Details

#call(service) ⇒ Object



58
59
60
# File 'lib/ppc/operation.rb', line 58

def call(service)
  eval "::PPC::API::#{@se.capitalize}::#{service.capitalize}"
end

#download(param = nil) ⇒ Object



51
52
53
54
55
56
# File 'lib/ppc/operation.rb', line 51

def download( param = nil )
    """
    download all objs of an account
    """
    eval("::PPC::API::#{@se.capitalize}::Bulk").download( @auth, param )
end

#get_obj(ids, service) ⇒ Object

helper fucntion



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ppc/operation.rb', line 67

def get_obj( ids, service )
    '''
    Return service object. 
    Providing single id, return single object.
    Providing multiple ids, return Array of objects
    '''
    class_obj =  eval "::PPC::Opeartion::#{service.capitalize}"
    param = @auth
    param[:se] = @se
    objs = []

    ids.each do |id|
      param[:id] = id
      objs << class_obj.new( param )
    end

    return objs.length == 1 ? objs[0] : objs 
end

#initialize(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ppc/operation.rb', line 6

def initialize( params )
  
  if params[:se] == nil
    raise 'please specific a search engine'
  else 
    @se = params[:se]
  end
  @id = params[:id]
  @auth = {
    username: params[:username],
    password: params[:password],
    # 在qihu360的api中,apikey就是auth[:token]
    token:    params[:token],
    target:   params[:target]
  }
  # add support for qihu360
  if @se == 'qihu'
    raise "you are using qihu service, please enter api_key" if params[:api_key].nil?
    @auth[:api_key] = params[:api_key]
  end
  if @se == 'qihu' && params[:token].nil? 
    raise "you are using qihu service, please enter api_secret" if params[:api_secret].nil?
    @auth[:api_secret] = params[:api_secret]
    @auth[:token] = qihu_refresh_token
  end
end

#qihu_refresh_tokenObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ppc/operation.rb', line 33

def qihu_refresh_token
    cipher_aes = OpenSSL::Cipher::AES.new(128, :CBC)
    cipher_aes.encrypt
    cipher_aes.key = @auth[:api_secret][0,16]
    cipher_aes.iv = @auth[:api_secret][16,16]
    encrypted = (cipher_aes.update(Digest::MD5.hexdigest(@auth[:password])) + cipher_aes.final).unpack('H*').join
    url = "https://api.e.360.cn/account/clientLogin"
    response = HTTParty.post(url,
      :body => {
      :username => @auth[:username],
      :passwd => encrypted[0,64]
      },
      :headers => {'apiKey' => @auth[:api_key] }
    )
    data = response.parsed_response
    data["account_clientLogin_response"]["accessToken"]
end