Class: Weary::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, http_method) ⇒ Response

Returns a new instance of Response.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/weary/response.rb', line 7

def initialize(http_response, http_method)
  raise ArgumentError, "Must be a Net::HTTPResponse" unless http_response.is_a?(Net::HTTPResponse)
  @raw = http_response
  @method = http_method
  @code = http_response.code.to_i
  @message = http_response.message
  @header = http_response.to_hash
  @content_type = http_response.content_type
  @cookie = http_response['Set-Cookie']
  @body = http_response.body
  self.format = http_response.content_type
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#content_typeObject (readonly) Also known as: mime_type

Returns the value of attribute content_type.



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

def content_type
  @content_type
end

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Instance Method Details

#follow_redirectObject



49
50
51
52
53
54
55
# File 'lib/weary/response.rb', line 49

def follow_redirect
  if redirected?
    Request.new(@raw['location'], @method).perform
  else
    nil
  end
end

#formatObject



45
46
47
# File 'lib/weary/response.rb', line 45

def format
  @format
end

#format=(type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/weary/response.rb', line 28

def format=(type)
  @format = case type
    when 'text/xml', 'application/xml'
      :xml
    when 'application/json', 'text/json', 'application/javascript', 'text/javascript'
      :json
    when 'text/html'
      :html
    when 'application/x-yaml', 'text/yaml'
      :yaml
    when 'text/plain'
      :plain
    else
      nil
  end
end

#parseObject

Raises:

  • (StandardError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/weary/response.rb', line 57

def parse
  raise StandardError, "The Response has no body. #{@method.to_s.upcase} request sent." unless @body
  handle_errors
  case @format
    when :xml, :html
      Crack::XML.parse @body
    when :json
      Crack::JSON.parse @body
    when :yaml
      YAML::load @body
    else
      @body
  end
end

#redirected?Boolean



20
21
22
# File 'lib/weary/response.rb', line 20

def redirected?
  @raw.is_a?(Net::HTTPRedirection)
end

#search(selector) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
# File 'lib/weary/response.rb', line 72

def search(selector)
  raise ArgumentError, "Search can only be used with an XML or HTML document." unless @format != (:xml || :html)
  doc = Nokogiri.parse(@body)
  doc.search(selector)
end

#success?Boolean



24
25
26
# File 'lib/weary/response.rb', line 24

def success?
  (200..299).include?(@code)
end