class ApiMapper::Client

API client class

Provides methods to access API endpoints @example

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

@attr_writer (ApiMapper::Router) router router used for processing requests

Attributes

router[W]

Public Class Methods

new(base_url) click to toggle source

A new instance of ApiMapper::Client

@param (String) base_url base URL of all API endpoints @return (ApiMapper::Client)

# File lib/api_mapper/client.rb, line 20
def initialize(base_url)
  @base_url = base_url
  @router = Router.new
end

Public Instance Methods

authorization(authorization) click to toggle source

Authorize client using `Authorization` HTTP header

@example

client.authorize("Bearer secret_token")

@param (String) authorization authorization header @return (String) authorization header

# File lib/api_mapper/client.rb, line 79
def authorization(authorization)
  @authorization = authorization
end
get(path) click to toggle source

Make HTTP GET request

@example

client.get('users')

@param (String) path request path

@return [Array, Object] mapped API response

# 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) click to toggle source

Make HTTP PATCH request

@example

client.patch('users', user)

@param (String) path request path @param (Object) payload request payload

@return [Array, Object] mapped API response

# 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) click to toggle source

Make HTTP POST request

@example

client.post('users', user)

@param (String) path request path @param (Object) payload request payload

@return [Array, Object] mapped API response

# 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

Private Instance Methods

connection() click to toggle source
# File lib/api_mapper/client.rb, line 102
def connection
  @connection ||= Faraday.new(url: @base_url) do |conn|
    conn.adapter :net_http
    conn.headers["Content-Type"] = "application/json"
    conn.headers["Accept"] = "application/json"
    conn.headers["Authorization"] = @authorization if @authorization
    conn.headers["X-Client"] = "ApiMapper-v#{ApiMapper::VERSION}"
  end
end
map_response(mapper, response) click to toggle source
# File lib/api_mapper/client.rb, line 85
def map_response(mapper, response)
  body = response.body
  if body.is_a? Hash
    mapper.call([body])[0]
  else
    mapper.call(body)
  end
end
mapper(method, path) click to toggle source
# File lib/api_mapper/client.rb, line 94
def mapper(method, path)
  @router.resolve(method, path).mapper
end
response(method, path, body = nil) click to toggle source
# File lib/api_mapper/client.rb, line 98
def response(method, path, body = nil)
  Response.new(connection.__send__(method, path, Serializer.new(body).call))
end