Skip to content

Commit bcbe1cb

Browse files
committed
WIP - start adding refund, commit and void to transactions
1 parent 7117dc3 commit bcbe1cb

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/blnk/resourceable.rb

+23-8
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ class << self
1818

1919
attr_accessor :resource_name, :id_field, :create_contract, :search_contract
2020

21-
def find(id)
22-
check_vars
23-
res = get_request(path: "/#{resource_name}/#{id}")
24-
return Success(new(res.parse)) if res.status.success?
25-
26-
Failure(res.parse)
27-
end
21+
def find(id) = with_req resp: get_request(path: "/#{resource_name}/#{id}")
2822

2923
def all
3024
check_vars
@@ -76,9 +70,30 @@ def create_contract_new
7670
end
7771

7872
def search_contract_new = (search_contract || DefaultSearchContract).new
73+
74+
def handler(parsed, status) = (status.success? ? Success(new(parsed)) : Failure(parsed))
75+
def with_req(resp:, self_caller: nil) = using_resp(resp:, self_caller:, &method(:handler))
76+
77+
def using_resp(resp:, self_caller: nil, &block)
78+
check_vars
79+
parsed = resp.parse.transform_keys(&:to_sym)
80+
self_caller&.reload
81+
82+
block.call(parsed, resp.status)
83+
end
84+
end
85+
86+
def reload
87+
self.class.find(_id) do |res|
88+
next unless res.status.success?
89+
90+
res.parse.each_pair do |k, v|
91+
self[k] = v
92+
end
93+
self
94+
end
7995
end
8096

81-
def reload = self.class.find(_id)
8297
# table[self.class.id_field]
8398
def persisted? = !_id.nil?
8499
def _id = public_send(self.class.id_field)

lib/blnk/transaction.rb

+12
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,23 @@ class CreateContract < Dry::Validation::Contract
1515
required(:allow_overdraft).value(:bool)
1616
optional(:inflight).value(:bool)
1717
optional(:rate).value(:integer)
18+
optional(:scheduled_for).value(:string)
1819
end
1920
end
2021

2122
self.resource_name = :transactions
2223
self.id_field = :transaction_id
2324
self.create_contract = CreateContract
25+
26+
def refund = self.class.with_req(req: request_refund, self_caller: self)
27+
def void = raise NotImplementedError
28+
def commit = raise NotImplementedError
29+
30+
private
31+
32+
def inflight_path = "/transactions/inflight/#{_id}"
33+
def refund_path = "/refund-transactions/#{_id}"
34+
def request_update_inflight(body:, path: inflight_path) = self.class.put_request(path:, body:)
35+
def request_refund(path: refund_path) = self.class.post_request(path:, body: nil)
2436
end
2537
end

0 commit comments

Comments
 (0)