pnap_billing_api 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/VERSION +1 -1
- data/docs/ApplicableDiscountDetails.md +24 -0
- data/docs/ApplicableDiscounts.md +1 -1
- data/docs/BandwidthRecord.md +5 -3
- data/docs/CreditDetails.md +49 -0
- data/docs/CreditDetailsBase.md +20 -0
- data/docs/CreditTypeEnum.md +15 -0
- data/docs/DiscountDetails.md +2 -2
- data/docs/DiscountTypeEnum.md +15 -0
- data/docs/GpuConfigurationMetadata.md +20 -0
- data/docs/OperatingSystemRecord.md +5 -3
- data/docs/PricingPlan.md +2 -2
- data/docs/PromoCreditDetails.md +24 -0
- data/docs/PublicSubnetRecord.md +5 -3
- data/docs/RatedUsageProductCategoryEnum.md +15 -0
- data/docs/RatedUsageRecord.md +6 -4
- data/docs/ReservationAutoRenewDisableRequest.md +1 -1
- data/docs/ServerProductMetadata.md +3 -1
- data/docs/ServerRecord.md +5 -3
- data/docs/StorageRecord.md +5 -3
- data/docs/SystemCreditCauseEnum.md +15 -0
- data/docs/SystemCreditDetails.md +22 -0
- data/lib/pnap_billing_api/models/applicable_discount_details.rb +294 -0
- data/lib/pnap_billing_api/models/applicable_discounts.rb +1 -1
- data/lib/pnap_billing_api/models/bandwidth_record.rb +14 -16
- data/lib/pnap_billing_api/models/credit_details.rb +105 -0
- data/lib/pnap_billing_api/models/credit_details_base.rb +261 -0
- data/lib/pnap_billing_api/models/credit_type_enum.rb +40 -0
- data/lib/pnap_billing_api/models/discount_details.rb +1 -14
- data/lib/pnap_billing_api/models/discount_type_enum.rb +41 -0
- data/lib/pnap_billing_api/models/gpu_configuration_metadata.rb +226 -0
- data/lib/pnap_billing_api/models/operating_system_record.rb +14 -16
- data/lib/pnap_billing_api/models/promo_credit_details.rb +301 -0
- data/lib/pnap_billing_api/models/public_subnet_record.rb +14 -16
- data/lib/pnap_billing_api/models/rated_usage_product_category_enum.rb +43 -0
- data/lib/pnap_billing_api/models/rated_usage_record.rb +16 -18
- data/lib/pnap_billing_api/models/server_product_metadata.rb +16 -4
- data/lib/pnap_billing_api/models/server_record.rb +14 -16
- data/lib/pnap_billing_api/models/storage_record.rb +14 -16
- data/lib/pnap_billing_api/models/system_credit_cause_enum.rb +39 -0
- data/lib/pnap_billing_api/models/system_credit_details.rb +283 -0
- data/lib/pnap_billing_api.rb +10 -0
- data/spec/models/applicable_discount_details_spec.rb +54 -0
- data/spec/models/bandwidth_record_spec.rb +6 -4
- data/spec/models/credit_details_base_spec.rb +42 -0
- data/spec/models/credit_details_spec.rb +32 -0
- data/spec/models/credit_type_enum_spec.rb +30 -0
- data/spec/models/discount_details_spec.rb +0 -4
- data/spec/models/discount_type_enum_spec.rb +30 -0
- data/spec/models/gpu_configuration_metadata_spec.rb +42 -0
- data/spec/models/operating_system_record_spec.rb +6 -4
- data/spec/models/promo_credit_details_spec.rb +54 -0
- data/spec/models/public_subnet_record_spec.rb +6 -4
- data/spec/models/rated_usage_product_category_enum_spec.rb +30 -0
- data/spec/models/rated_usage_record_spec.rb +6 -4
- data/spec/models/server_product_metadata_spec.rb +6 -0
- data/spec/models/server_record_spec.rb +6 -4
- data/spec/models/storage_record_spec.rb +6 -4
- data/spec/models/system_credit_cause_enum_spec.rb +30 -0
- data/spec/models/system_credit_details_spec.rb +48 -0
- metadata +71 -31
@@ -0,0 +1,294 @@
|
|
1
|
+
=begin
|
2
|
+
#Billing API
|
3
|
+
|
4
|
+
#Automate your infrastructure billing with the Bare Metal Cloud Billing API. Reserve your server instances to ensure guaranteed resource availability for 12, 24, and 36 months. Retrieve your server’s rated usage for a given period and enable or disable auto-renewals.<br> <br> <span class='pnap-api-knowledge-base-link'> Knowledge base articles to help you can be found <a href='https://phoenixnap.com/kb/phoenixnap-bare-metal-cloud-billing-models' target='_blank'>here</a> </span><br> <br> <b>All URLs are relative to (https://api.phoenixnap.com/billing/v1/)</b>
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 0.1
|
7
|
+
Contact: support@phoenixnap.com
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 7.2.0
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'date'
|
14
|
+
require 'time'
|
15
|
+
|
16
|
+
module BillingApi
|
17
|
+
class ApplicableDiscountDetails
|
18
|
+
# A unique code associated with the discount.
|
19
|
+
attr_accessor :code
|
20
|
+
|
21
|
+
attr_accessor :type
|
22
|
+
|
23
|
+
# The value or amount of the discount. The interpretation of this value depends on the 'type' of discount.
|
24
|
+
attr_accessor :value
|
25
|
+
|
26
|
+
# Coupon code which is the source of the discount.
|
27
|
+
attr_accessor :coupon_code
|
28
|
+
|
29
|
+
class EnumAttributeValidator
|
30
|
+
attr_reader :datatype
|
31
|
+
attr_reader :allowable_values
|
32
|
+
|
33
|
+
def initialize(datatype, allowable_values)
|
34
|
+
@allowable_values = allowable_values.map do |value|
|
35
|
+
case datatype.to_s
|
36
|
+
when /Integer/i
|
37
|
+
value.to_i
|
38
|
+
when /Float/i
|
39
|
+
value.to_f
|
40
|
+
else
|
41
|
+
value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid?(value)
|
47
|
+
!value || allowable_values.include?(value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Attribute mapping from ruby-style variable name to JSON key.
|
52
|
+
def self.attribute_map
|
53
|
+
{
|
54
|
+
:'code' => :'code',
|
55
|
+
:'type' => :'type',
|
56
|
+
:'value' => :'value',
|
57
|
+
:'coupon_code' => :'couponCode'
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns all the JSON keys this model knows about
|
62
|
+
def self.acceptable_attributes
|
63
|
+
attribute_map.values
|
64
|
+
end
|
65
|
+
|
66
|
+
# Attribute type mapping.
|
67
|
+
def self.openapi_types
|
68
|
+
{
|
69
|
+
:'code' => :'String',
|
70
|
+
:'type' => :'DiscountTypeEnum',
|
71
|
+
:'value' => :'Float',
|
72
|
+
:'coupon_code' => :'String'
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
# List of attributes with nullable: true
|
77
|
+
def self.openapi_nullable
|
78
|
+
Set.new([
|
79
|
+
])
|
80
|
+
end
|
81
|
+
|
82
|
+
# List of class defined in allOf (OpenAPI v3)
|
83
|
+
def self.openapi_all_of
|
84
|
+
[
|
85
|
+
:'DiscountDetails'
|
86
|
+
]
|
87
|
+
end
|
88
|
+
|
89
|
+
# Initializes the object
|
90
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
91
|
+
def initialize(attributes = {})
|
92
|
+
if (!attributes.is_a?(Hash))
|
93
|
+
fail ArgumentError, "The input argument (attributes) must be a hash in `BillingApi::ApplicableDiscountDetails` initialize method"
|
94
|
+
end
|
95
|
+
|
96
|
+
# check to see if the attribute exists and convert string to symbol for hash key
|
97
|
+
attributes = attributes.each_with_object({}) { |(k, v), h|
|
98
|
+
if (!self.class.attribute_map.key?(k.to_sym))
|
99
|
+
fail ArgumentError, "`#{k}` is not a valid attribute in `BillingApi::ApplicableDiscountDetails`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect
|
100
|
+
end
|
101
|
+
h[k.to_sym] = v
|
102
|
+
}
|
103
|
+
|
104
|
+
if attributes.key?(:'code')
|
105
|
+
self.code = attributes[:'code']
|
106
|
+
else
|
107
|
+
self.code = nil
|
108
|
+
end
|
109
|
+
|
110
|
+
if attributes.key?(:'type')
|
111
|
+
self.type = attributes[:'type']
|
112
|
+
else
|
113
|
+
self.type = nil
|
114
|
+
end
|
115
|
+
|
116
|
+
if attributes.key?(:'value')
|
117
|
+
self.value = attributes[:'value']
|
118
|
+
else
|
119
|
+
self.value = nil
|
120
|
+
end
|
121
|
+
|
122
|
+
if attributes.key?(:'coupon_code')
|
123
|
+
self.coupon_code = attributes[:'coupon_code']
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Show invalid properties with the reasons. Usually used together with valid?
|
128
|
+
# @return Array for valid properties with the reasons
|
129
|
+
def list_invalid_properties
|
130
|
+
warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
|
131
|
+
invalid_properties = Array.new
|
132
|
+
if @code.nil?
|
133
|
+
invalid_properties.push('invalid value for "code", code cannot be nil.')
|
134
|
+
end
|
135
|
+
|
136
|
+
if @type.nil?
|
137
|
+
invalid_properties.push('invalid value for "type", type cannot be nil.')
|
138
|
+
end
|
139
|
+
|
140
|
+
if @value.nil?
|
141
|
+
invalid_properties.push('invalid value for "value", value cannot be nil.')
|
142
|
+
end
|
143
|
+
|
144
|
+
invalid_properties
|
145
|
+
end
|
146
|
+
|
147
|
+
# Check to see if the all the properties in the model are valid
|
148
|
+
# @return true if the model is valid
|
149
|
+
def valid?
|
150
|
+
warn '[DEPRECATED] the `valid?` method is obsolete'
|
151
|
+
return false if @code.nil?
|
152
|
+
return false if @type.nil?
|
153
|
+
return false if @value.nil?
|
154
|
+
true
|
155
|
+
end
|
156
|
+
|
157
|
+
# Checks equality by comparing each attribute.
|
158
|
+
# @param [Object] Object to be compared
|
159
|
+
def ==(o)
|
160
|
+
return true if self.equal?(o)
|
161
|
+
self.class == o.class &&
|
162
|
+
code == o.code &&
|
163
|
+
type == o.type &&
|
164
|
+
value == o.value &&
|
165
|
+
coupon_code == o.coupon_code
|
166
|
+
end
|
167
|
+
|
168
|
+
# @see the `==` method
|
169
|
+
# @param [Object] Object to be compared
|
170
|
+
def eql?(o)
|
171
|
+
self == o
|
172
|
+
end
|
173
|
+
|
174
|
+
# Calculates hash code according to all attributes.
|
175
|
+
# @return [Integer] Hash code
|
176
|
+
def hash
|
177
|
+
[code, type, value, coupon_code].hash
|
178
|
+
end
|
179
|
+
|
180
|
+
# Builds the object from hash
|
181
|
+
# @param [Hash] attributes Model attributes in the form of hash
|
182
|
+
# @return [Object] Returns the model itself
|
183
|
+
def self.build_from_hash(attributes)
|
184
|
+
return nil unless attributes.is_a?(Hash)
|
185
|
+
attributes = attributes.transform_keys(&:to_sym)
|
186
|
+
transformed_hash = {}
|
187
|
+
openapi_types.each_pair do |key, type|
|
188
|
+
if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
|
189
|
+
transformed_hash["#{key}"] = nil
|
190
|
+
elsif type =~ /\AArray<(.*)>/i
|
191
|
+
# check to ensure the input is an array given that the attribute
|
192
|
+
# is documented as an array but the input is not
|
193
|
+
if attributes[attribute_map[key]].is_a?(Array)
|
194
|
+
transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
|
195
|
+
end
|
196
|
+
elsif !attributes[attribute_map[key]].nil?
|
197
|
+
transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
|
198
|
+
end
|
199
|
+
end
|
200
|
+
new(transformed_hash)
|
201
|
+
end
|
202
|
+
|
203
|
+
# Deserializes the data based on type
|
204
|
+
# @param string type Data type
|
205
|
+
# @param string value Value to be deserialized
|
206
|
+
# @return [Object] Deserialized data
|
207
|
+
def self._deserialize(type, value)
|
208
|
+
case type.to_sym
|
209
|
+
when :Time
|
210
|
+
Time.parse(value)
|
211
|
+
when :Date
|
212
|
+
Date.parse(value)
|
213
|
+
when :String
|
214
|
+
value.to_s
|
215
|
+
when :Integer
|
216
|
+
value.to_i
|
217
|
+
when :Float
|
218
|
+
value.to_f
|
219
|
+
when :Boolean
|
220
|
+
if value.to_s =~ /\A(true|t|yes|y|1)\z/i
|
221
|
+
true
|
222
|
+
else
|
223
|
+
false
|
224
|
+
end
|
225
|
+
when :Object
|
226
|
+
# generic object (usually a Hash), return directly
|
227
|
+
value
|
228
|
+
when /\AArray<(?<inner_type>.+)>\z/
|
229
|
+
inner_type = Regexp.last_match[:inner_type]
|
230
|
+
value.map { |v| _deserialize(inner_type, v) }
|
231
|
+
when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
|
232
|
+
k_type = Regexp.last_match[:k_type]
|
233
|
+
v_type = Regexp.last_match[:v_type]
|
234
|
+
{}.tap do |hash|
|
235
|
+
value.each do |k, v|
|
236
|
+
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
else # model
|
240
|
+
# models (e.g. Pet) or oneOf
|
241
|
+
klass = BillingApi.const_get(type)
|
242
|
+
klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
# Returns the string representation of the object
|
247
|
+
# @return [String] String presentation of the object
|
248
|
+
def to_s
|
249
|
+
to_hash.to_s
|
250
|
+
end
|
251
|
+
|
252
|
+
# to_body is an alias to to_hash (backward compatibility)
|
253
|
+
# @return [Hash] Returns the object in the form of hash
|
254
|
+
def to_body
|
255
|
+
to_hash
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns the object in the form of hash
|
259
|
+
# @return [Hash] Returns the object in the form of hash
|
260
|
+
def to_hash
|
261
|
+
hash = {}
|
262
|
+
self.class.attribute_map.each_pair do |attr, param|
|
263
|
+
value = self.send(attr)
|
264
|
+
if value.nil?
|
265
|
+
is_nullable = self.class.openapi_nullable.include?(attr)
|
266
|
+
next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
|
267
|
+
end
|
268
|
+
|
269
|
+
hash[param] = _to_hash(value)
|
270
|
+
end
|
271
|
+
hash
|
272
|
+
end
|
273
|
+
|
274
|
+
# Outputs non-array value in the form of hash
|
275
|
+
# For object, use to_hash. Otherwise, just return the value
|
276
|
+
# @param [Object] value Any valid value
|
277
|
+
# @return [Hash] Returns the value in the form of hash
|
278
|
+
def _to_hash(value)
|
279
|
+
if value.is_a?(Array)
|
280
|
+
value.compact.map { |v| _to_hash(v) }
|
281
|
+
elsif value.is_a?(Hash)
|
282
|
+
{}.tap do |hash|
|
283
|
+
value.each { |k, v| hash[k] = _to_hash(v) }
|
284
|
+
end
|
285
|
+
elsif value.respond_to? :to_hash
|
286
|
+
value.to_hash
|
287
|
+
else
|
288
|
+
value
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
end
|
293
|
+
|
294
|
+
end
|
@@ -18,7 +18,6 @@ module BillingApi
|
|
18
18
|
# The unique identifier of the rated usage record.
|
19
19
|
attr_accessor :id
|
20
20
|
|
21
|
-
# The category of the product associated with this usage record.
|
22
21
|
attr_accessor :product_category
|
23
22
|
|
24
23
|
# The code identifying the product associated to this usage record.
|
@@ -70,6 +69,8 @@ module BillingApi
|
|
70
69
|
|
71
70
|
attr_accessor :discount_details
|
72
71
|
|
72
|
+
attr_accessor :credit_details
|
73
|
+
|
73
74
|
attr_accessor :metadata
|
74
75
|
|
75
76
|
class EnumAttributeValidator
|
@@ -116,6 +117,7 @@ module BillingApi
|
|
116
117
|
:'correlation_id' => :'correlationId',
|
117
118
|
:'reservation_id' => :'reservationId',
|
118
119
|
:'discount_details' => :'discountDetails',
|
120
|
+
:'credit_details' => :'creditDetails',
|
119
121
|
:'metadata' => :'metadata'
|
120
122
|
}
|
121
123
|
end
|
@@ -129,7 +131,7 @@ module BillingApi
|
|
129
131
|
def self.openapi_types
|
130
132
|
{
|
131
133
|
:'id' => :'String',
|
132
|
-
:'product_category' => :'
|
134
|
+
:'product_category' => :'RatedUsageProductCategoryEnum',
|
133
135
|
:'product_code' => :'String',
|
134
136
|
:'location' => :'LocationEnum',
|
135
137
|
:'year_month' => :'String',
|
@@ -146,7 +148,8 @@ module BillingApi
|
|
146
148
|
:'usage_session_id' => :'String',
|
147
149
|
:'correlation_id' => :'String',
|
148
150
|
:'reservation_id' => :'String',
|
149
|
-
:'discount_details' => :'
|
151
|
+
:'discount_details' => :'ApplicableDiscountDetails',
|
152
|
+
:'credit_details' => :'Array<CreditDetails>',
|
150
153
|
:'metadata' => :'BandwidthDetails'
|
151
154
|
}
|
152
155
|
end
|
@@ -283,6 +286,12 @@ module BillingApi
|
|
283
286
|
self.discount_details = attributes[:'discount_details']
|
284
287
|
end
|
285
288
|
|
289
|
+
if attributes.key?(:'credit_details')
|
290
|
+
if (value = attributes[:'credit_details']).is_a?(Array)
|
291
|
+
self.credit_details = value
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
286
295
|
if attributes.key?(:'metadata')
|
287
296
|
self.metadata = attributes[:'metadata']
|
288
297
|
else
|
@@ -364,8 +373,6 @@ module BillingApi
|
|
364
373
|
warn '[DEPRECATED] the `valid?` method is obsolete'
|
365
374
|
return false if @id.nil?
|
366
375
|
return false if @product_category.nil?
|
367
|
-
product_category_validator = EnumAttributeValidator.new('String', ["bmc-server", "bandwidth", "operating-system", "public-ip", "storage"])
|
368
|
-
return false unless product_category_validator.valid?(@product_category)
|
369
376
|
return false if @product_code.nil?
|
370
377
|
return false if @location.nil?
|
371
378
|
return false if @start_date_time.nil?
|
@@ -382,16 +389,6 @@ module BillingApi
|
|
382
389
|
true
|
383
390
|
end
|
384
391
|
|
385
|
-
# Custom attribute writer method checking allowed values (enum).
|
386
|
-
# @param [Object] product_category Object to be assigned
|
387
|
-
def product_category=(product_category)
|
388
|
-
validator = EnumAttributeValidator.new('String', ["bmc-server", "bandwidth", "operating-system", "public-ip", "storage"])
|
389
|
-
unless validator.valid?(product_category)
|
390
|
-
fail ArgumentError, "invalid value for \"product_category\", must be one of #{validator.allowable_values}."
|
391
|
-
end
|
392
|
-
@product_category = product_category
|
393
|
-
end
|
394
|
-
|
395
392
|
# Checks equality by comparing each attribute.
|
396
393
|
# @param [Object] Object to be compared
|
397
394
|
def ==(o)
|
@@ -416,6 +413,7 @@ module BillingApi
|
|
416
413
|
correlation_id == o.correlation_id &&
|
417
414
|
reservation_id == o.reservation_id &&
|
418
415
|
discount_details == o.discount_details &&
|
416
|
+
credit_details == o.credit_details &&
|
419
417
|
metadata == o.metadata
|
420
418
|
end
|
421
419
|
|
@@ -428,7 +426,7 @@ module BillingApi
|
|
428
426
|
# Calculates hash code according to all attributes.
|
429
427
|
# @return [Integer] Hash code
|
430
428
|
def hash
|
431
|
-
[id, product_category, product_code, location, year_month, start_date_time, end_date_time, cost, cost_before_discount, cost_description, price_model, unit_price, unit_price_description, quantity, active, usage_session_id, correlation_id, reservation_id, discount_details, metadata].hash
|
429
|
+
[id, product_category, product_code, location, year_month, start_date_time, end_date_time, cost, cost_before_discount, cost_description, price_model, unit_price, unit_price_description, quantity, active, usage_session_id, correlation_id, reservation_id, discount_details, credit_details, metadata].hash
|
432
430
|
end
|
433
431
|
|
434
432
|
# Builds the object from hash
|
@@ -0,0 +1,105 @@
|
|
1
|
+
=begin
|
2
|
+
#Billing API
|
3
|
+
|
4
|
+
#Automate your infrastructure billing with the Bare Metal Cloud Billing API. Reserve your server instances to ensure guaranteed resource availability for 12, 24, and 36 months. Retrieve your server’s rated usage for a given period and enable or disable auto-renewals.<br> <br> <span class='pnap-api-knowledge-base-link'> Knowledge base articles to help you can be found <a href='https://phoenixnap.com/kb/phoenixnap-bare-metal-cloud-billing-models' target='_blank'>here</a> </span><br> <br> <b>All URLs are relative to (https://api.phoenixnap.com/billing/v1/)</b>
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 0.1
|
7
|
+
Contact: support@phoenixnap.com
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
OpenAPI Generator version: 7.2.0
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
require 'date'
|
14
|
+
require 'time'
|
15
|
+
|
16
|
+
module BillingApi
|
17
|
+
module CreditDetails
|
18
|
+
class << self
|
19
|
+
# List of class defined in oneOf (OpenAPI v3)
|
20
|
+
def openapi_one_of
|
21
|
+
[
|
22
|
+
:'PromoCreditDetails',
|
23
|
+
:'SystemCreditDetails'
|
24
|
+
]
|
25
|
+
end
|
26
|
+
|
27
|
+
# Builds the object
|
28
|
+
# @param [Mixed] Data to be matched against the list of oneOf items
|
29
|
+
# @return [Object] Returns the model or the data itself
|
30
|
+
def build(data)
|
31
|
+
# Go through the list of oneOf items and attempt to identify the appropriate one.
|
32
|
+
# Note:
|
33
|
+
# - We do not attempt to check whether exactly one item matches.
|
34
|
+
# - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 })
|
35
|
+
# due to the way the deserialization is made in the base_object template (it just casts without verifying).
|
36
|
+
# - TODO: scalar values are de facto behaving as if they were nullable.
|
37
|
+
# - TODO: logging when debugging is set.
|
38
|
+
openapi_one_of.each do |klass|
|
39
|
+
begin
|
40
|
+
next if klass == :AnyType # "nullable: true"
|
41
|
+
typed_data = find_and_cast_into_type(klass, data)
|
42
|
+
return typed_data if typed_data
|
43
|
+
rescue # rescue all errors so we keep iterating even if the current item lookup raises
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
openapi_one_of.include?(:AnyType) ? data : nil
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
SchemaMismatchError = Class.new(StandardError)
|
53
|
+
|
54
|
+
# Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse.
|
55
|
+
def find_and_cast_into_type(klass, data)
|
56
|
+
return if data.nil?
|
57
|
+
|
58
|
+
case klass.to_s
|
59
|
+
when 'Boolean'
|
60
|
+
return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
|
61
|
+
when 'Float'
|
62
|
+
return data if data.instance_of?(Float)
|
63
|
+
when 'Integer'
|
64
|
+
return data if data.instance_of?(Integer)
|
65
|
+
when 'Time'
|
66
|
+
return Time.parse(data)
|
67
|
+
when 'Date'
|
68
|
+
return Date.parse(data)
|
69
|
+
when 'String'
|
70
|
+
return data if data.instance_of?(String)
|
71
|
+
when 'Object' # "type: object"
|
72
|
+
return data if data.instance_of?(Hash)
|
73
|
+
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
|
74
|
+
if data.instance_of?(Array)
|
75
|
+
sub_type = Regexp.last_match[:sub_type]
|
76
|
+
return data.map { |item| find_and_cast_into_type(sub_type, item) }
|
77
|
+
end
|
78
|
+
when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }"
|
79
|
+
if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) }
|
80
|
+
sub_type = Regexp.last_match[:sub_type]
|
81
|
+
return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) }
|
82
|
+
end
|
83
|
+
else # model
|
84
|
+
const = BillingApi.const_get(klass)
|
85
|
+
if const
|
86
|
+
if const.respond_to?(:openapi_one_of) # nested oneOf model
|
87
|
+
model = const.build(data)
|
88
|
+
return model if model
|
89
|
+
else
|
90
|
+
# raise if data contains keys that are not known to the model
|
91
|
+
raise unless (data.keys - const.acceptable_attributes).empty?
|
92
|
+
model = const.build_from_hash(data)
|
93
|
+
return model if model && model.valid?
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
raise # if no match by now, raise
|
99
|
+
rescue
|
100
|
+
raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|