Sha256: 3fdede25af36f22d5ba54e491f0469fa02820960bada5fc9d6a298b8b6484614

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'excon/hypermedia/ext/response'

module Excon
  module HyperMedia
    # Response
    #
    # This HyperMedia::Response object helps determine valid subsequent
    # requests and attribute values.
    #
    class Response
      def initialize(response)
        @response = response
      end

      # handle
      #
      # Correctly handle the hypermedia request.
      #
      def handle(method_name, *params)
        return false unless enabled?

        if method_name == :rel
          handle_link(params.shift, params)
        elsif resource.type?(method_name) == :link
          handle_link(method_name, params)
        elsif resource.respond_to?(method_name, false)
          resource.send(method_name, *params)
        else
          false
        end
      end

      private

      attr_reader :response

      def resource
        @resource ||= Resource.new(response.body)
      end

      def enabled?
        response.data[:hypermedia] == true
      end

      def handle_link(name, params)
        Excon.new(resource.link(name).href, params.first.to_h.merge(hypermedia: true))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
excon-hypermedia-0.3.0 lib/excon/hypermedia/response.rb