Sha256: 60355546bad3763de3fba6565650f5a41a9dc05edb43489b7d7994e35708d27e

Contents?: true

Size: 948 Bytes

Versions: 2

Compression:

Stored size: 948 Bytes

Contents

require 'typhoeus'
require 'active_support/core_ext/module'

# The response contains the raw response (typhoeus)
# and provides functionality to access response data.
class LHC::Response
  autoload :Data, 'lhc/response/data'

  attr_accessor :request, :body_replacement

  delegate :effective_url, :code, :headers, :options, :mock, :success?, to: :raw

  # A response is initalized with the underlying raw response (typhoeus in our case)
  # and the associated request.
  def initialize(raw, request)
    self.request = request
    self.raw = raw
  end

  def data
    @data ||= LHC::Response::Data.new(self)
  end

  def [](key)
    data[key]
  end

  def body
    body_replacement || raw.body
  end

  # Provides response time in ms.
  def time
    (raw.time || 0) * 1000
  end

  def timeout?
    raw.timed_out?
  end

  def format
    return LHC::Formats::JSON.new if request.nil?
    request.format
  end

  private

  attr_accessor :raw

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lhc-6.0.1 lib/lhc/response.rb
lhc-6.0.0 lib/lhc/response.rb