Class: Basecamp3::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/basecamp3/request.rb

Overview

A class for handling requests

Instance Method Summary collapse

Constructor Details

#initialize(access_token, uri) ⇒ Request

Initializes the request object.

Parameters:

  • access_token (String)

    the data to send in the request

  • uri (String)


8
9
10
11
# File 'lib/basecamp3/request.rb', line 8

def initialize(access_token, uri)
  @access_token = access_token
  @uri = uri
end

Instance Method Details

#delete(path, model = 'raw') ⇒ Basecamp3::Model, OpenStruct

Sends the delete request.

Parameters:

  • path (String)

    the request path

  • model (String) (defaults to: 'raw')

    the name of the model

Returns:



70
71
72
# File 'lib/basecamp3/request.rb', line 70

def delete(path, model = 'raw')
  request(:delete, path, nil, model)
end

#get(path, params = {}, model = 'raw') ⇒ Basecamp3::Model, OpenStruct

Sends the get request.

Parameters:

  • path (String)

    the request path

  • params (Hash) (defaults to: {})

    the get params

  • model (String) (defaults to: 'raw')

    the name of the model

Returns:



38
39
40
# File 'lib/basecamp3/request.rb', line 38

def get(path, params = {}, model = 'raw')
  request(:get, "#{path}#{hash_to_get_query(params)}", nil, model)
end

#post(path, data = nil, model = 'raw') ⇒ Basecamp3::Model, OpenStruct

Sends the post request.

Parameters:

  • path (String)

    the request path

  • data (Hash) (defaults to: nil)

    the request body

  • model (String) (defaults to: 'raw')

    the name of the model

Returns:



49
50
51
# File 'lib/basecamp3/request.rb', line 49

def post(path, data = nil, model = 'raw')
  request(:post, path, data, model)
end

#put(path, data = nil, model = 'raw') ⇒ Basecamp3::Model, OpenStruct

Sends the put request.

Parameters:

  • path (String)

    the request path

  • data (Hash) (defaults to: nil)

    the request body

  • model (String) (defaults to: 'raw')

    the name of the model

Returns:



60
61
62
# File 'lib/basecamp3/request.rb', line 60

def put(path, data = nil, model = 'raw')
  request(:put, path, data, model)
end

#request(method, path, data = nil, model = 'raw') ⇒ Basecamp3::Model, OpenStruct

Sends the request.

Parameters:

  • method (Symbol)

    the symbol of the http method

  • path (String)

    the request path

  • data (Hash) (defaults to: nil)

    the request body

  • model (String) (defaults to: 'raw')

    the name of the model

Returns:



21
22
23
24
25
26
27
28
29
# File 'lib/basecamp3/request.rb', line 21

def request(method, path, data = nil, model = 'raw')
  uri = build_request_uri(path)
  https = build_https_object(uri)
  request = build_request_object(method, uri)

  request.body = data.to_json unless data.nil?

  get_response(https, request, model)
end