class Qiwi::Request::Base

Public Class Methods

attributes(*attrs) click to toggle source
# File lib/qiwi/request.rb, line 15
def self.attributes(*attrs)
  if attrs.empty?
    @_attributes
  else
    @_attributes ||= []
    @_attributes += attrs
    attr_accessor(*attrs)
  end
end
inherited(klass) click to toggle source
# File lib/qiwi/request.rb, line 10
def self.inherited(klass)
  klass.attributes :login, :password
  klass.validates_presence_of :login, :password
end
new(client, params) click to toggle source

See concrete classes for parameters description.

@param [Qiwi::Client] client @param [Hash] params

# File lib/qiwi/request.rb, line 29
def initialize(client, params)
  self.class.attributes.each { |attr| send(:"#{attr}=", params[attr]) }
  @login, @password = client.login, client.password
end

Public Instance Methods

body() click to toggle source
# File lib/qiwi/request.rb, line 34
def body
  with_envelope { |xml| soap_body(xml) }
end
method() click to toggle source

The SOAP method name

# File lib/qiwi/request.rb, line 60
def method
  @method ||= self.class.to_s.split('::').last.camelize(:lower)
end
result_from_xml(xml) click to toggle source

Make sense of what is returned by the server

@param [Nokogiri::XML::Document] xml

# File lib/qiwi/request.rb, line 67
def result_from_xml(xml)
  xml.xpath("//#{method}Response/#{method}Result").text.to_i
end
soap_body(xml) click to toggle source
# File lib/qiwi/request.rb, line 49
def soap_body(xml)
  xml['tns'].send(method) do
    self.class.attributes.each do |attr|
      # Underscore, so 'comment' is used as a parameter
      # some ugly way to remove the namespace
      xml.send("#{attr}_", send(attr)).instance_variable_get(:@node).namespace = nil
    end
  end
end
with_envelope(&block) click to toggle source
# File lib/qiwi/request.rb, line 38
def with_envelope(&block)
  Nokogiri::XML::Builder.new do |xml|
    xml.Envelope("xmlns:soapenv" => "http://www.w3.org/2003/05/soap-envelope",
                 "xmlns:tns" => "http://server.ishop.mw.ru/") do
      xml.parent.namespace = xml.parent.namespace_definitions.first
      xml.Header
      xml.Body(&block)
    end
  end
end