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.
-
#rate_limiter ⇒ Object
readonly
Returns the value of attribute rate_limiter.
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.
-
#get_with_headers(url, headers = {}) ⇒ Object
Make a GET request with custom headers.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#strip_api_url(url) ⇒ Object
Strip any trailing slash from the API URL.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lutaml/hal/client.rb', line 16 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 @rate_limiter = [:rate_limiter] || RateLimiter.new([:rate_limiting] || {}) @api_url = strip_api_url(@api_url) end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
14 15 16 |
# File 'lib/lutaml/hal/client.rb', line 14 def api_url @api_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
14 15 16 |
# File 'lib/lutaml/hal/client.rb', line 14 def connection @connection end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
14 15 16 |
# File 'lib/lutaml/hal/client.rb', line 14 def last_response @last_response end |
#rate_limiter ⇒ Object (readonly)
Returns the value of attribute rate_limiter.
14 15 16 |
# File 'lib/lutaml/hal/client.rb', line 14 def rate_limiter @rate_limiter end |
Instance Method Details
#get(url, params = {}) ⇒ Object
Make a GET request to the API
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lutaml/hal/client.rb', line 41 def get(url, params = {}) cache_key = "#{url}:#{params.to_json}" return @cache[cache_key] if @cache_enabled && @cache.key?(cache_key) response = @rate_limiter.with_rate_limiting do @last_response = @connection.get(url, params) handle_response(@last_response, url) end @cache[cache_key] = response if @cache_enabled response rescue Faraday::ConnectionFailed => e raise Lutaml::Hal::ConnectionError, "Connection failed: #{e.}" rescue Faraday::TimeoutError => e raise Lutaml::Hal::TimeoutError, "Request timed out: #{e.}" rescue Faraday::ParsingError => e raise Lutaml::Hal::ParsingError, "Response parsing error: #{e.}" rescue Faraday::Adapter::Test::Stubs::NotFound => e raise Lutaml::Hal::LinkResolutionError, "Resource not found: #{e.}" end |
#get_by_url(url, params = {}) ⇒ Object
Get a resource by its full URL
34 35 36 37 38 |
# File 'lib/lutaml/hal/client.rb', line 34 def get_by_url(url, params = {}) # Strip API endpoint if it's included path = strip_api_url(url) get(path, params) end |
#get_with_headers(url, headers = {}) ⇒ Object
Make a GET request with custom headers
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lutaml/hal/client.rb', line 64 def get_with_headers(url, headers = {}) cache_key = "#{url}:#{headers.to_json}" return @cache[cache_key] if @cache_enabled && @cache.key?(cache_key) response = @rate_limiter.with_rate_limiting do @last_response = @connection.get(url) do |req| headers.each { |key, value| req.headers[key] = value } end handle_response(@last_response, url) end @cache[cache_key] = response if @cache_enabled response rescue Faraday::ConnectionFailed => e raise Lutaml::Hal::ConnectionError, "Connection failed: #{e.}" rescue Faraday::TimeoutError => e raise Lutaml::Hal::TimeoutError, "Request timed out: #{e.}" rescue Faraday::ParsingError => e raise Lutaml::Hal::ParsingError, "Response parsing error: #{e.}" rescue Faraday::Adapter::Test::Stubs::NotFound => e raise Lutaml::Hal::LinkResolutionError, "Resource not found: #{e.}" end |
#strip_api_url(url) ⇒ Object
Strip any trailing slash from the API URL
29 30 31 |
# File 'lib/lutaml/hal/client.rb', line 29 def strip_api_url(url) url.sub(%r{/\Z}, '') end |