class Excon::HyperMedia::Response

Response

This HyperMedia::Response object helps determine valid subsequent requests and attribute values.

Attributes

response[R]

Public Class Methods

new(response) click to toggle source
# File lib/excon/hypermedia/response.rb, line 13
def initialize(response)
  @response = response
end

Public Instance Methods

handle(method_name, *params) click to toggle source

handle

Correctly handle the hypermedia request.

# File lib/excon/hypermedia/response.rb, line 21
def handle(method_name, *params) # rubocop:disable Metrics/CyclomaticComplexity
  return false unless enabled?

  case method_name
  when :resource                 then resource
  when :_links, :links           then resource._links
  when :_embedded, :embedded     then resource._embedded
  when :_properties, :properties then resource._properties
  when :rel                      then rel(params.shift, params)
  else false
  end
end

Private Instance Methods

body_to_hash() click to toggle source
# File lib/excon/hypermedia/response.rb, line 42
def body_to_hash
  (content_type =~ %r{application/(hal\+)?json}).nil? ? {} : JSON.parse(response.body)
end
content_type() click to toggle source
# File lib/excon/hypermedia/response.rb, line 46
def content_type
  response.headers['Content-Type'].to_s
end
enabled?() click to toggle source
# File lib/excon/hypermedia/response.rb, line 50
def enabled?
  response.data[:hypermedia] == true
end
rel(name, params) click to toggle source
# File lib/excon/hypermedia/response.rb, line 54
def rel(name, params)
  raise ArgumentError, 'missing relation name' unless name

  unless (link = resource._links[name])
    raise UnknownRelationError, "unknown relation: #{name}"
  end

  options = rel_params(params.first.to_h)

  link.respond_to?(:to_ary) ? link.map { |l| l.rel(options) } : link.rel(options)
end
rel_params(params) click to toggle source
# File lib/excon/hypermedia/response.rb, line 66
def rel_params(params)
  params.merge(
    hcp: (params[:hcp].nil? ? response.data[:hcp] : params[:hcp]),
    embedded: resource._embedded.to_h,
    hypermedia: true
  )
end
resource() click to toggle source
# File lib/excon/hypermedia/response.rb, line 38
def resource
  @resource ||= ResourceObject.new(body_to_hash)
end