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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ ApiMapper::Client

A new instance of ApiMapper::Client

Parameters:

  • base_url (String)

    base URL of all API endpoints



20
21
22
23
# File 'lib/api_mapper/client.rb', line 20

def initialize(base_url)
  @base_url = base_url
  @router = Router.new
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



79
80
81
# File 'lib/api_mapper/client.rb', line 79

def authorization(authorization)
  @authorization = authorization
end

#get(path) ⇒ Array, Object

Make HTTP GET request

Examples:

client.get('users')

Parameters:

  • path (String)

    request path

Returns:

  • (Array, Object)

    mapped API response



33
34
35
36
37
38
# File 'lib/api_mapper/client.rb', line 33

def get(path)
  response = response(:get, path)
  mapper = mapper(:get, path)

  map_response(mapper, response)
end

#patch(path, payload) ⇒ Array, Object

Make HTTP PATCH request

Examples:

client.patch('users', user)

Parameters:

  • path (String)

    request path

  • payload (Object)

    request payload

Returns:

  • (Array, Object)

    mapped API response



49
50
51
52
53
54
# File 'lib/api_mapper/client.rb', line 49

def patch(path, payload)
  mapper = mapper(:patch, path)
  response = response(:patch, path, payload)

  map_response(mapper, response)
end

#post(path, payload) ⇒ Array, Object

Make HTTP POST request

Examples:

client.post('users', user)

Parameters:

  • path (String)

    request path

  • payload (Object)

    request payload

Returns:

  • (Array, Object)

    mapped API response



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

def post(path, payload)
  mapper = mapper(:post, path)
  response = response(:post, path, payload)

  map_response(mapper, response)
end