Sha256: e4381ddbfe83da9d6cef3a862e12eaa144a5615ca05afc671bfec90d5a2e2616

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

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
  attr_reader :from_cache

  delegate :effective_url, :code, :headers, :options, :mock, :success?, to: :raw
  delegate :error_ignored?, to: :request
  alias from_cache? from_cache

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

  def data
    @data ||= body.present? ? LHC::Response::Data.new(self) : nil
  end

  def [](key)
    data[key]
  end

  def body
    body_replacement || raw.body.presence
  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.5.0 lib/lhc/response.rb
lhc-6.4.0 lib/lhc/response.rb