Class: Spine::Http::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Request

Returns a new instance of Request.


9
10
11
12
13
# File 'lib/spine/http/request.rb', line 9

def initialize(uri)
  @uri = URI.parse(uri)
  @headers = {}
  @payload = ''
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers


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

def headers
  @headers
end

#uriObject (readonly)

Returns the value of attribute uri


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

def uri
  @uri
end

Instance Method Details

#deleteObject


52
53
54
55
# File 'lib/spine/http/request.rb', line 52

def delete
  request = Net::HTTP::Delete.new(uri.request_uri)
  execute(request)
end

#getObject


30
31
32
# File 'lib/spine/http/request.rb', line 30

def get
  execute(Net::HTTP::Get.new(uri.request_uri))
end

#header(key, value) ⇒ Object


20
21
22
23
# File 'lib/spine/http/request.rb', line 20

def header(key, value)
  headers[key] = value
  self
end

#patchObject


40
41
42
43
44
# File 'lib/spine/http/request.rb', line 40

def patch
  request = Net::HTTP::Patch.new(uri.request_uri)
  add_payload(request)
  execute(request)
end

#payload(data, content_type = ContentTypes::Plain) ⇒ Object


25
26
27
28
# File 'lib/spine/http/request.rb', line 25

def payload(data, content_type = ContentTypes::Plain)
  @payload = content_type.dump(data)
  self
end

#postObject


34
35
36
37
38
# File 'lib/spine/http/request.rb', line 34

def post
  request = Net::HTTP::Post.new(uri.request_uri)
  add_payload(request)
  execute(request)
end

#putObject


46
47
48
49
50
# File 'lib/spine/http/request.rb', line 46

def put
  request = Net::HTTP::Put.new(uri.request_uri)
  add_payload(request)
  execute(request)
end

#query(parameters) ⇒ Object


15
16
17
18
# File 'lib/spine/http/request.rb', line 15

def query(parameters)
  uri.query = URI.encode_www_form(parameters)
  self
end