Sha256: de0109799874b4640ab6b44175f41c0199043a8f5c7b33fda418b9b3381cecf9

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

Contents

require 'net/http'

class Easytobookr::Request
  attr_accessor :function_name, :params

  def initialize(function_name,params={})
    @function_name = function_name
    @params = params
  end

  def to_xml
    request_builder.to_xml
  end

  def response
    http = Net::HTTP.new(uri.host,uri.port)
    response = http.post(uri.path,self.to_xml)
    Nokogiri.XML(response.body)
  end

  def request_builder
    Nokogiri::XML::Builder.new do |xml|
      xml.Easytobook {
        xml.Request(:target => config.env) {
          xml.Authentication(:username => config.username, :password => config.password) {
            xml.Function self.function_name
          }
          params.each {|key,value| xml.send(key,value) } if params
        }
      }
    end
  end
  protected :request_builder

  # Convenience method to returns the common config.
  def config
    Easytobookr.config
  end

  # Convenience method to returns the endpoint uri.
  def uri
    config.uri
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easytobookr-0.0.1 lib/easytobookr/request.rb