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
-
.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.
17 18 19 20 |
# File 'lib/api_client_builder/api_client.rb', line 17 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.
12 13 14 |
# File 'lib/api_client_builder/api_client.rb', line 12 def http_client @http_client end |
#url_generator ⇒ Object (readonly)
Returns the value of attribute url_generator.
12 13 14 |
# File 'lib/api_client_builder/api_client.rb', line 12 def url_generator @url_generator end |
Class Method Details
.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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/api_client_builder/api_client.rb', line 31 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.
57 58 59 60 61 62 63 64 65 |
# File 'lib/api_client_builder/api_client.rb', line 57 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.
75 76 77 78 79 80 81 82 83 |
# File 'lib/api_client_builder/api_client.rb', line 75 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 |