Sha256: e53a8782107b1ec6598953eaf06712abc27345c515386f5014b3790053ec5fec

Contents?: true

Size: 1.18 KB

Versions: 9

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 DHC::Response
  autoload :Data, 'dhc/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? ? DHC::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 DHC::Formats::JSON.new if request.nil?
    request.format
  end

  private

  attr_accessor :raw

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dhc-2.4.0 lib/dhc/response.rb
dhc-2.3.0 lib/dhc/response.rb
dhc-2.2.1 lib/dhc/response.rb
dhc-2.2.0 lib/dhc/response.rb
dhc-2.1.1 lib/dhc/response.rb
dhc-2.1.0 lib/dhc/response.rb
dhc-2.0.1 lib/dhc/response.rb
dhc-2.0.0 lib/dhc/response.rb
dhc-1.0.0 lib/dhc/response.rb