Class: PactBroker::Client::Hal::Entity
- Inherits:
-
Object
- Object
- PactBroker::Client::Hal::Entity
show all
- Defined in:
- lib/pact_broker/client/hal/entity.rb
Instance Method Summary
collapse
-
#_link(key, fallback_key = nil) ⇒ Object
-
#_link!(key) ⇒ Object
-
#_links(key) ⇒ Object
-
#_links!(key) ⇒ Object
-
#assert_success!(_ignored = nil) ⇒ Object
-
#can?(key) ⇒ Boolean
-
#does_not_exist? ⇒ Boolean
-
#embedded_entities(key = nil) ⇒ Object
-
#embedded_entities!(key) ⇒ Object
-
#embedded_entity ⇒ Object
-
#fetch(key, fallback_key = nil) ⇒ Object
-
#follow(key, http_method, *args) ⇒ Object
-
#get(key, *args) ⇒ Object
-
#get!(key, *args) ⇒ Object
-
#initialize(href, data, http_client, response = nil) ⇒ Entity
constructor
A new instance of Entity.
-
#method_missing(method_name, *args, &block) ⇒ Object
-
#patch(key, *args) ⇒ Object
-
#patch!(key, *args) ⇒ Object
-
#post(key, *args) ⇒ Object
-
#post!(key, *args) ⇒ Object
-
#put(key, *args) ⇒ Object
-
#put!(key, *args) ⇒ Object
-
#relation_not_found_error_message(key, href) ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#response ⇒ Object
-
#self_href(entity_hash) ⇒ Object
-
#success? ⇒ Boolean
Constructor Details
#initialize(href, data, http_client, response = nil) ⇒ Entity
Returns a new instance of Entity.
22
23
24
25
26
27
28
|
# File 'lib/pact_broker/client/hal/entity.rb', line 22
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
143
144
145
146
147
148
149
150
151
|
# File 'lib/pact_broker/client/hal/entity.rb', line 143
def method_missing(method_name, *args, &block)
if @data.respond_to?(:key?) && @data.key?(method_name.to_s)
@data[method_name.to_s]
elsif @links.respond_to?(:key?) && @links.key?(method_name)
Link.new(@links[method_name], @client).run(*args)
else
nil
end
end
|
Instance Method Details
#_link(key, fallback_key = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/pact_broker/client/hal/entity.rb', line 70
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
93
94
95
|
# File 'lib/pact_broker/client/hal/entity.rb', line 93
def _link!(key)
_link(key) or raise RelationNotFoundError.new(relation_not_found_error_message(key, @href))
end
|
#_links(key) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/pact_broker/client/hal/entity.rb', line 80
def _links(key)
if @links[key] && @links[key].is_a?(Array)
link_collection = @links[key].collect do | hash |
Link.new(hash, @client)
end
Links.new(@href, key, link_collection)
elsif @links[key].is_a?(Hash)
Links.new(@href, key, [Link.new(@links[key], @client)])
else
nil
end
end
|
#_links!(key) ⇒ Object
97
98
99
|
# File 'lib/pact_broker/client/hal/entity.rb', line 97
def _links!(key)
_links(key) or raise RelationNotFoundError.new(relation_not_found_error_message(key, @href))
end
|
#assert_success!(_ignored = nil) ⇒ Object
157
158
159
|
# File 'lib/pact_broker/client/hal/entity.rb', line 157
def assert_success!(_ignored = nil)
self
end
|
#can?(key) ⇒ Boolean
62
63
64
|
# File 'lib/pact_broker/client/hal/entity.rb', line 62
def can?(key)
@links.key? key.to_s
end
|
#does_not_exist? ⇒ Boolean
131
132
133
|
# File 'lib/pact_broker/client/hal/entity.rb', line 131
def does_not_exist?
false
end
|
#embedded_entities(key = nil) ⇒ Object
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/pact_broker/client/hal/entity.rb', line 116
def embedded_entities(key = nil)
embedded_ents = if key
@data["_embedded"][key]
else
yield @data["_embedded"]
end
embedded_ents.collect do | embedded_ent |
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end
|
#embedded_entities!(key) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/pact_broker/client/hal/entity.rb', line 108
def embedded_entities!(key)
embedded_ents = (@data["_embedded"] && @data["_embedded"][key])
raise EmbeddedEntityNotFoundError.new("Could not find embedded entity with key '#{key}' in resource at #{@href}") unless embedded_ents
embedded_ents.collect do | embedded_ent |
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end
|
#embedded_entity ⇒ Object
101
102
103
104
105
106
|
# File 'lib/pact_broker/client/hal/entity.rb', line 101
def embedded_entity
embedded_ent = yield @data["_embedded"]
if embedded_ent
Entity.new(self_href(embedded_ent), embedded_ent, @client, response)
end
end
|
#fetch(key, fallback_key = nil) ⇒ Object
139
140
141
|
# File 'lib/pact_broker/client/hal/entity.rb', line 139
def fetch(key, fallback_key = nil)
@links[key] || (fallback_key && @links[fallback_key])
end
|
#follow(key, http_method, *args) ⇒ Object
66
67
68
|
# File 'lib/pact_broker/client/hal/entity.rb', line 66
def follow(key, http_method, *args)
Link.new(@links[key].merge(method: http_method), @client).run(*args)
end
|
#get(key, *args) ⇒ Object
30
31
32
|
# File 'lib/pact_broker/client/hal/entity.rb', line 30
def get(key, *args)
_link(key).get(*args)
end
|
#get!(key, *args) ⇒ Object
34
35
36
|
# File 'lib/pact_broker/client/hal/entity.rb', line 34
def get!(key, *args)
get(key, *args).assert_success!
end
|
#patch(key, *args) ⇒ Object
54
55
56
|
# File 'lib/pact_broker/client/hal/entity.rb', line 54
def patch(key, *args)
_link(key).patch(*args)
end
|
#patch!(key, *args) ⇒ Object
58
59
60
|
# File 'lib/pact_broker/client/hal/entity.rb', line 58
def patch!(key, *args)
patch(key, *args).assert_success!
end
|
#post(key, *args) ⇒ Object
38
39
40
|
# File 'lib/pact_broker/client/hal/entity.rb', line 38
def post(key, *args)
_link(key).post(*args)
end
|
#post!(key, *args) ⇒ Object
42
43
44
|
# File 'lib/pact_broker/client/hal/entity.rb', line 42
def post!(key, *args)
post(key, *args).assert_success!
end
|
#put(key, *args) ⇒ Object
46
47
48
|
# File 'lib/pact_broker/client/hal/entity.rb', line 46
def put(key, *args)
_link(key).put(*args)
end
|
#put!(key, *args) ⇒ Object
50
51
52
|
# File 'lib/pact_broker/client/hal/entity.rb', line 50
def put!(key, *args)
put(key, *args).assert_success!
end
|
#relation_not_found_error_message(key, href) ⇒ Object
165
166
167
|
# File 'lib/pact_broker/client/hal/entity.rb', line 165
def relation_not_found_error_message(key, href)
"Could not find relation '#{key}' in resource at #{href}. The most likely reason for this is that you are on an old version of the Pact Broker and you need to upgrade, or you are using PactFlow and you don't have the permissions required for this action."
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
153
154
155
|
# File 'lib/pact_broker/client/hal/entity.rb', line 153
def respond_to_missing?(method_name, include_private = false)
@data.key?(method_name) || @links.key?(method_name)
end
|
#response ⇒ Object
135
136
137
|
# File 'lib/pact_broker/client/hal/entity.rb', line 135
def response
@response
end
|
#self_href(entity_hash) ⇒ Object
161
162
163
|
# File 'lib/pact_broker/client/hal/entity.rb', line 161
def self_href(entity_hash)
entity_hash["_links"] && entity_hash["_links"]["self"] && entity_hash["_links"]["self"]["href"]
end
|
#success? ⇒ Boolean
127
128
129
|
# File 'lib/pact_broker/client/hal/entity.rb', line 127
def success?
true
end
|