Class: SpecForge::HTTP::Request

Inherits:
Struct
  • Object
show all
Defined in:
lib/spec_forge/http/request.rb

Overview

Represents an HTTP request with all its components

Request is a value object that holds the URL, method, headers, query parameters, and body for an HTTP request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Request

Creates a new HTTP request with the specified options

Parameters:

  • options (Hash)

    Request options

Options Hash (**options):

  • :base_url (String)

    The base URL for the request

  • :url (String)

    The URL path for the request

  • :http_verb (String)

    The HTTP method (defaults to "GET")

  • :headers (Hash)

    HTTP headers

  • :query (Hash)

    Query parameters

  • :body (Hash, String)

    Request body



25
26
27
28
29
30
31
32
33
34
# File 'lib/spec_forge/http/request.rb', line 25

def initialize(**options)
  super(
    base_url: options[:base_url] || "",
    url: options[:url] || "",
    http_verb: Verb.from(options[:http_verb].presence || "GET"),
    headers: options[:headers] || {},
    query: options[:query] || {},
    body: options[:body] || {}
  )
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url

Returns:

  • (Object)

    the current value of base_url



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def base_url
  @base_url
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def headers
  @headers
end

#http_verbObject

Returns the value of attribute http_verb

Returns:

  • (Object)

    the current value of http_verb



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def http_verb
  @http_verb
end

#queryObject

Returns the value of attribute query

Returns:

  • (Object)

    the current value of query



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def query
  @query
end

#urlObject

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



11
12
13
# File 'lib/spec_forge/http/request.rb', line 11

def url
  @url
end

Instance Method Details

#content_typeString?

Returns the Content-Type header value

Returns:

  • (String, nil)

    The content type or nil if not set



41
42
43
# File 'lib/spec_forge/http/request.rb', line 41

def content_type
  headers["content-type"]
end

#json?Boolean

Returns whether this request has a JSON content type

Returns:

  • (Boolean)

    True if content type is application/json



50
51
52
# File 'lib/spec_forge/http/request.rb', line 50

def json?
  content_type == "application/json"
end

#to_hHash

Converts the request to a hash with stringified verb

Returns:

  • (Hash)

    Hash representation of the request



59
60
61
62
63
# File 'lib/spec_forge/http/request.rb', line 59

def to_h
  super.tap do |h|
    h[:http_verb] = h[:http_verb].to_s
  end
end