Sha256: ca05ce0e6a7ba91714928a94b80c5cf61208977427aa1d73bfb7f56c5ae7b631

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module Preposterous
  class Request
    extend Forwardable
    def_delegators :client, :get, :post
  
    attr_reader :client, :method, :path, :options
    
    def self.get(client, path, options={})
      new(client, :get, path, options).perform
    end

    def self.post(client, path, options={})
      new(client, :post, path, options).perform
    end

    def initialize(client, method, path, options={})
      @client, @method, @path, @options = client, method, path, options
    end

    def uri
      @uri ||= begin
        uri = URI.parse(path)
        if options[:fields] && options[:fields] != {}
          uri.query = to_query(options[:fields])
        end

        uri.to_s
      end
    end

    def perform
      Preposterous.make_friendly(send("perform_#{method}"))
    end

    private

    def perform_get
      get(uri, options[:headers])
    end

    def perform_post
      post(uri, options[:body], options[:headers])
    end

    def to_query(options)
      options.inject([]) do |collection, opt|
        collection << "#{CGI.escape opt[0].to_s}=#{CGI.escape opt[1].to_s}"
        collection
      end * '&'
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
preposterous-0.0.5 lib/preposterous/request.rb
preposterous-0.0.4 lib/preposterous/request.rb
preposterous-0.0.3 lib/preposterous/request.rb
preposterous-0.0.2 lib/preposterous/request.rb