Class: SeaShanty::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method:, url:, headers:, body:) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sea_shanty/request.rb', line 9

def initialize(method:, url:, headers:, body:)
  @method = method
  @url = case url
  when String
    URI.parse(url)
  when URI
    url
  else
    raise ArgumentError, "url should be a String or an URI"
  end
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/sea_shanty/request.rb', line 7

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/sea_shanty/request.rb', line 7

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/sea_shanty/request.rb', line 7

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/sea_shanty/request.rb', line 7

def url
  @url
end

Instance Method Details

#to_hObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sea_shanty/request.rb', line 23

def to_h
  {
    method: method.to_s,
    url: url.to_s,
    headers: headers,
    body: {
      string: body.to_s,
      encoding: body.nil? ? "" : body.encoding.name
    }
  }
end