recurly 3.0.0.beta.5 → 3.0.0

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.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/.bumpversion.cfg +12 -0
  3. data/.travis.yml +2 -2
  4. data/.yardopts +3 -0
  5. data/CONTRIBUTING.md +102 -0
  6. data/GETTING_STARTED.md +266 -0
  7. data/README.md +12 -194
  8. data/benchmark.rb +16 -0
  9. data/lib/recurly.rb +4 -7
  10. data/lib/recurly/client.rb +60 -60
  11. data/lib/recurly/client/operations.rb +498 -340
  12. data/lib/recurly/errors.rb +4 -0
  13. data/lib/recurly/http.rb +43 -0
  14. data/lib/recurly/pager.rb +4 -10
  15. data/lib/recurly/request.rb +6 -6
  16. data/lib/recurly/requests.rb +8 -0
  17. data/lib/recurly/requests/account_acquisition_updatable.rb +2 -2
  18. data/lib/recurly/requests/billing_info_create.rb +4 -0
  19. data/lib/recurly/requests/custom_field.rb +18 -0
  20. data/lib/recurly/requests/invoice_create.rb +0 -4
  21. data/lib/recurly/requests/invoice_refund.rb +2 -2
  22. data/lib/recurly/requests/line_item_create.rb +4 -0
  23. data/lib/recurly/requests/purchase_create.rb +3 -7
  24. data/lib/recurly/requests/shipping_fee_create.rb +22 -0
  25. data/lib/recurly/requests/shipping_purchase.rb +22 -0
  26. data/lib/recurly/requests/subscription_add_on_create.rb +2 -6
  27. data/lib/recurly/requests/subscription_add_on_update.rb +26 -0
  28. data/lib/recurly/requests/subscription_change_create.rb +7 -3
  29. data/lib/recurly/requests/subscription_change_shipping_create.rb +22 -0
  30. data/lib/recurly/requests/subscription_create.rb +4 -8
  31. data/lib/recurly/requests/subscription_shipping_create.rb +30 -0
  32. data/lib/recurly/requests/subscription_shipping_update.rb +22 -0
  33. data/lib/recurly/requests/subscription_update.rb +3 -7
  34. data/lib/recurly/resource.rb +14 -1
  35. data/lib/recurly/resources.rb +17 -0
  36. data/lib/recurly/resources/account_acquisition.rb +2 -2
  37. data/lib/recurly/resources/add_on.rb +1 -1
  38. data/lib/recurly/resources/billing_info.rb +6 -6
  39. data/lib/recurly/resources/coupon.rb +1 -1
  40. data/lib/recurly/resources/coupon_discount.rb +2 -2
  41. data/lib/recurly/resources/coupon_redemption.rb +2 -2
  42. data/lib/recurly/resources/coupon_redemption_mini.rb +2 -2
  43. data/lib/recurly/resources/error_may_have_transaction.rb +2 -2
  44. data/lib/recurly/resources/invoice.rb +4 -0
  45. data/lib/recurly/resources/line_item.rb +1 -1
  46. data/lib/recurly/resources/{billing_info_payment_method.rb → payment_method.rb} +18 -2
  47. data/lib/recurly/resources/plan.rb +1 -1
  48. data/lib/recurly/resources/shipping_method.rb +42 -0
  49. data/lib/recurly/resources/shipping_method_mini.rb +26 -0
  50. data/lib/recurly/resources/subscription.rb +3 -3
  51. data/lib/recurly/resources/subscription_change.rb +4 -0
  52. data/lib/recurly/resources/subscription_shipping.rb +26 -0
  53. data/lib/recurly/resources/transaction.rb +4 -4
  54. data/lib/recurly/resources/transaction_error.rb +30 -0
  55. data/lib/recurly/schema.rb +85 -49
  56. data/lib/recurly/schema/json_parser.rb +16 -8
  57. data/lib/recurly/schema/request_caster.rb +10 -16
  58. data/lib/recurly/schema/resource_caster.rb +48 -0
  59. data/lib/recurly/schema/schema_factory.rb +0 -2
  60. data/lib/recurly/schema/schema_validator.rb +11 -14
  61. data/lib/recurly/version.rb +1 -1
  62. data/recurly.gemspec +6 -6
  63. data/scripts/build +1 -0
  64. data/scripts/bump +4 -0
  65. data/scripts/format +2 -0
  66. data/scripts/release +11 -0
  67. data/scripts/test +1 -0
  68. metadata +38 -20
  69. data/.ruby-version +0 -1
  70. data/lib/recurly/resources/transaction_payment_method.rb +0 -34
  71. data/lib/recurly/schema/json_deserializer.rb +0 -56
