Class: ApiReference::Coupon

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/api_reference/models/coupon.rb

Overview

A coupon represents a reusable discount configuration that can be applied either as a fixed or percentage amount to an invoice or subscription. Coupons are activated using a redemption code, which applies the discount to a subscription or invoice. The duration of a coupon determines how long it remains available for use by end users.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#check_for_conflict, #process_additional_properties, #process_array, #process_basic_value, #process_hash, #to_hash, #to_json

Constructor Details

#initialize(id:, redemption_code:, discount:, times_redeemed:, duration_in_months:, max_redemptions:, archived_at:, additional_properties: nil) ⇒ Coupon

Returns a new instance of Coupon.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/api_reference/models/coupon.rb', line 76

def initialize(id:, redemption_code:, discount:, times_redeemed:,
               duration_in_months:, max_redemptions:, archived_at:,
               additional_properties: nil)
  # Add additional model properties to the instance
  additional_properties = {} if additional_properties.nil?

  @id = id
  @redemption_code = redemption_code
  @discount = discount
  @times_redeemed = times_redeemed
  @duration_in_months = duration_in_months
  @max_redemptions = max_redemptions
  @archived_at = archived_at
  @additional_properties = additional_properties
end

Instance Attribute Details

#archived_atDateTime

An archived coupon can no longer be redeemed. Active coupons will have a value of null for archived_at; this field will be non-null for archived coupons.

Returns:

  • (DateTime)


47
48
49
# File 'lib/api_reference/models/coupon.rb', line 47

def archived_at
  @archived_at
end

#discountObject

This string can be used to redeem this coupon for a given subscription.

Returns:

  • (Object)


27
28
29
# File 'lib/api_reference/models/coupon.rb', line 27

def discount
  @discount
end

#duration_in_monthsInteger

This allows for a coupon's discount to apply for a limited time (determined in months); a null value here means "unlimited time".

Returns:

  • (Integer)


36
37
38
# File 'lib/api_reference/models/coupon.rb', line 36

def duration_in_months
  @duration_in_months
end

#idString

Also referred to as coupon_id in this documentation.

Returns:

  • (String)


19
20
21
# File 'lib/api_reference/models/coupon.rb', line 19

def id
  @id
end

#max_redemptionsInteger

The maximum number of redemptions allowed for this coupon before it is exhausted; null here means "unlimited".

Returns:

  • (Integer)


41
42
43
# File 'lib/api_reference/models/coupon.rb', line 41

def max_redemptions
  @max_redemptions
end

#redemption_codeString

This string can be used to redeem this coupon for a given subscription.

Returns:

  • (String)


23
24
25
# File 'lib/api_reference/models/coupon.rb', line 23

def redemption_code
  @redemption_code
end

#times_redeemedInteger

The number of times this coupon has been redeemed.

Returns:

  • (Integer)


31
32
33
# File 'lib/api_reference/models/coupon.rb', line 31

def times_redeemed
  @times_redeemed
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



93
94
95
96
97
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
127
128
129
# File 'lib/api_reference/models/coupon.rb', line 93

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  id = hash.key?('id') ? hash['id'] : nil
  redemption_code =
    hash.key?('redemption_code') ? hash['redemption_code'] : nil
  discount = hash.key?('discount') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:CouponDiscount), hash['discount']
  ) : nil
  times_redeemed =
    hash.key?('times_redeemed') ? hash['times_redeemed'] : nil
  duration_in_months =
    hash.key?('duration_in_months') ? hash['duration_in_months'] : nil
  max_redemptions =
    hash.key?('max_redemptions') ? hash['max_redemptions'] : nil
  archived_at = if hash.key?('archived_at')
                  (DateTimeHelper.from_rfc3339(hash['archived_at']) if hash['archived_at'])
                end

  # Create a new hash for additional properties, removing known properties.
  new_hash = hash.reject { |k, _| names.value?(k) }

  additional_properties = APIHelper.get_additional_properties(
    new_hash, proc { |value| value }
  )

  # Create object from extracted values.
  Coupon.new(id: id,
             redemption_code: redemption_code,
             discount: discount,
             times_redeemed: times_redeemed,
             duration_in_months: duration_in_months,
             max_redemptions: max_redemptions,
             archived_at: archived_at,
             additional_properties: additional_properties)
