Class: Sogou::SEM::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sogou/sem/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth) ⇒ Base

Returns a new instance of Base.



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
32
33
34
35
36
# File 'lib/sogou/sem/base.rb', line 7

def initialize(auth)
  classname = self.class.name.split('::').last
  @service_name =
    case classname
      when 'CampaignService'
        'CpcPlanService'
      when 'AccountService'
        'AccountService'
      when 'AdgroupService'
        'CpcGrpService'
      when 'KeywordService'
        'CpcService'
      when 'CreativeService'
        'CpcIdeaService'
      when 'ReportService'
        'ReportService'
      when 'DownloadService'
        'AccountDownloadService'
      when 'KRService'
        'KRService'
      end

  @port_name = @service_name
  @username = auth.username
  @password = auth.password
  @token = auth.token
  @client = Savon.new("http://api.agent.sogou.com:8080/sem/sms/v1/#{@service_name}?wsdl")

  # Savon.new(@base_uri+service_name+'?wsdl')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Savon.new(@base_uri+service_name+‘?wsdl’)



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
# File 'lib/sogou/sem/base.rb', line 37

def method_missing(name, *args, &block)
  options,debug = args[0],args[1]
  options = {} if options.nil?
  name = name.to_s
  name_snake = name.snake_case
  # p name


  name_request_sym = (name +'Request').to_sym #if %w(getCampaignByCampaignId getAllCampaign addCampaign updateCampaign deleteCampaign).include?name
  # puts name_request_sym
  name_response_sym = (name+'Response').snake_case.to_sym
  operation = make_operation(name)
  operation.header = operation_header
  operation.body = {
    name_request_sym => options
  }
  ap operation.body if debug
  puts operation.build if debug
  response = operation.call
  ap response if debug
  ap response.failures if debug
  # if response.failures
  #   raise response.failures.to_s
  # else
    Sogou::Response.new(response,name_response_sym)
  # end
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/sogou/sem/base.rb', line 5

def debug
  @debug
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/sogou/sem/base.rb', line 4

def password
  @password
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/sogou/sem/base.rb', line 4

def token
  @token
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/sogou/sem/base.rb', line 4

def username
  @username
end

Instance Method Details

#example(operation, with_header = false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sogou/sem/base.rb', line 83

def example(operation,with_header=false)
  operation = make_operation(operation)
  if with_header
  {
    :example_header => operation.example_header,
    :example_body => operation.example_body
  }
  else
    operation.example_body
  end
end

#make_operation(operation_name) ⇒ Object



68
69
70
# File 'lib/sogou/sem/base.rb', line 68

def make_operation(operation_name)
  @client.operation(@service_name,@port_name,operation_name)
end

#operation_headerObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/sogou/sem/base.rb', line 72

def operation_header
 {
    :AuthHeader=>
    {
      :username=>@username,
      :password=>@password,
      :token=>@token
    }
  }
end

#operationsObject

end



64
65
66
# File 'lib/sogou/sem/base.rb', line 64

def operations
  @client.operations(@service_name,@port_name)
end