Class: Weary::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, http_verb = :get, options = {}) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
# File 'lib/weary/request.rb', line 7

def initialize(url, http_verb= :get, options={})
  self.method = http_verb
  self.uri = url
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/weary/request.rb', line 5

def options
  @options
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/weary/request.rb', line 4

def uri
  @uri
end

Instance Method Details

#methodObject



34
35
36
# File 'lib/weary/request.rb', line 34

def method
  @http_verb
end

#method=(http_verb) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/weary/request.rb', line 17

def method=(http_verb)
  @http_verb = case http_verb
    when *Methods[:get]
      :get
    when *Methods[:post]
      :post
    when *Methods[:put]
      :put
    when *Methods[:delete]
      :delete
    when *Methods[:head]
      :head
    else
      raise ArgumentError, "Only GET, POST, PUT, DELETE, and HEAD methods are supported"
  end
end

#performObject Also known as: make



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/weary/request.rb', line 38

def perform
  req = http.request(request)
  response = Response.new(req, @http_verb)
  unless options[:no_follow]
    if response.redirected?
      response.follow_redirect
    else
      response
    end
  else
    response
  end
end