Sha256: d438a25b90d09fb07144c833570bf36031608aa3c70c5a74536c64c24af1ea48

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

require 'uri'
require 'delegate'
require 'pact_broker/client/hash_refinements'

module PactBroker
  module Client
    module Hal
      class Links
        using PactBroker::Client::HashRefinements

        def initialize(href, key, links)
          @href = href
          @key = key
          @links = links
        end

        def names
          @names ||= links.collect(&:name).compact.uniq
        end

        def find!(name, not_found_message = nil)
          link = find(name)
          if link
            link
          else
            message = not_found_message || "Could not find relation '#{key}' with name '#{name}' in resource at #{href}."
            available_options = names.any? ? names.join(", ") : "<none found>"
            raise RelationNotFoundError.new(message.chomp(".") + ". Available options: #{available_options}")
          end
        end

        def find(name)
          links.find{ | link | link.name == name }
        end

        def select!(name, not_found_message = nil)
          selected_links = select(name)
          if selected_links.any?
            selected_links
          else
            message = not_found_message || "Could not find relation '#{key}' with name '#{name}' in resource at #{href}."
            available_options = names.any? ? names.join(", ") : "<none found>"
            raise RelationNotFoundError.new(message.chomp(".") + ". Available options: #{available_options}")
          end
        end

        def select(name)
          links.select{ | link | link.name == name }
        end

        private

        attr_reader :links, :key, :href
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
pact_broker-client-1.63.0 lib/pact_broker/client/hal/links.rb
pact_broker-client-1.62.1 lib/pact_broker/client/hal/links.rb
pact_broker-client-1.62.0 lib/pact_broker/client/hal/links.rb
pact_broker-client-1.61.1 lib/pact_broker/client/hal/links.rb
pact_broker-client-1.61.0 lib/pact_broker/client/hal/links.rb