Class: Baidu::SEM::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/baidu/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
# File 'lib/baidu/sem/base.rb', line 7

def initialize(auth)
  classname = self.class.name.split('::').last
  @service_name = classname
  @port_name = classname
  @username = auth.username
  @password = auth.password
  @token = auth.token
  @client = Savon.new("https://api.baidu.com/sem/sms/v3/#{classname}?wsdl")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



16
17
18
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
46
47
48
49
50
51
52
# File 'lib/baidu/sem/base.rb', line 16

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_tmp =
  case name
    when 'getAllCampaign'
      'GetAllCampaign'
    when 'getCampaignByCampaignId'
      'GetCampaignByCampaignId'
    when 'addCampaign'
      'AddCampaign'
    when 'updateCampaign'
      'UpdateCampaign'
    when 'deleteCampaign'
      'DeleteCampaign'
    when 'getCampaignByCampaignId'
      'GetCampaignByCampaignId'
    else
      name
  end

  name_request_sym = (name_tmp+'Request').to_sym #if %w(getCampaignByCampaignId getAllCampaign addCampaign updateCampaign deleteCampaign).include?name
  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
  Baidu::Response.new(response,name_response_sym)
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#example(operation, with_header = false) ⇒ Object



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

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



57
58
59
# File 'lib/baidu/sem/base.rb', line 57

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

#operation_headerObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/baidu/sem/base.rb', line 61

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

#operationsObject



53
54
55
# File 'lib/baidu/sem/base.rb', line 53

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