Class: PactBroker::Client::Hal::Entity

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

Direct Known Subclasses

ErrorEntity

Instance Method Summary collapse

Constructor Details

#initialize(href, data, http_client, response = nil) ⇒ Entity

Returns a new instance of Entity.



12
13
14
15
16
17
18
# File 'lib/pact_broker/client/hal/entity.rb', line 12

def initialize(href, data, http_client, response = nil)
  @href = href
  @data = data
  @links = (@data || {}).fetch("_links", {})
  @client = http_client
  @response = response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/pact_broker/client/hal/entity.rb', line 74

def method_missing(method_name, *args, &block)
  if @data.key?(method_name.to_s)
    @data[method_name.to_s]
  elsif @links.key?(method_name)
    Link.new(@links[method_name], @client).run(*args)
  else
    nil
  end
end

Instance Method Details



44
45
46
47
48
49
50
51
52
# File 'lib/pact_broker/client/hal/entity.rb', line 44

def _link(key, fallback_key = nil)
  if @links[key]
    Link.new(@links[key], @client)
  elsif fallback_key && @links[fallback_key]
    Link.new(@links[fallback_key], @client)
  else
    nil
  end
end

#_link!(key) ⇒ Object



54
55
56
# File 'lib/pact_broker/client/hal/entity.rb', line 54

def _link!(key)
  _link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
end

#assert_success!Object



88
89
90
# File 'lib/pact_broker/client/hal/entity.rb', line 88

def assert_success!
  self
end

#can?(key) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pact_broker/client/hal/entity.rb', line 36

def can?(key)
  @links.key? key.to_s
end

#does_not_exist?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/pact_broker/client/hal/entity.rb', line 62

def does_not_exist?
  false
end

#fetch(key, fallback_key = nil) ⇒ Object



70
71
72
# File 'lib/pact_broker/client/hal/entity.rb', line 70

def fetch(key, fallback_key = nil)
  @links[key] || (fallback_key && @links[fallback_key])
end

#follow(key, http_method, *args) ⇒ Object



40
41
42
# File 'lib/pact_broker/client/hal/entity.rb', line 40

def follow(key, http_method, *args)
  Link.new(@links[key].merge(method: http_method), @client).run(*args)
end

#get(key, *args) ⇒ Object



20
21
22
# File 'lib/pact_broker/client/hal/entity.rb', line 20

def get(key, *args)
  _link(key).get(*args)
end

#patch(key, *args) ⇒ Object



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

def patch(key, *args)
  _link(key).patch(*args)
end

#post(key, *args) ⇒ Object



24
25
26
# File 'lib/pact_broker/client/hal/entity.rb', line 24

def post(key, *args)
  _link(key).post(*args)
end

#put(key, *args) ⇒ Object



28
29
30
# File 'lib/pact_broker/client/hal/entity.rb', line 28

def put(key, *args)
  _link(key).put(*args)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/pact_broker/client/hal/entity.rb', line 84

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name) || @links.key?(method_name)
end

#responseObject



66
67
68
# File 'lib/pact_broker/client/hal/entity.rb', line 66

def response
  @response
end

#success?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pact_broker/client/hal/entity.rb', line 58

def success?
  true
end