lib/excon/hypermedia/response.rb in excon-hypermedia-0.4.3 vs lib/excon/hypermedia/response.rb in excon-hypermedia-0.5.0

- old
+ new

@@ -16,16 +16,19 @@ # handle # # Correctly handle the hypermedia request. # - def handle(method_name, *params) + def handle(method_name, *params) # rubocop:disable Metrics/CyclomaticComplexity return false unless enabled? case method_name - when :resource then resource - when :rel then rel(params.shift, params) + 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 @@ -47,13 +50,29 @@ def enabled? response.data[:hypermedia] == true end def rel(name, params) - link = resource._links.send(name) - options = params.first.to_h.merge(hypermedia: true) + 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 + + def rel_params(params) + params.merge( + hypermedia: true, + hcp: (params[:hcp].nil? ? response.data[:hcp] : params[:hcp]), + hcp_params: { + content_type: response.headers['Content-Type'], + embedded: resource._embedded + } + ) end end end end