@@ -5,7 +5,7 @@ module Recurly
5
5
  # and get a new Resource.
6
6
  class Resource
7
7
  extend Schema::SchemaFactory
8
- extend Schema::JsonDeserializer
8
+ extend Schema::ResourceCaster
9
9
  include Schema::SchemaValidator
10
10
 
11
11
  attr_reader :attributes
@@ -18,10 +18,23 @@ module Recurly
18
18
  self.attributes == other_resource.attributes
19
19
  end
20
20
 
21
+ # Hide instance variables to keep from accidental logging
22
+ def inspect
23
+ "#<#{self.class.name}:#{object_id}} @attributes=#{attributes}>"
24
+ end
25
+
21
26
  def to_s
22
27
  self.inspect
23
28
  end
24
29
 
30
+ def to_json
31
+ raise NoMethodError, "to_json is not implemented for Resources. Please use Resource#attributes"
32
+ end
33
+
34
+ def get_response
35
+ @response
36
+ end
37
+
25
38
  protected
26
39
 
27
40
  def schema
@@ -0,0 +1,17 @@
1
+ # Include all resource files
2
+ resources = File.join(File.dirname(__FILE__), "resources", "*.rb")
3
+ Dir.glob(resources, &method(:require))
4
+
5
+ module Recurly
6
+ module Resources
7
+ class Empty < Resource
8
+ end
9
+
10
+ class Page < Resource
11
+ # leave data untyped
12
+ define_attribute :data, Array, item_type: Hash
13
+ define_attribute :has_more, :Boolean
14
+ define_attribute :next, String
15
+ end
16
+ end
17
+ end
@@ -19,8 +19,8 @@ module Recurly
19
19
  define_attribute :channel, String
20
20
 
21
21
  # @!attribute cost
22
- # @return [Hash] Account balance
23
- define_attribute :cost, Hash
22
+ # @return [AccountAcquisitionCost]
23
+ define_attribute :cost, :AccountAcquisitionCost
24
24
 
25
25
  # @!attribute created_at
26
26
  # @return [DateTime] When the account acquisition data was created.
@@ -55,7 +55,7 @@ module Recurly
55
55
  define_attribute :state, String
56
56
 
57
57
  # @!attribute tax_code
58
- # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature `P0000000` is `physical`, `D0000000` is `digital`, and an empty string is `unknown`.
58
+ # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature you can use `unknown`, `physical`, or `digital`.
59
59
  define_attribute :tax_code, String
60
60
 
61
61
  # @!attribute updated_at
@@ -27,8 +27,8 @@ module Recurly
27
27
  define_attribute :first_name, String
28
28
 
29
29
  # @!attribute fraud
30
- # @return [Hash] Most recent fraud result.
31
- define_attribute :fraud, Hash
30
+ # @return [FraudInfo] Most recent fraud result.
31
+ define_attribute :fraud, :FraudInfo
32
32
 
33
33
  # @!attribute id
34
34
  # @return [String]
@@ -43,16 +43,16 @@ module Recurly
43
43
  define_attribute :object, String
44
44
 
45
45
  # @!attribute payment_method
