Class: KirinHttp::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/kirin_http/http_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, method = :get, content = nil, header = {}) ⇒ Message

Constructor

endpoint = “host.com/post/1?id=5” data = “name”:“John”, “country”: “singapore” headers = “application/json”, “Accept”: “text/plain” message = KirinHttpClient::HttpMessage.new(endpoint, :post, data.to_json, headers) # Use HttpClient to send the message

Parameters:

  • url (String)

    the full endpoint to query

  • method (Symbol) (defaults to: :get)

    the http verb. Accepted symbols :get :post :put :patch :delete :head. Default is :get

  • content (Object) (defaults to: nil)

    the request body. Can be any type. Defaults to nil. Put nil if there is no body

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

    the header of the request. Defaults to empty hash



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kirin_http/http_message.rb', line 18

def initialize(url, method = :get, content = nil, header = {})
  uri = URI.parse(url)
  @host = uri.host
  @port = uri.port
  @path = uri.request_uri
  @ssl = uri.scheme == "https"
  @header = header
  @header["Host"] = uri.host
  @method = method
  @content = content
  @uri = url
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/kirin_http/http_message.rb', line 4

def content
  @content
end

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def header
  @header
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def host
  @host
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/kirin_http/http_message.rb', line 4

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def ssl
  @ssl
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/kirin_http/http_message.rb', line 3

def uri
  @uri
end

Instance Method Details

#url=(value) ⇒ Object

Endpoint of the Http Message

Parameters:

  • value (String)

    the endpoint in full



33
34
35
36
37
38
39
40
41
# File 'lib/kirin_http/http_message.rb', line 33

def url=(value)
  uri = URI.parse(value)
  @host = uri.host
  @port = uri.port
  @path = uri.request_uri
  @ssl = uri.scheme == "https"
  @uri = value
  @header["Host"] = uri.host
end