Class: CongregaPlenum::HttpAdapter
- Inherits:
-
Object
- Object
- CongregaPlenum::HttpAdapter
- Defined in:
- lib/adapters/http_adapter.rb,
sig/congrega_plenum.rbs
Overview
Encapsulates Net::HTTP details and shared configuration. Centralising the HTTP wiring keeps the client implementation focused on retries and parsing.
Instance Attribute Summary collapse
-
#configuration ⇒ Configuration
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
-
#build_http_client(uri) ⇒ ::Net::HTTP
Builds the configured Net::HTTP client honoring SSL and timeouts.
-
#build_http_request(uri) ⇒ ::Net::HTTP::Get
Prepares a GET request with headers that make the API happy (user agent and content type).
-
#get(url) ⇒ ::Net::HTTPResponse
Executes a GET request and returns the raw Net::HTTPResponse.
-
#initialize(configuration: CongregaPlenum.configuration) ⇒ HttpAdapter
constructor
Creates a new adapter using the provided configuration.
- #logger ⇒ Logger
Constructor Details
#initialize(configuration: CongregaPlenum.configuration) ⇒ HttpAdapter
Creates a new adapter using the provided configuration. Injecting the configuration makes it trivial to plug test doubles or tweak settings when using multiple clients simultaneously.
10 11 12 |
# File 'lib/adapters/http_adapter.rb', line 10 def initialize(configuration: CongregaPlenum.configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Configuration (readonly)
Returns the value of attribute configuration.
30 31 32 |
# File 'lib/adapters/http_adapter.rb', line 30 def configuration @configuration end |
Instance Method Details
#build_http_client(uri) ⇒ ::Net::HTTP
Builds the configured Net::HTTP client honoring SSL and timeouts. Keeping this logic here avoids repetition if a different adapter is introduced.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/adapters/http_adapter.rb', line 34 def build_http_client(uri) host = uri.host raise URI::InvalidURIError, 'HTTP URL must include a host' unless host Net::HTTP.new(host, uri.port).tap do |http| http.use_ssl = uri.scheme == 'https' timeout = configuration.timeout http.read_timeout = timeout http.open_timeout = timeout end end |
#build_http_request(uri) ⇒ ::Net::HTTP::Get
Prepares a GET request with headers that make the API happy (user agent and content type). Any change requested by the Câmara ficará concentrada aqui.
48 49 50 51 52 53 |
# File 'lib/adapters/http_adapter.rb', line 48 def build_http_request(uri) Net::HTTP::Get.new(uri).tap do |request| request['User-Agent'] = 'CongregaPlenum/1.0' request['Accept'] = 'application/json' end end |
#get(url) ⇒ ::Net::HTTPResponse
Executes a GET request and returns the raw Net::HTTPResponse. All network errors of interest are mapped to ConnectionError so upper layers can treat them uniformly.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/adapters/http_adapter.rb', line 17 def get(url) uri = URI(url) http = build_http_client(uri) request = build_http_request(uri) logger.debug("CongregaPlenum: Requisição GET para #{url}") http.request(request) rescue Timeout::Error, SocketError, Errno::ECONNRESET => e raise ConnectionError, "Connection failed for #{url}: #{e.}" end |
#logger ⇒ Logger
55 56 57 |
# File 'lib/adapters/http_adapter.rb', line 55 def logger configuration.logger end |