Class: ApiAdaptor::Base
- Inherits:
-
Object
- Object
- ApiAdaptor::Base
- Extended by:
- Forwardable
- Defined in:
- lib/api_adaptor/base.rb
Overview
Base class for building API-specific clients.
Provides common functionality for JSON API clients including HTTP method delegation, URL construction, and pagination support. Subclass this to create clients for specific APIs.
Defined Under Namespace
Classes: InvalidAPIURL
Class Attribute Summary collapse
-
.default_options ⇒ Hash?
Default options merged into all Base instances.
-
.logger ⇒ Logger
Returns the default logger for Base instances.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The client configuration options.
Instance Method Summary collapse
-
#client ⇒ JSONClient
Returns the underlying JSONClient instance, creating it if necessary.
-
#create_client ⇒ JSONClient
Creates a new JSONClient with the configured options.
-
#delete_json(url, params = {}) ⇒ Response
Performs a DELETE request.
-
#get_json(url) {|Hash| ... } ⇒ Response, Object
Performs a GET request and parses JSON response.
-
#get_list(url) ⇒ ListResponse
Performs a GET request and wraps the response in a ListResponse.
-
#get_raw(url) ⇒ RestClient::Response
Performs a GET request and returns raw response.
-
#get_raw!(url) ⇒ RestClient::Response
Performs a GET request and returns raw response, raising on errors.
-
#initialize(endpoint_url = nil, options = {}) ⇒ Base
constructor
Initializes a new API client.
-
#patch_json(url, params = {}) ⇒ Response
Performs a PATCH request with JSON body.
-
#post_json(url, params = {}) ⇒ Response
Performs a POST request with JSON body.
-
#post_multipart(url, params = {}) ⇒ Response
Performs a POST request with multipart/form-data.
-
#put_json(url, params = {}) ⇒ Response
Performs a PUT request with JSON body.
-
#put_multipart(url, params = {}) ⇒ Response
Performs a PUT request with multipart/form-data.
-
#url_for_slug(slug, options = {}) ⇒ String
Constructs a URL for a given slug with query parameters.
Constructor Details
#initialize(endpoint_url = nil, options = {}) ⇒ Base
Initializes a new API client
161 162 163 164 165 166 167 168 169 |
# File 'lib/api_adaptor/base.rb', line 161 def initialize(endpoint_url = nil, = {}) [:endpoint_url] = endpoint_url raise InvalidAPIURL if !endpoint_url.nil? && endpoint_url !~ URI::RFC3986_Parser::RFC3986_URI = { logger: ApiAdaptor::Base.logger } = .merge(ApiAdaptor::Base. || {}) = .merge() self.endpoint = [:endpoint_url] end |
Class Attribute Details
.default_options ⇒ Hash?
Default options merged into all Base instances
139 140 141 |
# File 'lib/api_adaptor/base.rb', line 139 def end |
.logger ⇒ Logger
Returns the default logger for Base instances
145 146 147 |
# File 'lib/api_adaptor/base.rb', line 145 def self.logger @logger ||= ApiAdaptor::NullLogger.new end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns The client configuration options.
128 129 130 |
# File 'lib/api_adaptor/base.rb', line 128 def end |
Instance Method Details
#client ⇒ JSONClient
Returns the underlying JSONClient instance, creating it if necessary
44 45 46 |
# File 'lib/api_adaptor/base.rb', line 44 def client @client ||= create_client end |
#create_client ⇒ JSONClient
Creates a new JSONClient with the configured options
51 52 53 |
# File 'lib/api_adaptor/base.rb', line 51 def create_client ApiAdaptor::JsonClient.new() end |
#delete_json(url, params = {}) ⇒ Response
Performs a DELETE request
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#get_json(url) {|Hash| ... } ⇒ Response, Object
Performs a GET request and parses JSON response
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#get_list(url) ⇒ ListResponse
Performs a GET request and wraps the response in a ListResponse
196 197 198 199 200 |
# File 'lib/api_adaptor/base.rb', line 196 def get_list(url) get_json(url) do |r| ApiAdaptor::ListResponse.new(r, self) end end |
#get_raw(url) ⇒ RestClient::Response
Performs a GET request and returns raw response
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#get_raw!(url) ⇒ RestClient::Response
Performs a GET request and returns raw response, raising on errors
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#patch_json(url, params = {}) ⇒ Response
Performs a PATCH request with JSON body
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#post_json(url, params = {}) ⇒ Response
Performs a POST request with JSON body
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#post_multipart(url, params = {}) ⇒ Response
Performs a POST request with multipart/form-data
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#put_json(url, params = {}) ⇒ Response
Performs a PUT request with JSON body
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#put_multipart(url, params = {}) ⇒ Response
Performs a PUT request with multipart/form-data
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/api_adaptor/base.rb', line 116 def_delegators :client, :get_json, :post_json, :put_json, :patch_json, :delete_json, :get_raw, :get_raw!, :put_multipart, :post_multipart |
#url_for_slug(slug, options = {}) ⇒ String
Constructs a URL for a given slug with query parameters
181 182 183 |
# File 'lib/api_adaptor/base.rb', line 181 def url_for_slug(slug, = {}) "#{base_url}/#{slug}.json#{query_string(options)}" end |