class Gems::RequestBuilder

Builds HTTP requests for the RubyGems API @api private

Constants

APPLICATION_JSON

Content type for JSON request bodies

DEFAULT_USER_AGENT

The default ‘User-Agent’ HTTP header

The Ruby it runs on is named along with the library and its version, as ‘gem push` names it, so that the RubyGems.org operators can tell which client and which Ruby a request came from.

MULTIPART_FORM_DATA

Content type for multipart request bodies

OCTET_STREAM

Content type for binary request bodies

Attributes

The ‘User-Agent’ HTTP header sent with requests @api private @return [String] the user agent @example Get or set the user agent

builder.user_agent = "Custom User Agent"

Public Class Methods

Initialize a new RequestBuilder

@api private @param user_agent [String] the ‘User-Agent’ HTTP header sent with requests @return [RequestBuilder] a new instance @example Create a request builder

builder = Gems::RequestBuilder.new(user_agent: "Custom User Agent")
# File lib/gems/request_builder.rb, line 47
def initialize(user_agent: DEFAULT_USER_AGENT)
  @user_agent = user_agent
end

Public Instance Methods

Build an HTTP request

The body may be a Hash (sent as a form-encoded body), an Array of multipart fields (sent as multipart/form-data), a String (sent with the given content type), or an open file (sent as a stream with the given content type).

@api private @param http_method [Symbol] the HTTP method (:get, :post, :put, :patch, :delete) @param uri [URI::Generic] the request URI @param params [Hash] query parameters to append to the URI @param body [Hash, Array, String, File, nil] the request body @param content_type [String, nil] the content type for a String body (defaults to application/octet-stream) @param headers [Hash] additional headers for the request @param authenticator [Authenticator] the authenticator for the request @return [Net::HTTPRequest] the built HTTP request @raise [ArgumentError] if the HTTP method is not supported @example Build a GET request

builder.build(http_method: :get, uri: URI("https://rubygems.org/api/v1/gems/rails.json"))
# File lib/gems/request_builder.rb, line 69
def build(http_method:, uri:, params: {}, body: nil, content_type: nil, headers: {}, authenticator: Authenticator.new)
  request = create_request(http_method:, uri:, params:)
  add_headers(request:, headers:)
  add_body(request:, body:, content_type:)
  add_authentication(request:, authenticator:)
  request
end