Class: Pact::Hal::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/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.



14
15
16
17
18
19
20
# File 'lib/pact/hal/entity.rb', line 14

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



72
73
74
75
76
77
78
79
80
# File 'lib/pact/hal/entity.rb', line 72

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
    super
  end
end

Instance Method Details



46
47
48
49
50
51
52
53
54
# File 'lib/pact/hal/entity.rb', line 46

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



56
57
58
# File 'lib/pact/hal/entity.rb', line 56

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

#assert_success!Object



86
87
88
# File 'lib/pact/hal/entity.rb', line 86

def assert_success!
  self
end

#can?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pact/hal/entity.rb', line 38

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

#fetch(key, fallback_key = nil) ⇒ Object



68
69
70
# File 'lib/pact/hal/entity.rb', line 68

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

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



42
43
44
# File 'lib/pact/hal/entity.rb', line 42

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

#get(key, *args) ⇒ Object



22
23
24
# File 'lib/pact/hal/entity.rb', line 22

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

#get!(key, *args) ⇒ Object



26
27
28
# File 'lib/pact/hal/entity.rb', line 26

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

#post(key, *args) ⇒ Object



30
31
32
# File 'lib/pact/hal/entity.rb', line 30

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

#put(key, *args) ⇒ Object



34
35
36
# File 'lib/pact/hal/entity.rb', line 34

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

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

Returns:

  • (Boolean)


82
83
84
# File 'lib/pact/hal/entity.rb', line 82

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

#responseObject



64
65
66
# File 'lib/pact/hal/entity.rb', line 64

def response
  @response
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pact/hal/entity.rb', line 60

def success?
  true
end