Sha256: 989e82d7e16545ba38d40642303022e886f8166e4bbded648d35f430202bc3d0

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'multi_json'

module Flattr
  # Defines HTTP request methods
  module Request
    # Perform an HTTP DELETE request
    def delete(path, params={}, options={})
      request(:delete, path, params, options)
    end

    # Perform an HTTP GET request
    def get(path, params={}, options={})
      request(:get, path, params, options)
    end

    # Perform an HTTP POST request
    def post(path, params={}, options={})
      request(:post, path, params, options)
    end

    # Perform an HTTP PUT request
    def put(path, params={}, options={})
      request(:put, path, params, options)
    end

    # Perform an HTTP PATCH request
    def patch(path, params={}, options={})
      request(:patch, path, params, options)
    end
  private

    # Perform an HTTP request
    def request(method, path, params, options)
      response = connection(options).send(method) do |request|
        case method.to_sym
        when :get, :delete
          request.url(path, MultiJson.dump(params))
        when :post, :put, :patch
          request.path = path
          request.headers['Content-Type'] = 'application/json'
          request.body = MultiJson.dump(params) unless params.empty?
        end
      end
      options[:raw] ? response : response.body
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flattr-0.3.4 lib/flattr/request.rb