Class: Kookaburra::APIDriver

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/kookaburra/api_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration, http_client = Patron::Session.new) ⇒ APIDriver

Wraps http_client in a SimpleDelegator that causes request methods to either return the response body or raise an exception on an unexpected response status code.

Parameters:



13
14
15
16
# File 'lib/kookaburra/api_driver.rb', line 13

def initialize(configuration, http_client = Patron::Session.new)
  http_client.base_url = configuration.app_host
  super(http_client)
end

Instance Method Details

#delete(path, options = {}) ⇒ Object

Makes a DELETE request via the :http_client

Parameters:

  • path (String)

    The path to request (e.g. "/foo")

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

    a customizable set of options

Options Hash (options):

  • :expected_response_status (Integer) — default: 201

    The HTTP status code that you expect the server to respond with.

Raises:

  • (Kookaburra::UnexpectedResponse)

    raised if the HTTP status of the response does not match the :expected_response_status



62
63
64
# File 'lib/kookaburra/api_driver.rb', line 62

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

#get(path, options = {}) ⇒ Object

Makes a GET request via the :http_client

Parameters:

  • path (String)

    The path to request (e.g. "/foo")

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

    a customizable set of options

Options Hash (options):

  • :expected_response_status (Integer) — default: 201

    The HTTP status code that you expect the server to respond with.

Raises:

  • (Kookaburra::UnexpectedResponse)

    raised if the HTTP status of the response does not match the :expected_response_status



51
52
53
# File 'lib/kookaburra/api_driver.rb', line 51

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

#post(path, data, options = {}) ⇒ Object

Makes a POST request via the :http_client

Parameters:

  • path (String)

    The path to request (e.g. "/foo")

  • data (Hash, String)

    The post data. If a Hash is provided, it will be converted to an 'application/x-www-form-urlencoded' post body.

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

    a customizable set of options

Options Hash (options):

  • :expected_response_status (Integer) — default: 201

    The HTTP status code that you expect the server to respond with.

Raises:

  • (Kookaburra::UnexpectedResponse)

    raised if the HTTP status of the response does not match the :expected_response_status



27
28
29
# File 'lib/kookaburra/api_driver.rb', line 27

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

#put(path, data, options = {}) ⇒ Object

Makes a PUT request via the :http_client

Parameters:

  • path (String)

    The path to request (e.g. "/foo")

  • data (Hash, String)

    The post data. If a Hash is provided, it will be converted to an 'application/x-www-form-urlencoded' post body.

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

    a customizable set of options

Options Hash (options):

  • :expected_response_status (Integer) — default: 201

    The HTTP status code that you expect the server to respond with.

Raises:

  • (Kookaburra::UnexpectedResponse)

    raised if the HTTP status of the response does not match the :expected_response_status



40
41
42
# File 'lib/kookaburra/api_driver.rb', line 40

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