Class: ApiMapper::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/api_mapper/client.rb

Overview

API client class

Provides methods to access API endpoints

Examples:

client = ApiMapper::Client.new('http://api.example.com')
client.authorization("token super_secret_token")
user = client.get("user")
user. = 'Incognito'
incognito_user = client.patch("user", user)

Defined Under Namespace

Classes: ResponseMapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, router = Router.new) ⇒ ApiMapper::Client

A new instance of ApiMapper::Client

Parameters:

  • base_url (String)

    base URL of all API endpoints


18
19
20
21
# File 'lib/api_mapper/client.rb', line 18

def initialize(base_url, router = Router.new)
  @base_url = base_url
  @router = router
end

Instance Attribute Details

#router=(value) ⇒ ApiMapper::Router (writeonly)

router used for processing requests

Parameters:

Returns:


13
14
15
# File 'lib/api_mapper/client.rb', line 13

def router=(value)
  @router = value
end

Instance Method Details

#authorization(authorization) ⇒ String

Authorize client using `Authorization` HTTP header

Examples:

client.authorize("Bearer secret_token")

Parameters:

  • authorization (String)

    authorization header

Returns:

  • (String)

    authorization header


68
69
70
# File 'lib/api_mapper/client.rb', line 68

def authorization(authorization)
  @authorization = authorization
end

#get(path, payload = nil) ⇒ Array, Object

Make HTTP GET request

Examples:

client.get('users')

Parameters:

  • path (String)

    request path

Returns:

  • (Array, Object)

    mapped API response


31
32
33
# File 'lib/api_mapper/client.rb', line 31

def get(path, payload = nil)
  result(payload, Route.new(:get, path))
end

#patch(path, payload = nil) ⇒ Array, Object

Make HTTP PATCH request

Examples:

client.patch('users', user)

Parameters:

  • path (String)

    request path

  • payload (Object) (defaults to: nil)

    request payload

Returns:

  • (Array, Object)

    mapped API response


44
45
46
# File 'lib/api_mapper/client.rb', line 44

def patch(path, payload = nil)
  result(payload, Route.new(:patch, path))
end

#post(path, payload = nil) ⇒ Array, Object

Make HTTP POST request

Examples:

client.post('users', user)

Parameters:

  • path (String)

    request path

  • payload (Object) (defaults to: nil)

    request payload

Returns:

  • (Array, Object)

    mapped API response


57
58
59
# File 'lib/api_mapper/client.rb', line 57

def post(path, payload = nil)
  result(payload, Route.new(:post, path))
end