Class: Halchemy::Api
- Inherits:
-
Object
- Object
- Halchemy::Api
- Defined in:
- lib/halchemy/api.rb
Overview
This is the Halchemy::Api class, that is the main class for interacting with HAL-based APIs
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#error_handling ⇒ Object
Returns the value of attribute error_handling.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#parameters_list_style ⇒ Object
Returns the value of attribute parameters_list_style.
Instance Method Summary collapse
- #add_headers(headers) ⇒ Object
- #follow(resource) ⇒ Object
- #home ⇒ Object
-
#initialize(base_url = nil, headers: {}) ⇒ Api
constructor
A new instance of Api.
- #optimistic_concurrency_header(resource) ⇒ Object
- #remove_headers(header_keys) ⇒ Object
- #request(method, target, headers = nil, data = nil) ⇒ Object
-
#root ⇒ Object
root is deprecated and will be removed in an upcoming release, please use home instead.
- #using_endpoint(target, is_home: false) ⇒ Object
Constructor Details
#initialize(base_url = nil, headers: {}) ⇒ Api
Returns a new instance of Api.
21 22 23 24 25 26 27 28 29 |
# File 'lib/halchemy/api.rb', line 21 def initialize(base_url = nil, headers: {}) config = Configurator.new.config configure_base(config) configure_headers(config) configure_error_handling(config) @base_url = base_url unless base_url.nil? @headers.merge!(headers) end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
19 20 21 |
# File 'lib/halchemy/api.rb', line 19 def base_url @base_url end |
#error_handling ⇒ Object
Returns the value of attribute error_handling.
19 20 21 |
# File 'lib/halchemy/api.rb', line 19 def error_handling @error_handling end |
#headers ⇒ Object
Returns the value of attribute headers.
19 20 21 |
# File 'lib/halchemy/api.rb', line 19 def headers @headers end |
#parameters_list_style ⇒ Object
Returns the value of attribute parameters_list_style.
19 20 21 |
# File 'lib/halchemy/api.rb', line 19 def parameters_list_style @parameters_list_style end |
Instance Method Details
#add_headers(headers) ⇒ Object
45 |
# File 'lib/halchemy/api.rb', line 45 def add_headers(headers) = @headers.merge!(headers) |
#follow(resource) ⇒ Object
64 |
# File 'lib/halchemy/api.rb', line 64 def follow(resource) = Follower.new(self, resource) |
#home ⇒ Object
31 |
# File 'lib/halchemy/api.rb', line 31 def home = using_endpoint("/", is_home: true) |
#optimistic_concurrency_header(resource) ⇒ Object
66 67 68 69 |
# File 'lib/halchemy/api.rb', line 66 def optimistic_concurrency_header(resource) etag = resource._halchemy.response.headers["Etag"] || resource[@etag_field] etag.nil? ? {} : { "If-Match" => etag } end |
#remove_headers(header_keys) ⇒ Object
48 49 50 |
# File 'lib/halchemy/api.rb', line 48 def remove_headers(header_keys) header_keys.each { |key| @headers.delete(key) } end |
#request(method, target, headers = nil, data = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/halchemy/api.rb', line 52 def request(method, target, headers = nil, data = nil) url = build_url(target) request_headers = headers.nil? ? @headers : @headers.merge(headers) request = HttpModel::Request.new(method, url, data, request_headers) result = HTTPX.with(headers: request_headers).send(method, url, body: data) raise_for_errors(result) build_resource(request, result) end |
#root ⇒ Object
root is deprecated and will be removed in an upcoming release, please use home instead
34 |
# File 'lib/halchemy/api.rb', line 34 def root = using_endpoint("/", is_home: true) |
#using_endpoint(target, is_home: false) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/halchemy/api.rb', line 36 def using_endpoint(target, is_home: false) if is_home ReadOnlyRequester.new(self, target) else Requester.new(self, target) end end |