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.



8
9
10
11
12
# File 'lib/pact_broker/client/hal/links.rb', line 8

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

Instance Method Details

#find(name) ⇒ Object



29
30
31
# File 'lib/pact_broker/client/hal/links.rb', line 29

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

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/pact_broker/client/hal/links.rb', line 18

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



14
15
16
# File 'lib/pact_broker/client/hal/links.rb', line 14

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

#select(name) ⇒ Object



44
45
46
# File 'lib/pact_broker/client/hal/links.rb', line 44

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

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



33
34
35
36
37
38
39
40
41
42
# File 'lib/pact_broker/client/hal/links.rb', line 33

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