lib/cassette-rack/response.rb in cassette-rack-0.4.1 vs lib/cassette-rack/response.rb in cassette-rack-0.5.0
- old
+ new
@@ -1,19 +1,56 @@
+#require 'json'
+
module CassetteRack
class Response
- attr_reader :status, :headers, :body, :method
+ attr_reader :status, :headers, :body, :content
+ #, :method
- def initialize(response)
- @status = response.status
- @headers = response.headers
- @body = response.body
+ def initialize(res)
+ @response = res
+ @status = res.status
+ @headers = res.headers
+ @body = res.body
+ parse_content
end
def status_code
status
end
def response_headers
headers
end
+
+ def success?
+ @response.success?
+ end
+
+ def permit(*keys)
+ case content
+ when Hash
+ content.select { |key| keys.include? key }
+ when Array
+ content.map { |item| item.select { |key| keys.include? key } }
+ end
+ end
+
+ private
+ def parse_content
+ case body
+ when nil, '', ' '
+ @content = nil
+ when 'true'
+ @content = true
+ when 'false'
+ @content = false
+ else
+ if @response.env.request_headers['accept'] == 'application/json'
+ @content = JSON.parse(body)
+ else
+ @content = body
+ end
+ end
+ end
+ # end private
end
end