Sha256: ad76a320ad23d37cb1ec1f4214c5f18ad1d45ee933b1ad410b1a83b78d5f67b7

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Genability
  # Defines HTTP request methods
  module Request
    # Perform an HTTP GET request
    def get(path, options={}, raw=false, unformatted=false)
      request(:get, path, options, raw, unformatted)
    end

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

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

    # Perform an HTTP DELETE request
    def delete(path, options={}, raw=false, unformatted=false)
      request(:delete, path, options, raw, unformatted)
    end

    private

    # Perform an HTTP request
    def request(method, path, options, raw=false, unformatted=false)
      response = connection(raw).send(method) do |request|
        path = formatted_path(path) unless unformatted || default_request?
        case method
        when :get, :delete
          request.url(path, options)
          request.params['appId'] = application_id
          request.params['appKey'] = application_key
        when :post, :put
          request.path = path
          if options['fileData']
            request.headers['Content-type'] = 'multipart/form-data'
            request.body = options
          else
            request.headers['Content-Type'] = 'application/json;charset=utf-8'
            request.body = options unless options.empty?
          end
        end
      end
      raw ? response : response.body
    end

    def formatted_path(path)
      [path, format].compact.join('.')
    end

    def default_request?
      format.to_sym == :json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
genability-0.3.0 lib/genability/request.rb