Module: Stripe::APIOperations::Update

Included in:
Stripe::ApplicationFeeRefund, Card, Charge, Coupon, Customer, Invoice, InvoiceItem, Plan, Recipient, Refund, Subscription, Transfer
Defined in:
lib/stripe/api_operations/update.rb

Instance Method Summary collapse

Instance Method Details

#save(opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/stripe/api_operations/update.rb', line 4

def save(opts={})
  values = serialize_params(self).merge(opts)

  if @values[:metadata]
    values[:metadata] = 
  end

  if values.length > 0
    values.delete(:id)

    response, api_key = Stripe.request(:post, url, @api_key, values)
    refresh_from(response, api_key)
  end
  self
end

#serialize_metadataObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stripe/api_operations/update.rb', line 20

def 
  if @unsaved_values.include?(:metadata)
    # the metadata object has been reassigned
    # i.e. as object.metadata = {key => val}
     = @values[:metadata]  # new hash
    new_keys = .keys.map(&:to_sym)
    # remove keys at the server, but not known locally
    keys_to_unset = @previous_metadata.keys - new_keys
    keys_to_unset.each {|key| [key] = ''}

    
  else
    # metadata is a StripeObject, and can be serialized normally
    serialize_params(@values[:metadata])
  end
end

#serialize_params(obj) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stripe/api_operations/update.rb', line 37

def serialize_params(obj)
  case obj
  when nil
    ''
  when StripeObject
    unsaved_keys = obj.instance_variable_get(:@unsaved_values)
    obj_values = obj.instance_variable_get(:@values)
    update_hash = {}

    unsaved_keys.each do |k|
      update_hash[k] = serialize_params(obj_values[k])
    end

    update_hash
  else
    obj
  end
end