Class: Lutaml::Hal::Client
- Inherits:
-
Object
- Object
- Lutaml::Hal::Client
- Defined in:
- lib/lutaml/hal/client.rb
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
- #get_by_url(url, params = {}) ⇒ Object
- #get_by_url_with_headers(url, headers = {}) ⇒ Object
- #get_with_headers(url, headers = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #strip_api_url(url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 |
# File 'lib/lutaml/hal/client.rb', line 13 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? @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.
11 12 13 |
# File 'lib/lutaml/hal/client.rb', line 11 def api_url @api_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
11 12 13 |
# File 'lib/lutaml/hal/client.rb', line 11 def connection @connection end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
11 12 13 |
# File 'lib/lutaml/hal/client.rb', line 11 def last_response @last_response end |
#rate_limiter ⇒ Object (readonly)
Returns the value of attribute rate_limiter.
11 12 13 |
# File 'lib/lutaml/hal/client.rb', line 11 def rate_limiter @rate_limiter end |
Instance Method Details
#get(url, params = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lutaml/hal/client.rb', line 36 def get(url, params = {}) @rate_limiter.with_rate_limiting do @last_response = @connection.get(url, params) handle_response(@last_response, url) end 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
26 27 28 29 |
# File 'lib/lutaml/hal/client.rb', line 26 def get_by_url(url, params = {}) path = strip_api_url(url) get(path, params) end |
#get_by_url_with_headers(url, headers = {}) ⇒ Object
31 32 33 34 |
# File 'lib/lutaml/hal/client.rb', line 31 def get_by_url_with_headers(url, headers = {}) path = strip_api_url(url) get_with_headers(path, headers) end |
#get_with_headers(url, headers = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lutaml/hal/client.rb', line 51 def get_with_headers(url, headers = {}) @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 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 |
#strip_api_url(url) ⇒ Object
22 23 24 |
# File 'lib/lutaml/hal/client.rb', line 22 def strip_api_url(url) url.sub(%r{/\Z}, '') end |