Class: Weary::Response
- Inherits:
-
Object
- Object
- Weary::Response
- Defined in:
- lib/weary/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#content_type ⇒ Object
(also: #mime_type)
readonly
Returns the value of attribute content_type.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #follow_redirect ⇒ Object
- #format ⇒ Object
- #format=(type) ⇒ Object
-
#initialize(http_response, http_method) ⇒ Response
constructor
A new instance of Response.
- #parse ⇒ Object
- #redirected? ⇒ Boolean
- #search(selector) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(http_response, http_method) ⇒ Response
Returns a new instance of Response.
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 = http_response. @header = http_response.to_hash @content_type = http_response.content_type = http_response['Set-Cookie'] @body = http_response.body self.format = http_response.content_type end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def code @code end |
#content_type ⇒ Object (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 |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def header @header end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
4 5 6 |
# File 'lib/weary/response.rb', line 4 def method @method end |
#raw ⇒ Object (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_redirect ⇒ Object
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 |
#format ⇒ Object
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 |
#parse ⇒ Object
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
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 |