Class: Blnk::Resourceable
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Blnk::Resourceable
show all
- Extended by:
- Client, Forwardable
- Defined in:
- lib/blnk/resourceable.rb
Overview
Resoureable module that bring some tweaks for basic REST api integration
Defined Under Namespace
Classes: DefaultSearchContract, SearchResult
Class Attribute Summary collapse
Class Method Summary
collapse
-
.all ⇒ Object
-
.all_handler(parsed, status) ⇒ Object
-
.check_vars ⇒ Object
-
.create(**args) ⇒ Object
-
.create_contract_new ⇒ Object
-
.find(id) ⇒ Object
-
.find_request(id) ⇒ Object
-
.handler(parsed, status) ⇒ Object
-
.inj_handler(result:, error:, success:) ⇒ Object
-
.resource_path(id) ⇒ Object
-
.resources_path ⇒ Object
-
.search(**args) ⇒ Object
-
.search_contract_new ⇒ Object
-
.search_handler(parsed, status) ⇒ Object
-
.search_path ⇒ Object
-
.using_resp(resp:, kself: nil, &block) ⇒ Object
-
.with_handler(resp:, kself: nil, block: method(:handler)) ⇒ Object
-
.wrap_call(contract, args) ⇒ Object
Instance Method Summary
collapse
Methods included from Client
base_uri, client, get_request, post_request, put_request
Class Attribute Details
.create_contract ⇒ Object
Returns the value of attribute create_contract.
22
23
24
|
# File 'lib/blnk/resourceable.rb', line 22
def create_contract
@create_contract
end
|
.id_field ⇒ Object
Returns the value of attribute id_field.
22
23
24
|
# File 'lib/blnk/resourceable.rb', line 22
def id_field
@id_field
end
|
.resource_name ⇒ Object
Returns the value of attribute resource_name.
22
23
24
|
# File 'lib/blnk/resourceable.rb', line 22
def resource_name
@resource_name
end
|
.search_contract ⇒ Object
Returns the value of attribute search_contract.
22
23
24
|
# File 'lib/blnk/resourceable.rb', line 22
def search_contract
@search_contract
end
|
Class Method Details
.all ⇒ Object
31
32
33
34
35
|
# File 'lib/blnk/resourceable.rb', line 31
def all
check_vars
resp = get_request(path: resources_path)
with_handler(resp:, block: method(:all_handler))
end
|
.all_handler(parsed, status) ⇒ Object
81
82
83
84
85
|
# File 'lib/blnk/resourceable.rb', line 81
def all_handler(parsed, status)
success = status.success?
result = parsed.map { |r| new(r) } if success
inj_handler(success:, result:, error: parsed)
end
|
.check_vars ⇒ Object
50
51
52
53
54
|
# File 'lib/blnk/resourceable.rb', line 50
def check_vars
raise NotImplementedError, 'missing self.resource_name' unless resource_name
raise NotImplementedError, 'missing self.id_field' unless id_field
raise NotImplementedError, 'missing self.create_contract' unless create_contract
end
|
.create(**args) ⇒ Object
37
38
39
40
41
|
# File 'lib/blnk/resourceable.rb', line 37
def create(**args)
wrap_call(create_contract_new, args) do |contract|
with_handler(resp: post_request(path: resources_path, body: contract.to_h))
end
end
|
.create_contract_new ⇒ Object
66
67
68
69
70
|
# File 'lib/blnk/resourceable.rb', line 66
def create_contract_new
return create_contract.new if create_contract
raise NotImplementedError, 'missing self.create_contract'
end
|
.find(id) ⇒ Object
28
|
# File 'lib/blnk/resourceable.rb', line 28
def find(id) = with_handler resp: find_request(id)
|
.find_request(id) ⇒ Object
29
|
# File 'lib/blnk/resourceable.rb', line 29
def find_request(id) = get_request(path: resource_path(id))
|
.handler(parsed, status) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/blnk/resourceable.rb', line 87
def handler(parsed, status)
inj_handler(
result: new(parsed),
error: parsed,
success: status.success?
)
end
|
.inj_handler(result:, error:, success:) ⇒ Object
95
96
97
|
# File 'lib/blnk/resourceable.rb', line 95
def inj_handler(result:, error:, success:)
(success ? Success(result) : Failure(error))
end
|
.resource_path(id) ⇒ Object
25
|
# File 'lib/blnk/resourceable.rb', line 25
def resource_path(id) = "/#{resource_name}/#{id}"
|
.resources_path ⇒ Object
24
|
# File 'lib/blnk/resourceable.rb', line 24
def resources_path = "/#{resource_name}"
|
.search(**args) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/blnk/resourceable.rb', line 43
def search(**args)
wrap_call(search_contract_new, args) do |contract|
with_handler(resp: post_request(path: search_path, body: contract.to_h),
block: method(:search_handler))
end
end
|
.search_contract_new ⇒ Object
72
|
# File 'lib/blnk/resourceable.rb', line 72
def search_contract_new = (search_contract || DefaultSearchContract).new
|
.search_handler(parsed, status) ⇒ Object
74
75
76
77
78
79
|
# File 'lib/blnk/resourceable.rb', line 74
def search_handler(parsed, status)
success = status.success?
result = SearchResult.new(parsed) if success
inj_handler(result:, success:, error: parsed)
end
|
.search_path ⇒ Object
26
|
# File 'lib/blnk/resourceable.rb', line 26
def search_path = "/search/#{resource_name}"
|
.using_resp(resp:, kself: nil, &block) ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/blnk/resourceable.rb', line 103
def using_resp(resp:, kself: nil, &block)
check_vars
parsed = resp.parse
parsed = parsed.transform_keys(&:to_sym) unless parsed.is_a?(Array)
kself&.reload
block.call(parsed, resp.status)
end
|
.with_handler(resp:, kself: nil, block: method(:handler)) ⇒ Object
99
100
101
|
# File 'lib/blnk/resourceable.rb', line 99
def with_handler(resp:, kself: nil, block: method(:handler))
using_resp(resp:, kself:, &block)
end
|
.wrap_call(contract, args) ⇒ Object
56
57
58
59
60
61
62
63
64
|
# File 'lib/blnk/resourceable.rb', line 56
def wrap_call(contract, args)
check_vars
ccall = contract.call(args)
return Failure(ccall.errors.to_h) if ccall.failure?
return yield ccall if block_given?
ccall
end
|
Instance Method Details
#_id ⇒ Object
127
|
# File 'lib/blnk/resourceable.rb', line 127
def _id = public_send(self.class.id_field)
|
#persisted? ⇒ Boolean
126
|
# File 'lib/blnk/resourceable.rb', line 126
def persisted? = !_id.nil?
|
#reload ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/blnk/resourceable.rb', line 114
def reload
self.class.find_request(_id).tap do |res|
next unless res.status.success?
res.parse.each_pair do |k, v|
self[k] = v
end
end
self
end
|