Class: Simple::HTTP::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



2
3
4
# File 'lib/simple/http/request.rb', line 2

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



2
3
4
# File 'lib/simple/http/request.rb', line 2

def headers
  @headers
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/simple/http/request.rb', line 2

def url
  @url
end

#verbObject (readonly)

Returns the value of attribute verb.



2
3
4
# File 'lib/simple/http/request.rb', line 2

def verb
  @verb
end

Class Method Details

.build(verb:, url:, body: nil, headers:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/simple/http/request.rb', line 5

def build(verb:, url:, body: nil, headers:)
  if body_permitted?(verb) && !headers.key?("Content-Type")
    if body.is_a?(Hash) || body.is_a?(Array)
      headers["Content-Type"] = "application/json"
      body = JSON.generate(body)
    end
  end

  new verb, url, body, headers
end

Instance Method Details

#to_sObject



36
37
38
39
# File 'lib/simple/http/request.rb', line 36

def to_s
  scrubbed_url = url.gsub(/\/\/(.*):(.*)@/) { |_| "//#{$1}:xxxxxx@" }
  "#{verb} #{scrubbed_url}"
end