Sha256: 10f14b69c3534125069e2e86eec55f2926d6584815b3396e4736378d45ab7562

Contents?: true

Size: 1.15 KB

Versions: 12

Compression:

Stored size: 1.15 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 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

12 entries across 12 versions & 1 rubygems

Version Path
lhc-10.1.2 lib/lhc/response.rb
lhc-10.1.1 lib/lhc/response.rb
lhc-10.1.0 lib/lhc/response.rb
lhc-10.0.2 lib/lhc/response.rb
lhc-9.4.4 lib/lhc/response.rb
lhc-10.0.1 lib/lhc/response.rb
lhc-10.0.0 lib/lhc/response.rb
lhc-9.4.3 lib/lhc/response.rb
lhc-9.4.2 lib/lhc/response.rb
lhc-9.4.1 lib/lhc/response.rb
lhc-9.4.0 lib/lhc/response.rb
lhc-9.3.1 lib/lhc/response.rb