Class: Lutaml::Hal::Client
- Inherits:
-
Object
- Object
- Lutaml::Hal::Client
- Defined in:
- lib/lutaml/hal/client.rb
Overview
HAL Client for making HTTP requests to HAL APIs
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
Instance Method Summary collapse
-
#get(url, params = {}) ⇒ Object
Make a GET request to the API.
-
#get_by_url(url, params = {}) ⇒ Object
Get a resource by its full URL.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 |
# File 'lib/lutaml/hal/client.rb', line 15 def initialize( = {}) @api_url = [:api_url] || raise(ArgumentError, 'api_url is required') @connection = [:connection] || create_connection @params_default = [:params_default] || {} @debug = [:debug] || !ENV['DEBUG_API'].nil? @cache = [:cache] || {} @cache_enabled = [:cache_enabled] || false end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
13 14 15 |
# File 'lib/lutaml/hal/client.rb', line 13 def api_url @api_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
13 14 15 |
# File 'lib/lutaml/hal/client.rb', line 13 def connection @connection end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
13 14 15 |
# File 'lib/lutaml/hal/client.rb', line 13 def last_response @last_response end |
Instance Method Details
#get(url, params = {}) ⇒ Object
Make a GET request to the API
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lutaml/hal/client.rb', line 32 def get(url, params = {}) cache_key = "#{url}:#{params.to_json}" return @cache[cache_key] if @cache_enabled && @cache.key?(cache_key) @last_response = @connection.get(url, params) response = handle_response(@last_response, url) @cache[cache_key] = response if @cache_enabled response rescue Faraday::ConnectionFailed => e raise ConnectionError, "Connection failed: #{e.}" rescue Faraday::TimeoutError => e raise TimeoutError, "Request timed out: #{e.}" rescue Faraday::ParsingError => e raise ParsingError, "Response parsing error: #{e.}" rescue Faraday::Adapter::Test::Stubs::NotFound => e raise LinkResolutionError, "Resource not found: #{e.}" end |
#get_by_url(url, params = {}) ⇒ Object
Get a resource by its full URL
25 26 27 28 29 |
# File 'lib/lutaml/hal/client.rb', line 25 def get_by_url(url, params = {}) # Strip API endpoint if it's included path = url.sub(%r{^#{@api_url}/}, '') get(path, params) end |