Class: Ipiranga::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ipiranga/client.rb

Direct Known Subclasses

KM, PF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ipiranga/client.rb', line 12

def initialize(opts = {})
  @wsdl_url = URI(opts.fetch(:wsdl_url, wsdl_url))
  @username = opts[:username]
  @password = opts[:password]
  @wsdl = opts[:wsdl]

  operations.each do |operation|
    define_singleton_method("post_#{operation}") do |&block|
      post(operation, &block)
    end

    define_singleton_method(operation) do |&block|
      post(operation, &block).body_hash
    end unless respond_to?(operation)
  end
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/ipiranga/client.rb', line 10

def password
  @password
end

#soapObject (readonly)

Returns the value of attribute soap.



9
10
11
# File 'lib/ipiranga/client.rb', line 9

def soap
  @soap
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/ipiranga/client.rb', line 10

def username
  @username
end

#wsdlObject

Returns the value of attribute wsdl.



10
11
12
# File 'lib/ipiranga/client.rb', line 10

def wsdl
  @wsdl
end

#wsdl_urlObject (readonly)

Returns the value of attribute wsdl_url.



9
10
11
# File 'lib/ipiranga/client.rb', line 9

def wsdl_url
  @wsdl_url
end

Instance Method Details

#has_credentials?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ipiranga/client.rb', line 62

def has_credentials?
  !username.nil? && !password.nil?
end

#operation(key) ⇒ Object



41
42
43
# File 'lib/ipiranga/client.rb', line 41

def operation(key)
  soap.wsdl.operations[key]
end

#operationsObject



37
38
39
# File 'lib/ipiranga/client.rb', line 37

def operations
  soap.wsdl.operations.keys
end

#post(operation) {|pRequest| ... } ⇒ Object

Yields:

  • (pRequest)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ipiranga/client.rb', line 45

def post(operation)
  request = soap.request(operation)

  pRequest = request.body.pRequest

  yield pRequest if block_given?

  append_wsse(request) if has_credentials?

  uri = URI(request.url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http_response = http.post(uri.path.gsub(".cls", ".CLS"), request.content, request.headers)

  soap.response(request, http_response.body)
end