Sha256: b38ded70471bfec4f3759aa8ff1e34a07d1ed6f2b37f12a36bfe36a957edb181

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

# encoding: utf-8

module BitBucket
  # Defines HTTP verbs
  module Request

    METHODS = [:get, :post, :put, :delete, :patch]
    METHODS_WITH_BODIES = [ :post, :put, :patch ]

    def get_request(path, params={}, options={})
      request(:get, path, params, options)
    end

    def patch_request(path, params={}, options={})
      request(:patch, path, params, options)
    end

    def post_request(path, params={}, options={})
      request(:post, path, params, options)
    end

    def put_request(path, params={}, options={})
      request(:put, path, params, options)
    end

    def delete_request(path, params={}, options={})
      request(:delete, path, params, options)
    end

    def request(method, path, params, options)
      if !METHODS.include?(method)
        raise ArgumentError, "unkown http method: #{method}"
      end
      # _extract_mime_type(params, options)

      puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']

      conn = connection(options)
      path = (conn.path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'

      response = conn.send(method) do |request|
        case method.to_sym
        when *(METHODS - METHODS_WITH_BODIES)
          request.body = params.delete('data') if params.has_key?('data')
          request.url(path, params)
        when *METHODS_WITH_BODIES
          request.path = path
          request.body = extract_data_from_params(params) unless params.empty?
        end
      end
      response.body
    end

    private

    def extract_data_from_params(params) # :nodoc:
      return params['data'] if params.has_key?('data') and !params['data'].nil?
      return params
    end

    def _extract_mime_type(params, options) # :nodoc:
      options['resource']  = params['resource'] ? params.delete('resource') : ''
      options['mime_type'] = params['resource'] ? params.delete('mime_type') : ''
    end

  end # Request
end # BitBucket

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
reenhanced_bitbucket_api-0.1.6 lib/bitbucket_rest_api/request.rb
bitbucket_rest_api-0.1.5 lib/bitbucket_rest_api/request.rb
bitbucket_rest_api-0.1.4 lib/bitbucket_rest_api/request.rb
bitbucket_rest_api-0.1.2 lib/bitbucket_rest_api/request.rb
bitbucket_rest_api-0.1.1 lib/bitbucket_rest_api/request.rb
bitbucket_rest_api-0.1.0 lib/bitbucket_rest_api/request.rb