Sha256: 57e7953bf536321b5cd388206466cd1cff018aa3dcaa2f62247b7c35b0a51bcd

Contents?: true

Size: 859 Bytes

Versions: 1

Compression:

Stored size: 859 Bytes

Contents

module WebClient
  class Request

    TYPES = [:get, :post, :put, :delete]

    attr_accessor :type
    attr_accessor :url
    attr_accessor :body
    attr_accessor :headers

    def initialize(options={}, &block)
      @type = options[:type] || :get
      @url = options[:url]
      @body = options[:body]
      @headers = options[:headers] || {}

      block.call(self) if block_given?
    end

    def to_http
      klass = eval("Net::HTTP::#{type.to_s.titleize}")
      request = klass.new url
      request.set_form_data(body) if body.is_a? Hash
      request.body = body if body.is_a? String
      headers.each { |k, v| request.send("#{k}=", v) }
      request
    end

    TYPES.each do |request_type|
      define_singleton_method request_type do |options, &block|
        new options.merge(type: request_type, &block)
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web_client-0.0.4 lib/web_client/request.rb