Sha256: b0fc7f3e795d6a38db26931d0f8be7af66fcb37aa76ddb62125ee4199f3bfbbb

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

module MambaNation
  class Request
    extend Forwardable

    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 self.put(client, path, options={})
      new(client, :put, path, options).perform
    end

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

    attr_reader :client, :method, :path, :options

    def_delegators :client, :get, :post, :put, :delete

    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[:query] && options[:query] != {}
          uri.query = to_query(options[:query])
        end
        
        uri.to_s
      end
    end

    def perform
      puts "\nuri: #{uri}\noptions[:body]: #{options[:body]}\noptions[:headers]: #{options[:headers]}"
      MambaNation.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 perform_put
      put(uri, options[:body], options[:headers])
    end

    def perform_delete
      delete(uri, options[:headers])
    end

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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mambanation-0.1.2 lib/mambanation/request.rb
mambanation-0.1.1 lib/mambanation/request.rb
mambanation-0.1.0 lib/mambanation/request.rb
mambanation-0.0.5 lib/mambanation/request.rb