Class: CouchShell::Response
- Inherits:
-
Object
- Object
- CouchShell::Response
- Defined in:
- lib/couch-shell/response.rb
Constant Summary collapse
- JSON_CONTENT_TYPES =
[ "application/json", "text/plain" ].freeze
Instance Method Summary collapse
- #attr(name, altname = nil) ⇒ Object
- #body ⇒ Object
- #code ⇒ Object
- #content_type ⇒ Object
-
#initialize(response) ⇒ Response
constructor
responseis a HTTP::Message from httpclient library, or a Net::HTTP response. -
#json ⇒ Object
Response body parsed as json.
- #message ⇒ Object
- #ok? ⇒ Boolean
Constructor Details
#initialize(response) ⇒ Response
response is a HTTP::Message from httpclient library, or a Net::HTTP response
16 17 18 19 20 |
# File 'lib/couch-shell/response.rb', line 16 def initialize(response) @res = response @json = nil @computed_json = false end |
Instance Method Details
#attr(name, altname = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/couch-shell/response.rb', line 61 def attr(name, altname = nil) name = name.to_sym altname = altname ? altname.to_sym : nil if json if json.respond_to?(name) json.__send__ name elsif altname && json.respond_to?(altname) json.__send__ altname end end end |
#body ⇒ Object
51 52 53 |
# File 'lib/couch-shell/response.rb', line 51 def body @res.respond_to?(:content) ? @res.content : @res.body end |
#code ⇒ Object
39 40 41 |
# File 'lib/couch-shell/response.rb', line 39 def code @res.respond_to?(:status) ? @res.status.to_s : @res.code end |
#content_type ⇒ Object
55 56 57 58 59 |
# File 'lib/couch-shell/response.rb', line 55 def content_type @res.respond_to?(:content_type) ? @res.content_type : @res.contenttype.sub(/;[^;]*\z/, '') end |
#json ⇒ Object
Response body parsed as json. nil if body is empty, false if parsing failed.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/couch-shell/response.rb', line 24 def json unless @computed_json if JSON_CONTENT_TYPES.include?(content_type) && !body.nil? && !body.empty? begin @json = JsonValue.wrap(JSON.parse(body)) rescue JSON::ParserError @json = false end end @computed_json = true end @json end |
#message ⇒ Object
43 44 45 |
# File 'lib/couch-shell/response.rb', line 43 def @res.respond_to?(:message) ? @res. : @res.reason end |
#ok? ⇒ Boolean
47 48 49 |
# File 'lib/couch-shell/response.rb', line 47 def ok? code.start_with?("2") end |