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, represented as a ruby data structure. 
- 
  
    
      #json_value  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Like json, but wrapped in a CouchShell::JsonValue. 
- #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_value = nil @computed_json_value = false end | 
Instance Method Details
#attr(name, altname = nil) ⇒ Object
| 66 67 68 69 70 71 72 73 74 75 76 | # File 'lib/couch-shell/response.rb', line 66 def attr(name, altname = nil) name = name.to_sym altname = altname ? altname.to_sym : nil if json_value if json_value.respond_to?(name) json_value.__send__ name elsif altname && json_value.respond_to?(altname) json_value.__send__ altname end end end | 
#body ⇒ Object
| 56 57 58 | # File 'lib/couch-shell/response.rb', line 56 def body @res.respond_to?(:content) ? @res.content : @res.body end | 
#code ⇒ Object
| 44 45 46 | # File 'lib/couch-shell/response.rb', line 44 def code @res.respond_to?(:status) ? @res.status.to_s : @res.code end | 
#content_type ⇒ Object
| 60 61 62 63 64 | # File 'lib/couch-shell/response.rb', line 60 def content_type @res.respond_to?(:content_type) ? @res.content_type : @res.contenttype.sub(/;[^;]*\z/, '') end | 
#json ⇒ Object
Response body parsed as json, represented as a ruby data structure. nil if body is empty, false if parsing failed.
| 24 25 26 | # File 'lib/couch-shell/response.rb', line 24 def json json_value ? json_value.unwrapped! : json_value end | 
#json_value ⇒ Object
Like json, but wrapped in a CouchShell::JsonValue.
| 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # File 'lib/couch-shell/response.rb', line 29 def json_value unless @computed_json_value if JSON_CONTENT_TYPES.include?(content_type) && !body.nil? && !body.empty? begin @json_value = JsonValue.wrap(JSON.parse(body)) rescue JSON::ParserError @json_value = false end end @computed_json_value = true end @json_value end | 
#message ⇒ Object
| 48 49 50 | # File 'lib/couch-shell/response.rb', line 48 def @res.respond_to?(:message) ? @res. : @res.reason end | 
#ok? ⇒ Boolean
| 52 53 54 | # File 'lib/couch-shell/response.rb', line 52 def ok? code.start_with?("2") end |