Sha256: 9130d43720edd21fcb835b3f65bb3394527349a428ee98baea74a176c7526242

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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
      attr_reader :response

      def initialize(response)
        @response = response
      end

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

        case resource.type?(method_name)
        when :link      then return handle_link(method_name, params)
        when :attribute then return handle_attribute(method_name)
        end

        respond_to?(method_name) ? send(method_name) : false
      end

      def links
        resource.links
      end

      def attributes
        resource.attributes
      end

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

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

      def disabled?
        !enabled?
      end

      private

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

      def handle_attribute(name)
        attributes[name.to_s]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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