class Laximo::Respond::Base
Constants
- RESPONSE_LOGIN_RESULT
- RESPONSE_RESULT
- RESPONSE_SOAP_ERROR
Public Class Methods
new(request)
click to toggle source
# File lib/laximo/respond.rb, line 60 def initialize(request) @error = nil @result = [] prepare_request(request) end
parsing_result(str)
click to toggle source
# File lib/laximo/respond.rb, line 15 def parsing_result(str) ::NotImplementedError.new("Метод `parsing_result` не реализован в классе #{self.class}") end
Private Class Methods
node_to_hash(node) { |h, node| ... }
click to toggle source
# File lib/laximo/respond.rb, line 21 def node_to_hash(node) return {} if node.nil? h = {} node.attributes.each { |key, snd| h[key.to_sym] = snd.value } return {} if h.empty? yield(h, node) if block_given? h end
nodes_to_hash(nodes, recursive: true)
click to toggle source
# File lib/laximo/respond.rb, line 37 def nodes_to_hash(nodes, recursive: true) arr = [] nodes.each { |node| h = {} node.attributes.each { |key, snd| h[key.to_sym] = snd.value } if recursive children = nodes_to_hash(node.children, recursive: true) h[:children] = children unless children.empty? end arr << h } arr end
Public Instance Methods
error()
click to toggle source
# File lib/laximo/respond.rb, line 79 def error @error end
failure?()
click to toggle source
# File lib/laximo/respond.rb, line 73 def failure? !@error.nil? end
Also aliased as: error?
result()
click to toggle source
# File lib/laximo/respond.rb, line 83 def result @result end
result!()
click to toggle source
# File lib/laximo/respond.rb, line 87 def result! raise error.class, error.message if failure? result end
success?()
click to toggle source
# File lib/laximo/respond.rb, line 69 def success? @error.nil? end
Private Instance Methods
prepare_error(http)
click to toggle source
# File lib/laximo/respond.rb, line 106 def prepare_error(http) @result = [] if http.respond_to?(:body) begin doc = ::Nokogiri::XML(http.body) doc.remove_namespaces! if (res = doc.xpath(RESPONSE_SOAP_ERROR)).empty? @error = http else @error = soap_errors(res.text) end rescue ::Exception => ex @error = ex end elsif http.is_a?(::Net::HTTPBadRequest) @error = ::Laximo::SslCertificateError.new('SSL cetificate doesn`t found or invalidate') else @error = http end end
prepare_http(http)
click to toggle source
# File lib/laximo/respond.rb, line 135 def prepare_http(http) begin doc = ::Nokogiri::XML(http.body) doc.remove_namespaces! res = doc.xpath(RESPONSE_LOGIN_RESULT).children[0].to_s res = doc.xpath(RESPONSE_RESULT).children[0].to_s if res.blank? @error = nil @result = self.class.parsing_result( ::Nokogiri::XML(unescape(res)) ) || [] if @result.empty? && ::Laximo.options.raise_on_empty_response? @error = ::Laximo::SoapEmptyResponseError.new('Empty response') end rescue ::Exception => ex @result = [] @error = ex end end
prepare_request(request)
click to toggle source
# File lib/laximo/respond.rb, line 96 def prepare_request(request) if request.is_a?(::Net::HTTPOK) prepare_http(request) else prepare_error(request) end end
soap_errors(err_txt)
click to toggle source
# File lib/laximo/respond.rb, line 163 def soap_errors(err_txt) err_name, msg = err_txt.split(':') err_cls = ::Laximo::ERRORS[err_name] || ::Laximo::SoapError err_cls.new(msg) end
unescape(str)
click to toggle source
# File lib/laximo/respond.rb, line 172 def unescape(str) ::CGI::unescapeHTML str end