class ApiTransformer::BackendResponse
Container for backend response data
Attributes
body[R]
Public Class Methods
new(http)
click to toggle source
# File lib/api_transformer/backend_response.rb, line 14 def initialize(http) @http = http @body = "" @http.stream { |chunk| @body += chunk } end
Public Instance Methods
[](key)
click to toggle source
# File lib/api_transformer/backend_response.rb, line 43 def [](key) if json.respond_to?(:keys) json[key] else nil end end
content_type()
click to toggle source
# File lib/api_transformer/backend_response.rb, line 61 def content_type @http.response_header["CONTENT_TYPE"] end
headers()
click to toggle source
# File lib/api_transformer/backend_response.rb, line 65 def headers @http.response_header end
json()
click to toggle source
# File lib/api_transformer/backend_response.rb, line 29 def json @data ||= begin json = JSON.parse(@body) if json.respond_to?(:keys) Data.try_convert(json) else json end end rescue JSON::ParserError, Encoding::InvalidByteSequenceError {} end
status()
click to toggle source
# File lib/api_transformer/backend_response.rb, line 25 def status @http.response_header.http_status end
stream(&block)
click to toggle source
# File lib/api_transformer/backend_response.rb, line 69 def stream(&block) if block block.call(@body) @http.stream(&block) EM::Synchrony::Iterator.new([block]).each do |_, iter| @http.callback { iter.next } end else fail "a block is required when streaming" end end
success?()
click to toggle source
# File lib/api_transformer/backend_response.rb, line 21 def success? status >= 200 && status < 300 end