Class: LandbClient

Inherits:
Object
  • Object
show all
Defined in:
lib/landb/landb_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLandbClient

Returns a new instance of LandbClient.



15
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/landb/landb_client.rb', line 15

def initialize
  # Get the configurations. arguments are: wsdl, username, password.
  if !@@config 
    return
  end
  
  # Make HTTPI silent.
  HTTPI.log = false
  
  # Make savon silent.
  Savon.configure do |savon_config|
    savon_config.log = false
  end
  
  # Getting the SOAP client with WSDL specifications.
  @client = Savon.client(@@config["wsdl"]) 
  @client.http.auth.ssl.verify_mode = :none


  # Create a hash that contains all the operations and arguments that SOAP server supports.
  # e.g. The get_auth_token action is taking 3 arguments: Login, Password, Type.
  @operations_to_arguments_hash = get_all_operations_and_arguments
  
  # For each action the SOAP server supports, we create a dynamic method for our class.
  @client.wsdl.soap_actions.each do |method_name|
    # Ruby method "send" helps us to create the methods of the class.
    self.class.send :define_method, method_name do |arg|
      
      # Error handling for arguments.
      arg = arg.to_a unless arg.instance_of? Array
      
      if arg.count != @operations_to_arguments_hash[method_name].count
        puts "Arguments are: #{@operations_to_arguments_hash[method_name].inspect}"
        puts 'Arguments should be in an array. e.g. ["test", "test2"]'
        return
      end
      
      # For each action we have, we must integrate it with Savon.
      response = @client.request(:message, method_name) do |soap|
        # We are dynamically creating the content of the SOAP request.
        soap.header = {"Auth" => {"token" => @auth_token } } if @auth_token
        
        soap.body = {}
        
        i = 0
        # The operations_to_arguments_hash hash has all the needed arguments for the request.
        # We populate the soap arguments with the user arguments.
        @operations_to_arguments_hash[method_name].each do |soap_argument|
          soap.body.merge! soap_argument => arg[i]
          i+=1
        end
        
        # Savon tutorials infoms that if you are using ruby<1.9 there might be problems with the order.
        # This is a workaround to this problem.
        soap.body.merge! :order! => @operations_to_arguments_hash[method_name]
        
      end
      
      # We are setting the auth token automatically when existes.
      if response.to_hash[:get_auth_token_response]
        @auth_token = response.to_hash[:get_auth_token_response][:token]
      end
      
      LandbResponse.new(response.to_hash["#{method_name}_response".to_sym])
    end
  end
        
  # There is a bug in the current version of Savon.(?)(i am using 1.1.0) This is a workarround. 
  # Ref: https://github.com/rubiii/savon/pull/275
  @client = Savon.client do |wsdl|
    wsdl.endpoint = @client.wsdl.endpoint.to_s
    wsdl.namespace = @client.wsdl.namespace
  end
  @client.http.auth.ssl.verify_mode = :none

  # Initialize the token, so the client would be ready for usage
  # only if name and password have been provided

  if (!@@config['username'].nil? || !@@config['password'].nil?)
    self.get_auth_token [@@config["username"], @@config["password"], "NICE"] 
  end

end

Class Method Details

.flushObject



11
12
13
# File 'lib/landb/landb_client.rb', line 11

def self.flush
  @@instance = LandbClient.new
end

.instanceObject



3
4
5
# File 'lib/landb/landb_client.rb', line 3

def self.instance
  @@instance ||= LandbClient.new
end

.set_config(config) ⇒ Object



7
8
9
# File 'lib/landb/landb_client.rb', line 7

def self.set_config(config)
  @@config = config
end

Instance Method Details

#help_all_operationsObject



103
104
105
# File 'lib/landb/landb_client.rb', line 103

def help_all_operations
  @operations_to_arguments_hash.keys
end

#help_arguments_for_operation(operation) ⇒ Object



107
108
109
# File 'lib/landb/landb_client.rb', line 107

def help_arguments_for_operation(operation)
  @operations_to_arguments_hash[operation.to_sym]
end


99
100
101
# File 'lib/landb/landb_client.rb', line 99

def print_auth_token
  @auth_token
end