46
- # @return [Hash]
47
- define_attribute :payment_method, Hash
46
+ # @return [PaymentMethod]
47
+ define_attribute :payment_method, :PaymentMethod
48
48
 
49
49
  # @!attribute updated_at
50
50
  # @return [DateTime] When the billing information was last changed.
51
51
  define_attribute :updated_at, DateTime
52
52
 
53
53
  # @!attribute updated_by
54
- # @return [Hash]
55
- define_attribute :updated_by, Hash
54
+ # @return [BillingInfoUpdatedBy]
55
+ define_attribute :updated_by, :BillingInfoUpdatedBy
56
56
 
57
57
  # @!attribute valid
58
58
  # @return [Boolean]
@@ -75,7 +75,7 @@ module Recurly
75
75
  define_attribute :object, String
76
76
 
77
77
  # @!attribute plans
78
- # @return [Array[PlanMini]] Plans
78
+ # @return [Array[PlanMini]] A list of plans for which this coupon applies. This will be `null` if `applies_to_all_plans=true`.
79
79
  define_attribute :plans, Array, { :item_type => :PlanMini }
80
80
 
81
81
  # @!attribute plans_names
@@ -15,8 +15,8 @@ module Recurly
15
15
  define_attribute :percent, Integer
16
16
 
17
17
  # @!attribute trial
18
- # @return [Hash] This is only present when `type=free_trial`.
19
- define_attribute :trial, Hash
18
+ # @return [CouponDiscountTrial] This is only present when `type=free_trial`.
19
+ define_attribute :trial, :CouponDiscountTrial
20
20
 
21
21
  # @!attribute type
22
22
  # @return [String]
@@ -23,8 +23,8 @@ module Recurly
23
23
  define_attribute :currency, String
24
24
 
25
25
  # @!attribute discounted
26
- # @return [String] The amount that was discounted upon the application of the coupon, formatted with the currency.
27
- define_attribute :discounted, String
26
+ # @return [Float] The amount that was discounted upon the application of the coupon, formatted with the currency.
27
+ define_attribute :discounted, Float
28
28
 
29
29
  # @!attribute id
30
30
  # @return [String] Coupon Redemption ID
@@ -15,8 +15,8 @@ module Recurly
15
15
  define_attribute :created_at, DateTime
16
16
 
17
17
  # @!attribute discounted
18
- # @return [String] The amount that was discounted upon the application of the coupon, formatted with the currency.
19
- define_attribute :discounted, String
18
+ # @return [Float] The amount that was discounted upon the application of the coupon, formatted with the currency.
19
+ define_attribute :discounted, Float
20
20
 
21
21
  # @!attribute id
22
22
  # @return [String] Coupon Redemption ID
@@ -15,8 +15,8 @@ module Recurly
15
15
  define_attribute :params, Array, { :item_type => Hash }
16
16
 
17
17
  # @!attribute transaction_error
18
- # @return [Hash] This is only included on errors with `type=transaction`.
19
- define_attribute :transaction_error, Hash
18
+ # @return [TransactionError] This is only included on errors with `type=transaction`.
19
+ define_attribute :transaction_error, :TransactionError
20
20
 
21
21
  # @!attribute type
22
22
  # @return [String] Type
@@ -90,6 +90,10 @@ module Recurly
90
90
  # @return [Float] The refundable amount on a charge invoice. It will be null for all other invoices.
91
91
  define_attribute :refundable_amount, Float
92
92
 
93
+ # @!attribute shipping_address
94
+ # @return [ShippingAddress]
95
+ define_attribute :shipping_address, :ShippingAddress
96
+
93
97
  # @!attribute state
94
98
  # @return [String] Invoice state
95
99
  define_attribute :state, String
@@ -139,7 +139,7 @@ module Recurly
139
139
  define_attribute :tax, Float
140
140
 
141
141
  # @!attribute tax_code
142
- # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature `P0000000` is `physical`, `D0000000` is `digital`, and an empty string is `unknown`.
142
+ # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature you can use `unknown`, `physical`, or `digital`.
143
143
  define_attribute :tax_code, String
