Class: X::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/x/request_builder.rb

Overview

Builds HTTP requests for the X API

Constant Summary collapse

DEFAULT_HEADERS =

Default headers for API requests

{
  "Content-Type" => "application/json; charset=utf-8",
  "User-Agent" => "X-Client/#{VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PLATFORM})"
}.freeze
HTTP_METHODS =

Mapping of HTTP method symbols to Net::HTTP classes

{
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  put: Net::HTTP::Put,
  delete: Net::HTTP::Delete
}.freeze

Instance Method Summary collapse

Instance Method Details

#build(http_method:, uri:, body: nil, headers: {}, authenticator: Authenticator.new) ⇒ Net::HTTPRequest

Build an HTTP request

Examples:

Build a GET request

builder.build(http_method: :get, uri: URI("https://api.x.com/2/users/me"))

Parameters:

  • http_method (Symbol)

    the HTTP method (:get, :post, :put, :delete)

  • uri (URI)

    the request URI

  • body (String, nil) (defaults to: nil)

    the request body

  • headers (Hash) (defaults to: {})

    additional headers for the request

  • authenticator (Authenticator) (defaults to: Authenticator.new)

    the authenticator for the request

Returns:

  • (Net::HTTPRequest)

    the built HTTP request

Raises:

  • (ArgumentError)

    if the HTTP method is not supported



35
36
37
38
39
40
# File 'lib/x/request_builder.rb', line 35

def build(http_method:, uri:, body: nil, headers: {}, authenticator: Authenticator.new)
  request = create_request(http_method:, uri:, body:)
  add_headers(request:, headers:)
  add_authentication(request:, authenticator:)
  request
end