Class: FractalApi::BaseModel
- Inherits:
-
Object
- Object
- FractalApi::BaseModel
show all
- Defined in:
- lib/fractal_api/base_model.rb
Direct Known Subclasses
Balance, Bank, BankAccount, BankAccountScheme, Category, Company, Consent, Forecasting::Balance, Forecasting::Forecast, Forecasting::Transaction, Merchant, Transaction, WebhookNotification
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
75
76
77
78
79
|
# File 'lib/fractal_api/base_model.rb', line 75
def initialize(attributes = {})
attributes.each do |attr, value|
public_send("#{attr}=", value)
end
end
|
Class Method Details
.attribute_aliases(aliases = nil) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/fractal_api/base_model.rb', line 43
def attribute_aliases(aliases = nil)
@attribute_aliases ||= {}
return @attribute_aliases unless aliases
@attribute_aliases.merge!(aliases)
end
|
.attributes(*attributes) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/fractal_api/base_model.rb', line 33
def attributes(*attributes)
@attributes ||= []
return @attributes unless attributes
attr_accessor(*attributes)
@attributes += attributes
end
|
.build(json: {}, key_transformer: self.key_transformer) ⇒ Object
Sets all the instance variables by reading the JSON from FractalApi and converting the keys from PascalCase to snake_case, as it’s the standard in Ruby.
65
66
67
68
69
70
71
72
|
# File 'lib/fractal_api/base_model.rb', line 65
def build(json: {}, key_transformer: self.key_transformer)
new.tap do |record|
attributes.each do |attr|
key = attribute_aliases.key?(attr) ? attribute_aliases[attr] : attr
record.public_send("#{attr}=", json[key_transformer.transform(key)])
end
end
end
|
51
52
53
54
55
56
57
|
# File 'lib/fractal_api/base_model.rb', line 51
def format_url(url, params)
formatted = url.dup.strip
params.each { |key, value| formatted.sub!(":#{key}", value) }
formatted
end
|
.get(path, params: {}, headers: {}) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/fractal_api/base_model.rb', line 9
def get(path, params: {}, headers: {})
result = FractalApi.client.get(path.strip, params: params, headers: )
raise APIKeyError, result.body[:error] if result.status == 401
result
end
|
59
60
61
|
# File 'lib/fractal_api/base_model.rb', line 59
def key_transformer
CamelizerLower
end
|
.post(path, params: {}, headers: {}) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/fractal_api/base_model.rb', line 17
def post(path, params: {}, headers: {})
result = FractalApi.client.post(path.strip, params: params, headers: )
raise APIKeyError, result.body[:error] if result.status == 401
result
end
|
.put(path, params: {}, headers: {}) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/fractal_api/base_model.rb', line 25
def put(path, params: {}, headers: {})
result = FractalApi.client.put(path.strip, params: params, headers: )
raise APIKeyError, result.body[:error] if result.status == 401
result
end
|
Instance Method Details
#as_json ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/fractal_api/base_model.rb', line 97
def as_json
self.class.attributes.each_with_object({}) do |attr, hash|
value = public_send(attr)
value = value.as_json if value.respond_to?(:as_json)
if value.is_a?(Array)
value = value.map { |v| v.respond_to?(:as_json) ? v.as_json : v }
end
value = value.iso8601 if value.respond_to?(:iso8601)
key = self.class.key_transformer.transform(attr)
hash[key] = value
end
end
|
93
94
95
|
# File 'lib/fractal_api/base_model.rb', line 93
def format_url(url, params)
self.class.format_url(url, params)
end
|
#get(path, params: {}, headers: {}) ⇒ Object
81
82
83
|
# File 'lib/fractal_api/base_model.rb', line 81
def get(path, params: {}, headers: {})
self.class.get(path, params: params, headers: )
end
|
#post(path, params: {}, headers: {}) ⇒ Object
85
86
87
|
# File 'lib/fractal_api/base_model.rb', line 85
def post(path, params: {}, headers: {})
self.class.post(path, params: params, headers: )
end
|
#put(path, params: {}, headers: {}) ⇒ Object
89
90
91
|
# File 'lib/fractal_api/base_model.rb', line 89
def put(path, params: {}, headers: {})
self.class.put(path, params: params, headers: )
end
|