Class: APIClientBuilder::APIClient
- Inherits:
-
Object
- Object
- APIClientBuilder::APIClient
- 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
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#url_generator ⇒ Object
readonly
Returns the value of attribute url_generator.
Class Method Summary collapse
-
.delete(type, route) ⇒ DeleteRequest
Used to define a DELETE api route on the base class.
-
.get(type, plurality, route, **_opts) ⇒ Request
Used to define a GET api route on the base class.
-
.post(type, route) ⇒ PostRequest
Used to define a POST api route on the base class.
-
.put(type, route) ⇒ PutRequest
Used to define a PUT api route on the base class.
Instance Method Summary collapse
-
#initialize(**opts) ⇒ APIClient
constructor
A new instance of APIClient.
Constructor Details
#initialize(**opts) ⇒ APIClient
Returns a new instance of APIClient.
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_client ⇒ Object (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_generator ⇒ Object (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.
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.
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.
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.
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 |