144
144
 
145
145
  # @!attribute tax_exempt
@@ -4,7 +4,15 @@
4
4
  # need and we will usher them to the appropriate places.
5
5
  module Recurly
6
6
  module Resources
7
- class BillingInfoPaymentMethod < Resource
7
+ class PaymentMethod < Resource
8
+
9
+ # @!attribute account_type
10
+ # @return [String] The bank account type. Only present for ACH payment methods.
11
+ define_attribute :account_type, String
12
+
13
+ # @!attribute billing_agreement_id
14
+ # @return [String] Billing Agreement identifier. Only present for Amazon or Paypal payment methods.
15
+ define_attribute :billing_agreement_id, String
8
16
 
9
17
  # @!attribute card_type
10
18
  # @return [String] Visa, MasterCard, American Express, Discover, JCB, etc.
@@ -23,12 +31,20 @@ module Recurly
23
31
  define_attribute :first_six, String
24
32
 
25
33
  # @!attribute last_four
26
- # @return [String] Credit card number's last four digits.
34
+ # @return [String] Credit card number's last four digits. Will refer to bank account if payment method is ACH.
27
35
  define_attribute :last_four, String
28
36
 
29
37
  # @!attribute object
30
38
  # @return [String]
31
39
  define_attribute :object, String
40
+
41
+ # @!attribute routing_number
42
+ # @return [String] The bank account's routing number. Only present for ACH payment methods.
43
+ define_attribute :routing_number, String
44
+
45
+ # @!attribute routing_number_bank
46
+ # @return [String] The bank name of this routing number.
47
+ define_attribute :routing_number_bank, String
32
48
  end
33
49
  end
34
50
  end
@@ -67,7 +67,7 @@ module Recurly
67
67
  define_attribute :state, String
68
68
 
69
69
  # @!attribute tax_code
70
- # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature `P0000000` is `physical`, `D0000000` is `digital`, and an empty string is `unknown`.
70
+ # @return [String] Used by Avalara, Vertex, and Recurly’s EU VAT tax feature. The tax code values are specific to each tax system. If you are using Recurly’s EU VAT feature you can use `unknown`, `physical`, or `digital`.
71
71
  define_attribute :tax_code, String
72
72
 
73
73
  # @!attribute tax_exempt
@@ -0,0 +1,42 @@
1
+ # This file is automatically created by Recurly's OpenAPI generation process
2
+ # and thus any edits you make by hand will be lost. If you wish to make a
3
+ # change to this file, please create a Github issue explaining the changes you
4
+ # need and we will usher them to the appropriate places.
5
+ module Recurly
6
+ module Resources
7
+ class ShippingMethod < Resource
8
+
9
+ # @!attribute code
10
+ # @return [String] The internal name used identify the shipping method.
11
+ define_attribute :code, String
12
+
13
+ # @!attribute created_at
14
+ # @return [DateTime] Created at
15
+ define_attribute :created_at, DateTime
16
+
17
+ # @!attribute deleted_at
18
+ # @return [DateTime] Deleted at
19
+ define_attribute :deleted_at, DateTime
20
+
21
+ # @!attribute id
22
+ # @return [String] Shipping Method ID
23
+ define_attribute :id, String
24
+
25
+ # @!attribute name
26
+ # @return [String] The name of the shipping method displayed to customers.
27
+ define_attribute :name, String
28
+
29
+ # @!attribute object
30
+ # @return [String] Object type
31
+ define_attribute :object, String
32
+
33
+ # @!attribute tax_code
34
+ # @return [String] Used by Avalara, Vertex, and Recurly’s built-in tax feature. The tax code values are specific to each tax system. If you are using Recurly’s built-in taxes the values are: - `FR` – Common Carrier FOB Destination - `FR022000` – Common Carrier FOB Origin - `FR020400` – Non Common Carrier FOB Destination - `FR020500` – Non Common Carrier FOB Origin - `FR010100` – Delivery by Company Vehicle Before Passage of Title - `FR010200` – Delivery by Company Vehicle After Passage of Title - `NT` – Non-Taxable
35
+ define_attribute :tax_code, String
36
+
37
+ # @!attribute updated_at
38
+ # @return [DateTime] Last updated at
39
+ define_attribute :updated_at, DateTime
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ # This file is automatically created by Recurly's OpenAPI generation process
2
+ # and thus any edits you make by hand will be lost. If you wish to make a
3
+ # change to this file, please create a Github issue explaining the changes you
4
+ # need and we will usher them to the appropriate places.
5
+ module Recurly
6
+ module Resources
7
+ class ShippingMethodMini < Resource
8
+
9
+ # @!attribute code
10
+ # @return [String] The internal name used identify the shipping method.
11
+ define_attribute :code, String
12
+
13
+ # @!attribute id
14
+ # @return [String] Shipping Method ID
15
+ define_attribute :id, String
16
+
17
+ # @!attribute name
18
+ # @return [String] The name of the shipping method displayed to customers.
19
+ define_attribute :name, String
20
+
21
+ # @!attribute object
22
+ # @return [String] Object type
23
+ define_attribute :object, String
24
+ end
25
+ end
26
+ end
@@ -126,9 +126,9 @@ module Recurly
126
126
  # @return [Integer] If `auto_renew=true`, when a term completes, `total_billing_cycles` takes this value as the length of subsequent terms. Defaults to the plan's `total_billing_cycles`.
