Class: KillBillClient::Model::Resource
- Inherits:
-
Object
- Object
- KillBillClient::Model::Resource
show all
- Defined in:
- lib/killbill_client/models/resource.rb
Direct Known Subclasses
AccountAttributes, AccountEmailAttributes, AccountTimelineAttributes, AuditLogAttributes, BillingExceptionAttributes, BundleAttributes, BundleTimelineAttributes, CatalogAttributesSimple, ChargebackAttributes, CreditAttributes, CustomFieldAttributes, DeletedEventSubscriptionAttributes, EventSubscriptionAttributes, InvoiceAttributes, InvoiceEmailAttributes, InvoiceItemAttributes, NewEventSubscriptionAttributes, NotificationAttributes, OverdueStateAttributes, PaymentAttributes, PaymentMethodAttributes, PaymentMethodPluginDetailAttributes, PaymentMethodProperties, PhaseAttributes, PlanAttributes, PlanDetailAttributes, PriceAttributes, ProductAttributes, RefundAttributes, Security, StackTraceElementAttributes, SubscriptionAttributes, TagAttributes, TagDefinitionAttributes, TenantAttributes, TenantKeyAttributes
Constant Summary
collapse
- KILLBILL_API_PREFIX =
'/1.0/kb/'
'pagination'
- @@attribute_names =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.attribute(name) ⇒ Object
-
.create_alias(new_name, old_name) ⇒ Object
hack to cater the api return attributes and javax attributes without editing gen scripts call only after its declared as a instance_method using attr_accessor.
-
.delete(uri, params = {}, options = {}, clazz = self) ⇒ Object
-
.extract_session_id(response) ⇒ Object
Extract the session id from a response.
-
.from_json(resource_class, json) ⇒ Resource
Instantiates a record from a JSON blob.
-
.from_response(resource_class, response) ⇒ Resource
Instantiates a record from an HTTP response, setting the record’s response attribute in the process.
-
.get(uri, params = {}, options = {}, clazz = self) ⇒ Object
-
.has_many(attr_name, type = nil) ⇒ Object
-
.has_one(attr_name, type = nil) ⇒ Object
-
.head(uri, params = {}, options = {}, clazz = self) ⇒ Object
-
.instantiate_record_from_json(resource_class, data) ⇒ Object
-
.post(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object
-
.put(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object
Instance Method Summary
collapse
Instance Attribute Details
#clazz ⇒ Object
Returns the value of attribute clazz.
7
8
9
|
# File 'lib/killbill_client/models/resource.rb', line 7
def clazz
@clazz
end
|
#etag ⇒ Object
Returns the value of attribute etag.
7
8
9
|
# File 'lib/killbill_client/models/resource.rb', line 7
def etag
@etag
end
|
#response ⇒ Object
Returns the value of attribute response.
7
8
9
|
# File 'lib/killbill_client/models/resource.rb', line 7
def response
@response
end
|
#session_id ⇒ Object
Returns the value of attribute session_id.
7
8
9
|
# File 'lib/killbill_client/models/resource.rb', line 7
def session_id
@session_id
end
|
#uri ⇒ Object
184
185
186
|
# File 'lib/killbill_client/models/resource.rb', line 184
def uri
@uri
end
|
Class Method Details
.attribute(name) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/killbill_client/models/resource.rb', line 128
def attribute(name)
self.send('attr_accessor', name.to_sym)
self.instance_variable_set('@json_attributes', []) unless self.instance_variable_get('@json_attributes')
self.instance_variable_get('@json_attributes') << name.to_s
attributes = self.instance_variable_get('@json_attributes')
(
class << self;
self
end).send(:define_method, :json_attributes) do
attributes
end
end
|
.create_alias(new_name, old_name) ⇒ Object
hack to cater the api return attributes and javax attributes without editing gen scripts call only after its declared as a instance_method using attr_accessor
162
163
164
165
|
# File 'lib/killbill_client/models/resource.rb', line 162
def create_alias(new_name, old_name)
alias_method new_name.to_sym, old_name.to_sym alias_method "#{new_name}=".to_sym, "#{old_name}=".to_sym end
|
.delete(uri, params = {}, options = {}, clazz = self) ⇒ Object
38
39
40
41
|
# File 'lib/killbill_client/models/resource.rb', line 38
def delete(uri, params = {}, options = {}, clazz = self)
response = KillBillClient::API.delete uri, params, options
from_response clazz, response
end
|
Extract the session id from a response
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/killbill_client/models/resource.rb', line 168
def (response)
session_cookie = response['set-cookie']
unless session_cookie.nil?
session_cookie.split(';').each do |chunk|
chunk.strip!
key, value = chunk.split('=')
return value if key == 'JSESSIONID'
end
end
nil
end
|
.from_json(resource_class, json) ⇒ Resource
Instantiates a record from a JSON blob.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/killbill_client/models/resource.rb', line 77
def from_json(resource_class, json)
return nil if json.nil? or json.size == 0
data = JSON.parse json
if data.is_a? Array
records = Resources.new
data.each do |data_element|
if data_element.is_a? Enumerable
records << instantiate_record_from_json(resource_class, data_element)
else
records << data_element
end
end
records
else
instantiate_record_from_json(resource_class, data)
end
end
|
.from_response(resource_class, response) ⇒ Resource
Instantiates a record from an HTTP response, setting the record’s response attribute in the process.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/killbill_client/models/resource.rb', line 49
def from_response(resource_class, response)
case response['Content-Type']
when %r{application/pdf}
response.body
when %r{application/json}
record = from_json resource_class, response.body
session_id = (response)
record.instance_eval {
@clazz = resource_class
@etag = response['ETag']
@session_id = session_id
@pagination_max_nb_records = response['X-Killbill-Pagination-MaxNbRecords'].to_i unless response['X-Killbill-Pagination-MaxNbRecords'].nil?
@pagination_total_nb_records = response['X-Killbill-Pagination-TotalNbRecords'].to_i unless response['X-Killbill-Pagination-TotalNbRecords'].nil?
@pagination_next_page = response['X-Killbill-Pagination-NextPageUri']
@response = response
}
record
else
raise ArgumentError, "#{response['Content-Type']} is not supported by the library"
end
end
|
.get(uri, params = {}, options = {}, clazz = self) ⇒ Object
23
24
25
26
|
# File 'lib/killbill_client/models/resource.rb', line 23
def get(uri, params = {}, options = {}, clazz = self)
response = KillBillClient::API.get uri, params, options
from_response clazz, response
end
|
.has_many(attr_name, type = nil) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/killbill_client/models/resource.rb', line 144
def has_many(attr_name, type = nil)
send("attr_accessor", attr_name.to_sym)
@@attribute_names[self.name] = {} unless @@attribute_names[self.name]
@@attribute_names[self.name][attr_name.to_sym] = {:type => type, :cardinality => :many }
end
|
.has_one(attr_name, type = nil) ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/killbill_client/models/resource.rb', line 152
def has_one(attr_name, type = nil)
send("attr_accessor", attr_name.to_sym)
@@attribute_names[self.name] = {} unless @@attribute_names[self.name]
@@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :one }
end
|
.head(uri, params = {}, options = {}, clazz = self) ⇒ Object
18
19
20
21
|
# File 'lib/killbill_client/models/resource.rb', line 18
def head(uri, params = {}, options = {}, clazz = self)
response = KillBillClient::API.head uri, params, options
from_response clazz, response
end
|
.instantiate_record_from_json(resource_class, data) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/killbill_client/models/resource.rb', line 98
def instantiate_record_from_json(resource_class, data)
record = resource_class.send :new
data.each do |name, value|
name = Utils.underscore name
attr_desc = @@attribute_names[record.class.name][name.to_sym] rescue nil
unless attr_desc.nil?
type = attr_desc[:type]
if attr_desc[:cardinality] == :many && !type.nil? && value.is_a?(Array)
newValue = []
value.each do |val|
if val.is_a?(Hash)
newValue << instantiate_record_from_json(type, val)
else
newValue << val
end
end
value = newValue
elsif attr_desc[:cardinality] == :one && !type.nil? && value.is_a?(Hash)
value = instantiate_record_from_json(type, value)
end
end
record.send("#{Utils.underscore name}=", value )
end
record
end
|
.post(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object
28
29
30
31
|
# File 'lib/killbill_client/models/resource.rb', line 28
def post(uri, body = nil, params = {}, options = {}, clazz = self)
response = KillBillClient::API.post uri, body, params, options
from_response clazz, response
end
|
.put(uri, body = nil, params = {}, options = {}, clazz = self) ⇒ Object
33
34
35
36
|
# File 'lib/killbill_client/models/resource.rb', line 33
def put(uri, body = nil, params = {}, options = {}, clazz = self)
response = KillBillClient::API.put uri, body, params, options
from_response clazz, response
end
|
Instance Method Details
#==(o) ⇒ Object
Also known as:
eql?
219
220
221
|
# File 'lib/killbill_client/models/resource.rb', line 219
def ==(o)
o.class == self.class && o.to_hash == to_hash
end
|
#_to_hash(value) ⇒ Object
197
198
199
200
201
202
203
204
205
|
# File 'lib/killbill_client/models/resource.rb', line 197
def _to_hash(value)
if value.is_a?(Resource)
value.to_hash
elsif value.is_a?(Array)
value.map { |v| _to_hash(v) }
else
value
end
end
|
#hash ⇒ Object
224
225
226
|
# File 'lib/killbill_client/models/resource.rb', line 224
def hash
to_hash.hash
end
|
#refresh(options = {}, clazz = self.class) ⇒ Object
211
212
213
214
215
216
217
|
# File 'lib/killbill_client/models/resource.rb', line 211
def refresh(options = {}, clazz=self.class)
if @uri
self.class.get @uri, {}, options, clazz
else
self
end
end
|
#to_hash ⇒ Object
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/killbill_client/models/resource.rb', line 186
def to_hash
json_hash = {}
self.class.json_attributes.each do |name|
value = self.send(name)
unless value.nil?
json_hash[Utils.camelize name, :lower] = _to_hash(value)
end
end
json_hash
end
|
#to_json ⇒ Object
207
208
209
|
# File 'lib/killbill_client/models/resource.rb', line 207
def to_json
to_hash.to_json
end
|