Sha256: 20ac83afcce2d08e2c444c9f2d7fc00bc955a3a5585dc334b5665eb2b61aa395

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

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

  delegate :effective_url, :code, :headers, :options, :mock, :success?, to: :raw
  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 ||= 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

3 entries across 3 versions & 1 rubygems

Version Path
lhc-6.1.2 lib/lhc/response.rb
lhc-6.1.1 lib/lhc/response.rb
lhc-6.1.0 lib/lhc/response.rb