Class: PactBroker::Client::Hal::Links

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/client/hal/links.rb

Instance Method Summary collapse

Constructor Details

#initialize(href, key, links) ⇒ Links

Returns a new instance of Links.



11
12
13
14
15
# File 'lib/pact_broker/client/hal/links.rb', line 11

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

Instance Method Details

#find(name) ⇒ Object



32
33
34
# File 'lib/pact_broker/client/hal/links.rb', line 32

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

#find!(name, not_found_message = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/pact_broker/client/hal/links.rb', line 21

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

#namesObject



17
18
19
# File 'lib/pact_broker/client/hal/links.rb', line 17

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

#select(name) ⇒ Object



47
48
49
# File 'lib/pact_broker/client/hal/links.rb', line 47

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

#select!(name, not_found_message = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/pact_broker/client/hal/links.rb', line 36

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