127
127
  define_attribute :renewal_billing_cycles, Integer
128
128
 
129
- # @!attribute shipping_address
130
- # @return [ShippingAddress]
131
- define_attribute :shipping_address, :ShippingAddress
129
+ # @!attribute shipping
130
+ # @return [SubscriptionShipping]
131
+ define_attribute :shipping, :SubscriptionShipping
132
132
 
133
133
  # @!attribute state
134
134
  # @return [String] State
@@ -42,6 +42,10 @@ module Recurly
42
42
  # @return [Integer] Subscription quantity
43
43
  define_attribute :quantity, Integer
44
44
 
45
+ # @!attribute shipping
46
+ # @return [SubscriptionShipping]
47
+ define_attribute :shipping, :SubscriptionShipping
48
+
45
49
  # @!attribute subscription_id
46
50
  # @return [String] The ID of the subscription that is going to be changed.
47
51
  define_attribute :subscription_id, String
@@ -0,0 +1,26 @@
1
+ # This file is automatically created by Recurly's OpenAPI generation process
2
+ # and thus any edits you make by hand will be lost. If you wish to make a
3
+ # change to this file, please create a Github issue explaining the changes you
4
+ # need and we will usher them to the appropriate places.
5
+ module Recurly
6
+ module Resources
7
+ class SubscriptionShipping < Resource
8
+
9
+ # @!attribute address
10
+ # @return [ShippingAddress]
11
+ define_attribute :address, :ShippingAddress
12
+
13
+ # @!attribute amount
14
+ # @return [Float] Subscription's shipping cost
15
+ define_attribute :amount, Float
16
+
17
+ # @!attribute method
18
+ # @return [ShippingMethodMini]
19
+ define_attribute :method, :ShippingMethodMini
20
+
21
+ # @!attribute object
22
+ # @return [String] Object type
23
+ define_attribute :object, String
24
+ end
25
+ end
26
+ end
@@ -103,12 +103,12 @@ module Recurly
103
103
  define_attribute :original_transaction_id, String
104
104
 
105
105
  # @!attribute payment_gateway
106
- # @return [Hash]
107
- define_attribute :payment_gateway, Hash
106
+ # @return [TransactionPaymentGateway]
107
+ define_attribute :payment_gateway, :TransactionPaymentGateway
108
108
 
109
109
  # @!attribute payment_method
