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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rigor/plugin/io_boundary.rb', line 117

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