Class: NewStoreApi::PercentageDiscount

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/new_store_api/models/percentage_discount.rb

Overview

A percentage discount.

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(applied_amount = nil, id = nil, rate = nil, scope = nil, coupon_code = SKIP, description = SKIP, external_reference = SKIP) ⇒ PercentageDiscount

Returns a new instance of PercentageDiscount.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/new_store_api/models/percentage_discount.rb', line 80

def initialize(applied_amount = nil, id = nil, rate = nil, scope = nil,
               coupon_code = SKIP, description = SKIP,
               external_reference = SKIP)
  @applied_amount = applied_amount
  @coupon_code = coupon_code unless coupon_code == SKIP
  @description = description unless description == SKIP
  @external_reference = external_reference unless external_reference == SKIP
  @id = id
  @rate = rate
  @scope = scope
  @type = 'percentage'
end

Instance Attribute Details

#applied_amountInteger

The discount amount applied to this line item, in the currency's minor unit. For a discount prorated from an order-level promotion, this is only this line's share.

Returns:

  • (Integer)


16
17
18
# File 'lib/new_store_api/models/percentage_discount.rb', line 16

def applied_amount
  @applied_amount
end

#coupon_codeString

The coupon code that triggered the discount, when one was used.

Returns:

  • (String)


20
21
22
# File 'lib/new_store_api/models/percentage_discount.rb', line 20

def coupon_code
  @coupon_code
end

#descriptionString

Human-readable description of why the discount was applied.

Returns:

  • (String)


24
25
26
# File 'lib/new_store_api/models/percentage_discount.rb', line 24

def description
  @description
end

#external_referenceString

To identify the discount in the external system.

Returns:

  • (String)


28
29
30
# File 'lib/new_store_api/models/percentage_discount.rb', line 28

def external_reference
  @external_reference
end

#idUUID | String

ID of the discount; shared across all lines from the same promotion so downstream can aggregate them.

Returns:

  • (UUID | String)


33
34
35
# File 'lib/new_store_api/models/percentage_discount.rb', line 33

def id
  @id
end

#rateFloat

The discount percentage (0-100).

Returns:

  • (Float)


37
38
39
# File 'lib/new_store_api/models/percentage_discount.rb', line 37

def rate
  @rate
end

#scopeLevelEnum

Whether the discount was applied at the item level or prorated from an order-level discount.

Returns:



42
43
44
# File 'lib/new_store_api/models/percentage_discount.rb', line 42

def scope
  @scope
end

#typeString (readonly)

Discriminates a percentage discount.

Returns:

  • (String)


46
47
48
# File 'lib/new_store_api/models/percentage_discount.rb', line 46

def type
  @type
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/new_store_api/models/percentage_discount.rb', line 94

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  applied_amount =
    hash.key?('applied_amount') ? hash['applied_amount'] : nil
  id = hash.key?('id') ? hash['id'] : nil
  rate = hash.key?('rate') ? hash['rate'] : nil
  scope = hash.key?('scope') ? hash['scope'] : nil
  coupon_code = hash.key?('coupon_code') ? hash['coupon_code'] : SKIP
  description = hash.key?('description') ? hash['description'] : SKIP
  external_reference =
    hash.key?('external_reference') ? hash['external_reference'] : SKIP

  # Create object from extracted values.
  PercentageDiscount.new(applied_amount,
                         id,
                         rate,
                         scope,
                         coupon_code,
                         description,
                         external_reference)
end

.namesObject

A mapping from model property names to API property names.



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

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['applied_amount'] = 'applied_amount'
  @_hash['coupon_code'] = 'coupon_code'
  @_hash['description'] = 'description'
  @_hash['external_reference'] = 'external_reference'
  @_hash['id'] = 'id'
  @_hash['rate'] = 'rate'
  @_hash['scope'] = 'scope'
  @_hash['type'] = 'type'
  @_hash
end

.nullablesObject

An array for nullable fields



72
73
74
75
76
77
78
# File 'lib/new_store_api/models/percentage_discount.rb', line 72

def self.nullables
  %w[
    coupon_code
    description
    external_reference
  ]
end

.optionalsObject

An array for optional fields



63
64
65
66
67
68
69
# File 'lib/new_store_api/models/percentage_discount.rb', line 63

def self.optionals
  %w[
    coupon_code
    description
    external_reference
  ]
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/new_store_api/models/percentage_discount.rb', line 120

def self.validate(value)
  if value.instance_of? self
    return (
      APIHelper.valid_type?(value.applied_amount,
                            ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.id,
                              ->(val) { val.instance_of? String }) and
        APIHelper.valid_type?(value.rate,
                              ->(val) { val.instance_of? Float }) and
        APIHelper.valid_type?(value.scope,
                              ->(val) { LevelEnum.validate(val) }) and
        APIHelper.valid_type?(value.type,
                              ->(val) { val.instance_of? String })
    )
  end

  return false unless value.instance_of? Hash

  (
    APIHelper.valid_type?(value['applied_amount'],
                          ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['id'],
                            ->(val) { val.instance_of? String }) and
      APIHelper.valid_type?(value['rate'],
                            ->(val) { val.instance_of? Float }) and
      APIHelper.valid_type?(value['scope'],
                            ->(val) { LevelEnum.validate(val) }) and
      APIHelper.valid_type?(value['type'],
                            ->(val) { val.instance_of? String })
  )
end

Instance Method Details

#inspectObject

Provides a debugging-friendly string with detailed object information.



161
162
163
164
165
166
167
# File 'lib/new_store_api/models/percentage_discount.rb', line 161

def inspect
  class_name = self.class.name.split('::').last
  "<#{class_name} applied_amount: #{@applied_amount.inspect}, coupon_code:"\
  " #{@coupon_code.inspect}, description: #{@description.inspect}, external_reference:"\
  " #{@external_reference.inspect}, id: #{@id.inspect}, rate: #{@rate.inspect}, scope:"\
  " #{@scope.inspect}, type: #{@type.inspect}>"
end

#to_sObject

Provides a human-readable string representation of the object.



153
154
155
156
157
158
# File 'lib/new_store_api/models/percentage_discount.rb', line 153

def to_s
  class_name = self.class.name.split('::').last
  "<#{class_name} applied_amount: #{@applied_amount}, coupon_code: #{@coupon_code},"\
  " description: #{@description}, external_reference: #{@external_reference}, id: #{@id},"\
  " rate: #{@rate}, scope: #{@scope}, type: #{@type}>"
end