110
- # @return [Hash] Payment method (TODO: this overlaps with BillingInfo’s payment_method but only documents credit cards)
111
- define_attribute :payment_method, Hash
110
+ # @return [PaymentMethod]
111
+ define_attribute :payment_method, :PaymentMethod
112
112
 
113
113
  # @!attribute refunded
114
114
  # @return [Boolean] Indicates if part or all of this transaction was refunded.
@@ -0,0 +1,30 @@
1
+ # This file is automatically created by Recurly's OpenAPI generation process
2
+ # and thus any edits you make by hand will be lost. If you wish to make a
3
+ # change to this file, please create a Github issue explaining the changes you
4
+ # need and we will usher them to the appropriate places.
5
+ module Recurly
6
+ module Resources
7
+ class TransactionError < Resource
8
+
9
+ # @!attribute category
10
+ # @return [String] Category
11
+ define_attribute :category, String
12
+
13
+ # @!attribute code
14
+ # @return [String] Code
15
+ define_attribute :code, String
16
+
17
+ # @!attribute merchant_advice
18
+ # @return [String] Merchant message
19
+ define_attribute :merchant_advice, String
20
+
21
+ # @!attribute message
22
+ # @return [String] Customer message
23
+ define_attribute :message, String
24
+
25
+ # @!attribute transaction_id
26
+ # @return [String] Transaction ID
27
+ define_attribute :transaction_id, String
28
+ end
29
+ end
30
+ end
@@ -3,21 +3,21 @@ module Recurly
3
3
  # This is used for requests and resources.
4
4
  class Schema
5
5
  # The attributes in the schema
6
- # @return [Array<Attribute>]
6
+ # @return [Hash<String,Attribute>]
7
7
  attr_reader :attributes
8
8
 
9
9
  def initialize
10
- @attributes = []
10
+ @attributes = {}
11
11
  end
12
12
 
13
13
  # Adds an attribute to the schema definition
14
14
  #
15
15
  # @param name [Symbol] The name of the attribute
16
16
  # @param type [Class,Symbol] The type of the attribute. Use capitalized symbol for Recurly class. Example: :Account.
17
- # @param options [Hash] The attribute options. See {Attribute#options}
17
+ # @param options [Schema::Attribute] The created and registered attribute object.
18
18
  def add_attribute(name, type, options)
19
- attribute = Attribute.new(name, type, options)
20
- @attributes.push(attribute)
19
+ attribute = Attribute.build(type, options)
20
+ @attributes[name.to_s] = attribute
21
21
  attribute
22
22
  end
23
23
 
@@ -26,8 +26,7 @@ module Recurly
26
26
  # @param name [String,Symbol] The name/key of the attribute
27
27
  # @return [Attribute,nil] The found Attribute. nil if not found.
28
28
  def get_attribute(name)
29
- name = name.to_s
30
- @attributes.find { |a| a.name.to_s == name }
29
+ @attributes[name.to_s]
31
30
  end
32
31
 
33
32
  # Gets a recurly class given a symbol name.
@@ -46,75 +45,112 @@ module Recurly
46
45
  Resources::Address
47
46
  elsif Requests.const_defined?(type)
48
47
  Requests.const_get(type)
49
- elsif Resources.const_defined?(type)
48
+ elsif Recurly::Resources.const_defined?(type)
50
49
  Resources.const_get(type)
51
50
  else
52
51
  raise ArgumentError, "Recurly type '#{type}' is unknown"
53
52
  end
54
53
  end
55
54
 
