Class: Ksef::Internal::Connection
- Inherits:
-
Object
- Object
- Ksef::Internal::Connection
- Defined in:
- lib/ksef/internal/connection.rb
Overview
Thin Faraday wrapper around the KSeF HTTP API.
All response-shape parsing and error classification lives here so the higher-level resource classes can treat the API as a typed boundary.
Constant Summary collapse
- RETRY_OPTIONS =
{ max: 2, interval: 0.4, interval_randomness: 0.2, backoff_factor: 2, retry_statuses: [502, 503, 504], methods: %i[get head post delete put patch], # `Faraday::RetriableResponse` MUST stay in this list: faraday-retry # raises it internally to trigger a `retry_statuses` retry. Because we # override `exceptions` (rather than inheriting the middleware default, # which includes it), omitting it silently disables 502/503/504 retries # *and* leaks the raw synthetic exception past `check!` instead of # mapping it to a typed `Ksef::ServerError`. exceptions: [ Errno::ETIMEDOUT, Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::RetriableResponse ] }.freeze
- JSON_CONTENT_TYPE =
"application/json"
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Class Method Summary collapse
-
.parse_json(response) ⇒ Object
Parses a JSON response body, returning
{}on empty bodies.
Instance Method Summary collapse
-
#initialize(configuration) ⇒ Connection
constructor
A new instance of Connection.
-
#request(method, path, body: nil, headers: {}, query: {}, bearer_token: nil) ⇒ Faraday::Response
Issues an HTTP request and returns a
Faraday::Response.
Constructor Details
#initialize(configuration) ⇒ Connection
Returns a new instance of Connection.
39 40 41 |
# File 'lib/ksef/internal/connection.rb', line 39 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
37 38 39 |
# File 'lib/ksef/internal/connection.rb', line 37 def configuration @configuration end |
Class Method Details
.parse_json(response) ⇒ Object
Parses a JSON response body, returning {} on empty bodies.
63 64 65 66 67 68 69 |
# File 'lib/ksef/internal/connection.rb', line 63 def self.parse_json(response) return {} if response.body.nil? || response.body.to_s.empty? JSON.parse(response.body) rescue JSON::ParserError {} end |
Instance Method Details
#request(method, path, body: nil, headers: {}, query: {}, bearer_token: nil) ⇒ Faraday::Response
Issues an HTTP request and returns a Faraday::Response.
52 53 54 55 56 57 58 59 60 |
# File 'lib/ksef/internal/connection.rb', line 52 def request(method, path, body: nil, headers: {}, query: {}, bearer_token: nil) response = http.run_request(method, (path), nil, build_headers(headers, bearer_token)) do |req| req.params.update(query) unless query.empty? assign_body(req, body) end check!(response) response end |