Class: Rigor::Plugin::DefaultHttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/plugin/io_boundary.rb

Overview

Default HTTP client wrapping ‘Net::HTTP`. Wraps a single `GET` over HTTPS. Specs inject a fake client that conforms to the same `#get(url, timeout:, max_bytes:)` shape so the tests don’t require network access.

Instance Method Summary collapse

Instance Method Details

#get(url, timeout:, max_bytes:) ⇒ Object

rubocop:disable Metrics/MethodLength



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rigor/plugin/io_boundary.rb', line 141

def get(url, timeout:, max_bytes:)
  require "net/http"
  require "uri"

  uri = URI.parse(url)
  body = +""
  Net::HTTP.start(uri.host, uri.port, use_ssl: true,
                                      open_timeout: timeout,
                                      read_timeout: timeout) do |http|
    http.request_get(uri.request_uri) do |response|
      unless response.is_a?(Net::HTTPSuccess)
        raise Plugin::AccessDeniedError.new(
          "URL #{url.inspect} returned non-success status #{response.code}",
          reason: :url_fetch_failed,
          resource: url
        )
      end
      response.read_body do |chunk|
        body << chunk
        if body.bytesize > max_bytes
          raise Plugin::AccessDeniedError.new(
            "URL #{url.inspect} body exceeds #{max_bytes} bytes",
            reason: :url_body_too_large,
            resource: url
          )
        end
      end
    end
  end
  body
end