Sha256: 309497f776fae078caea26ee0a0f2dd6edcf6a4d6cfa5e16071e6680210006fa

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

require "json"

class Hypernova::Response
  def initialize(request)
    @request = request
  end

  # Example parsed body with no error:
  # {
  #   "0" => {
  #     "name" => "hello_world.js",
  #     "html" => "<div>Hello World</div>",
  #     "meta" => {},
  #     "duration" => 100,
  #     "statusCode" => 200,
  #     "success" => true,
  #     "error" => nil,
  #   }
  # }

  # Example parsed body with error:
  # {
  #   "0" => {
  #     "html" => "<p>Error!</p>",
  #     "name" => "goodbye_galaxy.js",
  #     "meta" => {},
  #     "duration" => 100,
  #     "statusCode" => 500,
  #     "success" => false,
  #     "error" => {
  #       "name" => "TypeError",
  #       "message" => "Cannot read property 'forEach' of undefined",
  #       "stack" => [
  #         "TypeError: Cannot read property 'forEach' of undefined",
  #         "at TravelerLanding.componentWillMount",
  #         "at ReactCompositeComponentMixin.mountComponent",
  #       ],
  #     },
  #   }
  # }
  def parsed_body
    response = parse_body
    # This enables backward compatibility with the old server response format.
    # In the new format, the response results are contained within a "results" key. The top level
    # hash contains a "success" and "error" which relates to the whole batch.
    response = response["results"] || response
  end

  private

  attr_reader :request

  def body
    request.body
  end

  def parse_body
    JSON.parse(body)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hypernova-2.0.0 lib/hypernova/response.rb
hypernova-1.4.0 lib/hypernova/response.rb
hypernova-1.3.0 lib/hypernova/response.rb
hypernova-1.2.0 lib/hypernova/response.rb
hypernova-1.1.0 lib/hypernova/response.rb
hypernova-1.0.3 lib/hypernova/response.rb
hypernova-1.0.2 lib/hypernova/response.rb