Class: Economic::BaseRepo
- Inherits:
-
Object
- Object
- Economic::BaseRepo
show all
- Defined in:
- lib/economic/base_repo.rb
Direct Known Subclasses
AccountRepo, AccountingYearRepo, CustomerGroupRepo, CustomerRepo, DepartmentRepo, DepartmentalDistributionRepo, Invoices::Repo, JournalRepo, JournalVoucherRepo, LayoutRepo, NestedBaseRepo, Orders::Repo, PaymentTermsRepo, PaymentTypeRepo, ProductGroupRepo, ProductRepo, SelfRepo, SupplierGroupRepo, SupplierRepo, UnitRepo, VatTypeRepo, VatZoneRepo
Constant Summary
collapse
- URL =
"https://restapi.e-conomic.com/".freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.endpoint ⇒ Object
Returns the value of attribute endpoint.
10
11
12
|
# File 'lib/economic/base_repo.rb', line 10
def endpoint
@endpoint
end
|
Class Method Details
.all(filter_text: "", url: endpoint_url) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/economic/base_repo.rb', line 25
def all(filter_text: "", url: endpoint_url)
= {}
pageindex = 0
entries = []
while ["nextPage"] || pageindex.zero?
response = fetch(url: url, pageindex: pageindex, filter_text: filter_text)
hash = JSON.parse(response.body)
hash["collection"].each do |entry_hash|
entries.push model.new(entry_hash)
end
= hash["pagination"]
pageindex += 1
end
entries
end
|
.destroy(id, url: endpoint_url) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/economic/base_repo.rb', line 65
def destroy(id, url: endpoint_url)
url += "/" + id_to_url_formatted_id(id)
response = send_request(method: :delete, url: url)
success_codes = [200, 204]
true if success_codes.include?(response.code)
end
|
.endpoint_url ⇒ Object
61
62
63
|
# File 'lib/economic/base_repo.rb', line 61
def endpoint_url
URL + endpoint_name
end
|
.filter(filter_text, url: endpoint_url) ⇒ Object
45
46
47
|
# File 'lib/economic/base_repo.rb', line 45
def filter(filter_text, url: endpoint_url)
all(filter_text: filter_text, url: url)
end
|
.find(id, url: endpoint_url) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/economic/base_repo.rb', line 53
def find(id, url: endpoint_url)
url += "/" + id_to_url_formatted_id(id)
response = send_request(method: :get, url: url)
entry_hash = JSON.parse(response.body)
model.new(entry_hash)
end
|
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/economic/base_repo.rb', line 84
def id_to_url_formatted_id(id)
id.to_s.gsub("_", "_8_")
.gsub("<", "_0_")
.gsub(">", "_1_")
.gsub("*", "_2_")
.gsub("%", "_3_")
.gsub(":", "_4_")
.gsub("&", "_5_")
.gsub("/", "_6_")
.gsub("\\", "_7_")
.gsub(" ", "_9_")
.gsub("?", "_10_")
.gsub(".", "_11_")
.gsub("#", "_12_")
.gsub("+", "_13_")
end
|
.save(model, url: endpoint_url) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/economic/base_repo.rb', line 12
def save(model, url: endpoint_url)
if model.id_key.nil?
post_or_put = :post
else
post_or_put = :put
url += "/" + id_to_url_formatted_id(model.id_key)
end
response = send_request(method: post_or_put, url: url, payload: model.to_h.to_json)
modelize_response(response)
end
|
.send_request(method:, url:, payload: "") ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/economic/base_repo.rb', line 73
def send_request(method:, url:, payload: "", &)
if payload.strip.empty?
RestClient::Request.execute(method: method, url: url, headers: , &)
else
RestClient::Request.execute(method: method, url: url, payload: payload, headers: , &)
end
rescue => e
warn "#{e} #{e.response}" if e.respond_to?(:response)
raise e
end
|
.updated_after(date) ⇒ Object
49
50
51
|
# File 'lib/economic/base_repo.rb', line 49
def updated_after(date)
filter("lastUpdated$gt:#{to_iso8601z(date)}")
end
|