Class: APIClientBuilder::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/api_client_builder/api_client.rb

Overview

The base APIClient that defines the interface for defining an API Client. Should be sub-classed and then provided an HTTPClient handler and a response handler.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ APIClient

Returns a new instance of APIClient.

Parameters:

  • opts (Hash)

    options hash

Options Hash (**opts):

  • :http_client (Symbol)

    The http client handler

  • :paginator (Symbol)

    The response handler



11
12
13
14
# File 'lib/api_client_builder/api_client.rb', line 11

def initialize(**opts)
  @url_generator = APIClientBuilder::URLGenerator.new(opts[:domain])
  @http_client = opts[:http_client]
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



6
7
8
# File 'lib/api_client_builder/api_client.rb', line 6

def http_client
  @http_client
end

#url_generatorObject (readonly)

Returns the value of attribute url_generator.



6
7
8
# File 'lib/api_client_builder/api_client.rb', line 6

def url_generator
  @url_generator
end

Class Method Details

.delete(type, route) ⇒ DeleteRequest

Used to define a DELETE api route on the base class. Will yield a method that takes the shape of ‘delete_type’ that will return a DeleteRequest.

Parameters:

  • type (Symbol)

    defines the route model

  • route (String)

    defines the routes endpoint

Returns:



87
88
89
90
91
92
93
94
# File 'lib/api_client_builder/api_client.rb', line 87

def self.delete(type, route)
  define_method("delete_#{type}") do |**params|
    DeleteRequest.new(
      type,
      response_handler_build(http_client, @url_generator.build_route(route, **params), type)
    )
  end
end

.get(type, plurality, route, **_opts) ⇒ Request

Used to define a GET api route on the base class. Will yield a method that takes the shape of ‘get_type’ that will return a CollectionResponse or ItemResponse based on plurality.

Parameters:

  • type (Symbol)

    defines the route model

  • plurality (Symbol)

    defines the routes plurality

  • route (String)

    defines the routes endpoint

Returns:

  • (Request)

    either a GetCollection or GetItem request



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/api_client_builder/api_client.rb', line 25

def self.get(type, plurality, route, **_opts)
  if plurality == :collection
    define_method("get_#{type}") do |**params|
      GetCollectionRequest.new(
        type,
        response_handler_build(http_client, @url_generator.build_route(route, **params), type)
      )
    end
  elsif plurality == :singular
    define_method("get_#{type}") do |**params|
      GetItemRequest.new(
        type,
        response_handler_build(http_client, @url_generator.build_route(route, **params), type)
      )
    end
  end
end

.post(type, route) ⇒ PostRequest

Used to define a POST api route on the base class. Will yield a method that takes the shape of ‘post_type’ that will return a PostRequest.

Parameters:

  • type (Symbol)

    defines the route model

  • route (String)

    defines the routes endpoint

Returns:

  • (PostRequest)

    the request object that handles posts



51
52
53
54
55
56
57
58
59
# File 'lib/api_client_builder/api_client.rb', line 51

def self.post(type, route)
  define_method("post_#{type}") do |body, **params|
    PostRequest.new(
      type,
      response_handler_build(http_client, @url_generator.build_route(route, **params), type),
      body
    )
  end
end

.put(type, route) ⇒ PutRequest

Used to define a PUT api route on the base class. Will yield a method that takes the shape of ‘put_type’ that will return a PutRequest.

Parameters:

  • type (Symbol)

    defines the route model

  • route (String)

    defines the routes endpoint

Returns:

  • (PutRequest)

    the request object that handles puts



69
70
71
72
73
74
75
76
77
# File 'lib/api_client_builder/api_client.rb', line 69

def self.put(type, route)
  define_method("put_#{type}") do |body, **params|
    PutRequest.new(
      type,
      response_handler_build(http_client, @url_generator.build_route(route, **params), type),
      body
    )
  end
end