56
- # Describes a list attribute type
57
- class List
58
- # The type of the elements of the list
59
- # @return [Symbol]
60
- attr_accessor :item_type
55
+ class Attribute
56
+ # The type of the attribute. Might be a class like `DateTime`
57
+ # or could be a Recurly object. In this case a symbol should be used.
58
+ # Example: :Account. To get the Recurly type use #recurly_class
59
+ # @return [Class,Symbol]
60
+ attr_reader :type
61
+
62
+ PRIMITIVE_TYPES = [
63
+ String,
64
+ Integer,
65
+ Float,
66
+ Hash,
67
+ ].freeze
68
+
69
+ def self.build(type, options = {})
70
+ if PRIMITIVE_TYPES.include? type
71
+ PrimitiveAttribute.new(type)
72
+ elsif type == :Boolean
73
+ BooleanAttribute.new
74
+ elsif type == DateTime
75
+ DateTimeAttribute.new
76
+ elsif type.is_a? Symbol
77
+ ResourceAttribute.new(type)
78
+ elsif type == Array
79
+ item_attr = build(options[:item_type])
80
+ ArrayAttribute.new(item_attr)
81
+ else
82
+ throw ArgumentError
83
+ end
84
+ end
61
85
 
62
- def initialize(item_type)
63
- @item_type = item_type
86
+ def initialize(type = nil)
87
+ @type = type
64
88
  end
65
89
 
66
- def to_s
67
- "List<#{item_type}>"
90
+ def cast(value)
91
+ value
68
92
  end
69
- end
70
93
 
71
- # Describes and attribute for a schema.
72
- class Attribute
73
- # The name of the attribute.
74
- # @return [Symbol]
75
- attr_accessor :name
94
+ def recurly_class
95
+ @recurly_class ||= Schema.get_recurly_class(type)
96
+ end
97
+ end
76
98
 
77
- # The type of the attribute. Might be a class like `DateTime`
78
- # or could be a Recurly object. In this case a symbol should be used.
79
- # Example: :Account
80
- # @return [Class,Symbol]
81
- attr_accessor :type
99
+ class PrimitiveAttribute < Attribute
100
+ def is_valid?(value)
101
+ value.is_a? self.type
102
+ end
103
+ end
82
104
 
83
- # Options for the attribute.
84
- # @return [Hash]
85
- attr_accessor :options
105
+ class BooleanAttribute < Attribute
106
+ def is_valid?(value)
107
+ [true, false].include? value
108
+ end
109
+ end
86
110
 
87
- # The description of the attribute for documentation.
88
- # @return [String]
89
- attr_accessor :description
111
+ class DateTimeAttribute < Attribute
112
+ def is_valid?(value)
113
+ value.is_a?(String) || value.is_a?(DateTime)
114
+ end
90
115
 
91
- def initialize(name, type, options = {}, description = nil)
92
- @name = name
93
- @type = type
94
- @options = options
95
- @description = description
116
+ def cast(value)
117
+ if value.is_a?(DateTime)
118
+ value
119
+ else
120
+ DateTime.parse(value)
121
+ end
96
122
  end
97
123
 
98
- def read_only?
99
- @options.fetch(:read_only, false)
124
+ def type
125
+ DateTime
100
126
  end
127
+ end
101
128
 
102
- def recurly_class
103
- Schema.get_recurly_class(type == Array ? options[:item_type] : type)
129
+ class ResourceAttribute < Attribute
130
+ def is_valid?(value)
131
+ value.is_a? Hash
104
132
  end
105
133
 
106
- def is_primitive?
107
- t = type == Array ? options[:item_type] : type
108
- t.is_a?(Class) || t == :Boolean
134
+ def cast(value)
135
+ self.recurly_class.cast(value)
109
136
  end
110
137
  end
111
138
 
112
- private_constant :List
113
- private_constant :Attribute
139
+ class ArrayAttribute < Attribute
140
+ def is_valid?(value)
141
+ value.is_a? Array
142
+ end
143
+
144
+ def cast(value)
145
+ value.map do |v|
146
+ self.type.cast(v)
147
+ end
148
+ end
149
+ end
114
150
  end
115
151
 
116
152
  require_relative "./schema/schema_factory"
117
153
  require_relative "./schema/schema_validator"
118
- require_relative "./schema/json_deserializer"
154
+ require_relative "./schema/resource_caster"
119
155
  require_relative "./schema/request_caster"
120
156
  end