Sha256: 635b89fba33cac5b93aa6bf0c8b3559fdf469427f56ce0724e198225c1f242c4

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

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 seconds
  def time
    raw.time || 0
  end

  # Provides response time in milliseconds
  def time_ms
    time * 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

6 entries across 6 versions & 1 rubygems

Version Path
lhc-16.0.0.pre.pro2162.2 lib/lhc/response.rb
lhc-16.0.0.pre.pro2162 lib/lhc/response.rb
lhc-15.0.1 lib/lhc/response.rb
lhc-15.0.0 lib/lhc/response.rb
lhc-14.0.0 lib/lhc/response.rb
lhc-13.4.0.pre.pro1766.1 lib/lhc/response.rb