Class: RocketPants::Client

Inherits:
APISmith::Base
  • Object
show all
Defined in:
lib/rocket_pants/client.rb

Overview

Implements a generalised base for building clients on top of the rocket pants controller. This automatically unpacks the API into the correct response types, handles errors (using the same error registry as the server) and in general makes it simpler to implement api clients.

Examples:

A versioned api client

class MyApiClient < RocketPants::Client

  class Profile < APISmith::Smash
    property :name
    property :email
  end

  version 1
  base_uri "http://api.example.com/"

  def profile
    get 'profile', :transformer => Profile
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initializes a new client, optionally setting up the host for this client.

Parameters:

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

    general client options

Options Hash (options):

  • :api_host (String)

    the overriden base_uri host for this client instance.



68
69
70
71
72
73
74
# File 'lib/rocket_pants/client.rb', line 68

def initialize(options = {})
  super
  # Setup the base uri if passed in to the client.
  if options[:api_host].present?
    add_request_options! :base_uri => HTTParty.normalize_base_uri(options[:api_host])
  end
end

Class Method Details

._original_endpointObject



54
# File 'lib/rocket_pants/client.rb', line 54

alias _original_endpoint endpoint

.endpoint(path) ⇒ Object

Sets the endpoint url, taking into account the version number as a prefix if present.



58
59
60
61
# File 'lib/rocket_pants/client.rb', line 58

def endpoint(path)
  self._actual_endpoint = path
  _original_endpoint File.join(*[_version, path].compact.map(&:to_s))
end

.versionInteger .version(number) ⇒ Object

Overloads:

  • .versionInteger

    Returns the current API version number for this client.

    Returns:

    • (Integer)

      the current API version number for this client

  • .version(number) ⇒ Object

    Sets a variable noting what version of the api the client uses and updates the endpoint to prefix it with the given version number.

    Parameters:

    • number (Integer)

      the version of the api to process.



44
45
46
47
48
49
50
51
52
# File 'lib/rocket_pants/client.rb', line 44

def version(number = nil)
  if number.nil?
    _version
  else
    self._version = number
    endpoint _actual_endpoint
    number
  end
end