end

.namesObject

A mapping from model property names to API property names.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/api_reference/models/coupon.rb', line 50

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['id'] = 'id'
  @_hash['redemption_code'] = 'redemption_code'
  @_hash['discount'] = 'discount'
  @_hash['times_redeemed'] = 'times_redeemed'
  @_hash['duration_in_months'] = 'duration_in_months'
  @_hash['max_redemptions'] = 'max_redemptions'
  @_hash['archived_at'] = 'archived_at'
  @_hash
end

.nullablesObject

An array for nullable fields



68
69
70
71
72
73
74
# File 'lib/api_reference/models/coupon.rb', line 68

def self.nullables
  %w[
    duration_in_months
    max_redemptions
    archived_at
  ]
end

.optionalsObject

An array for optional fields



63
64
65
# File 'lib/api_reference/models/coupon.rb', line 63

def self.optionals
  []
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:

  • The (Coupon | Hash)

    value against the validation is performed.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/api_reference/models/coupon.rb', line 137

def self.validate(value)
  if value.instance_of? self
    return (
      APIHelper.valid_type?(value.id,
                            ->(val) { val.instance_of? String }) and
        APIHelper.valid_type?(value.redemption_code,
                              ->(val) { val.instance_of? String }) and
        UnionTypeLookUp.get(:CouponDiscount)
                       .validate(value.discount) and
        APIHelper.valid_type?(value.times_redeemed,
                              ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.duration_in_months,
                              ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.max_redemptions,
                              ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.archived_at,
                              ->(val) { val.instance_of? DateTime })
    )
  end

  return false unless value.instance_of? Hash

  (
    APIHelper.valid_type?(value['id'],
                          ->(val) { val.instance_of? String }) and
      APIHelper.valid_type?(value['redemption_code'],
                            ->(val) { val.instance_of? String }) and
      UnionTypeLookUp.get(:CouponDiscount)
                     .validate(value['discount']) and
      APIHelper.valid_type?(value['times_redeemed'],
                            ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['duration_in_months'],
                            ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['max_redemptions'],
                            ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['archived_at'],
                            ->(val) { val.instance_of? String })
  )
end

Instance Method Details

#inspectObject

Provides a debugging-friendly string with detailed object information.



187
188
189
190
191
192
193
# File 'lib/api_reference/models/coupon.rb', line 187

def inspect
  class_name = self.class.name.split('::').last
  "<#{class_name} id: #{@id.inspect}, redemption_code: #{@redemption_code.inspect}, discount:"\
  " #{@discount.inspect}, times_redeemed: #{@times_redeemed.inspect}, duration_in_months:"\
  " #{@duration_in_months.inspect}, max_redemptions: #{@max_redemptions.inspect}, archived_at:"\
  " #{@archived_at.inspect}, additional_properties: #{@additional_properties}>"
end

#to_custom_archived_atObject



131
132
133
# File 'lib/api_reference/models/coupon.rb', line 131

def to_custom_archived_at
  DateTimeHelper.to_rfc3339(archived_at)
end

#to_sObject

Provides a human-readable string representation of the object.



178
179
180
181
182
183
184
# File 'lib/api_reference/models/coupon.rb', line 178

def to_s
  class_name = self.class.name.split('::').last
  "<#{class_name} id: #{@id}, redemption_code: #{@redemption_code}, discount: #{@discount},"\
  " times_redeemed: #{@times_redeemed}, duration_in_months: #{@duration_in_months},"\
  " max_redemptions: #{@max_redemptions}, archived_at: #{@archived_at}, additional_properties:"\
  " #{@additional_properties}>"
end