lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb in excon-hypermedia-0.5.0 vs lib/excon/hypermedia/middlewares/hypertext_cache_pattern.rb in excon-hypermedia-0.5.1

- old
+ new

@@ -13,14 +13,14 @@ attr_reader :datum def request_call(datum) @datum = datum - return super unless datum[:hcp] == true && datum[:method] == :get && find_embedded + return super unless datum[:hcp] == true && datum[:method] == :get && resource datum[:response] = { - body: @embedded.to_json, + body: resource.to_json, hcp: true, headers: content_type_header, remote_ip: '127.0.0.1', status: 200 } @@ -28,25 +28,19 @@ super end private - def find_embedded - datum.dig(:hcp_params, :embedded).to_h.each do |_, object| - break if (@embedded = object_to_embedded(object)) - end + def resource + @resource ||= embedded.find { |name, _| name == relation_name }.to_a[1] + end - @embedded + def relation_name + datum.dig(:hcp_params, :relation) end - def object_to_embedded(object) - uri = ::Addressable::URI.new(datum.tap { |h| h.delete(:port) }) - - if object.respond_to?(:to_ary) - object.find { |hash| hash.dig('_links', 'self', 'href') == uri.to_s } - elsif object.dig('_links', 'self', 'href') == uri.to_s - object - end + def embedded + datum.dig(:hcp_params, :embedded) end def content_type_header return {} unless (header = datum.dig(:hcp_params, :content_type))