Sha256: a338102e92e15e2bc4b9d441dc51209e6b9587575f80188437aa8258a6bb7e5e

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 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.capitalize}")
      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

2 entries across 2 versions & 1 rubygems

Version Path
web_client-0.0.6 lib/web_client/request.rb
web_client-0.0.5 lib/web_client/request.rb