Sha256: 1de8edf9cef0036a6de11a1ccb7c61bdfcfaea566b46e3d27179852faa516df9

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

module SoapyCake
  class Request
    attr_accessor :api_key, :time_offset
    attr_reader :role, :service, :method, :opts

    def initialize(role, service, method, opts = {})
      @role = role.to_s
      @service = service.to_s
      @method = method.to_s
      @opts = opts
    end

    def path
      "#{api_path}/#{service}.asmx"
    end

    def xml
      Nokogiri::XML::Builder.new do |xml|
        xml['env'].Envelope(xml_namespaces) do
          xml.Header
          xml.Body do
            xml['cake'].send(method.camelize.to_sym) do
              xml_params(xml)
            end
          end
        end
      end.to_xml
    end

    def addedit?
      service == 'addedit'
    end

    private

    def api_path
      "#{(role != 'admin') ? "/#{role.pluralize}" : ''}/api/#{version}"
    end

    def xml_params(xml)
      xml.api_key api_key
      opts.each do |k, v|
        xml.send(k.to_sym, format_param(v))
      end
    end

    def xml_namespaces
      {
        'xmlns:env' => 'http://www.w3.org/2003/05/soap-envelope',
        'xmlns:cake' => "http://cakemarketing.com#{api_path}/"
      }
    end

    def format_param(value)
      case value
      when Time, DateTime, Date
        (value.to_datetime.utc + time_offset.to_i.hours).strftime('%Y-%m-%dT%H:%M:%S')
      else
        value
      end
    end

    def version
      API_VERSIONS[role][service][method] || fail
    rescue
      raise(Error, "Unknown API call #{role}::#{service}::#{method}")
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
soapy_cake-1.5.0 lib/soapy_cake/request.rb
soapy_cake-1.4.0 lib/soapy_cake/request.rb
soapy_cake-1.3.6 lib/soapy_cake/request.rb
soapy_cake-1.3.5 lib/soapy_cake/request.rb
soapy_cake-1.3.4 lib/soapy_cake/request.rb
soapy_cake-1.3.3 lib/soapy_cake/request.rb
soapy_cake-1.3.2 lib/soapy_cake/request.rb
soapy_cake-1.3.1 lib/soapy_cake/request.rb
soapy_cake-1.3.0 lib/soapy_cake/request.rb
soapy_cake-1.2.0 lib/soapy_cake/request.rb