• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-ruby / #5867

18 Apr 2024 09:24PM UTC coverage: 92.724% (-4.8%) from 97.485%
#5867

push

github

ramya-stripe
Bump version to 11.2.0

10067 of 10857 relevant lines covered (92.72%)

258.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/lib/stripe/api_operations/save.rb
1
# frozen_string_literal: true
2

3
module Stripe
1✔
4
  module APIOperations
1✔
5
    module Save
1✔
6
      module ClassMethods
1✔
7
        # Updates an API resource
8
        #
9
        # Updates the identified resource with the passed in parameters.
10
        #
11
        # ==== Attributes
12
        #
13
        # * +id+ - ID of the resource to update.
14
        # * +params+ - A hash of parameters to pass to the API
15
        # * +opts+ - A Hash of additional options (separate from the params /
16
        #   object values) to be added to the request. E.g. to allow for an
17
        #   idempotency_key to be passed in the request headers, or for the
18
        #   api_key to be overwritten. See
19
        #   {APIOperations::Request.execute_resource_request}.
20
        def update(id, params = {}, opts = {})
1✔
21
          params.each_key do |k|
3✔
22
            raise ArgumentError, "Cannot update protected field: #{k}" if protected_fields.include?(k)
3✔
23
          end
24

25
          request_stripe_object(
2✔
26
            method: :post,
27
            path: "#{resource_url}/#{id}",
×
28
            params: params,
29
            opts: opts
30
          )
31
        end
32
      end
33

34
      # The `save` method is DEPRECATED and will be removed in a future major
35
      # version of the library. Use the `update` method on the resource instead.
36
      #
37
      # Creates or updates an API resource.
38
      #
39
      # If the resource doesn't yet have an assigned ID and the resource is one
40
      # that can be created, then the method attempts to create the resource.
41
      # The resource is updated otherwise.
42
      #
43
      # ==== Attributes
44
      #
45
      # * +params+ - Overrides any parameters in the resource's serialized data
46
      #   and includes them in the create or update. If +:req_url:+ is included
47
      #   in the list, it overrides the update URL used for the create or
48
      #   update.
49
      # * +opts+ - A Hash of additional options (separate from the params /
50
      #   object values) to be added to the request. E.g. to allow for an
51
      #   idempotency_key to be passed in the request headers, or for the
52
      #   api_key to be overwritten. See
53
      #   {APIOperations::Request.execute_resource_request}.
54
      def save(params = {}, opts = {})
1✔
55
        # We started unintentionally (sort of) allowing attributes sent to
56
        # +save+ to override values used during the update. So as not to break
57
        # the API, this makes that official here.
58
        update_attributes(params)
57✔
59

60
        # Now remove any parameters that look like object attributes.
61
        params = params.reject { |k, _| respond_to?(k) }
59✔
62

63
        values = serialize_params(self).merge(params)
57✔
64

65
        # Please note that id gets removed here our call to #url above has already
66
        # generated a uri for this object with an identifier baked in
67
        values.delete(:id)
57✔
68

69
        resp, opts = execute_resource_request(:post, save_url, values, opts, ["save"])
57✔
70
        initialize_from(resp.data, opts, resp)
56✔
71
      end
72
      extend Gem::Deprecate
1✔
73
      deprecate :save, "the `update` class method (for examples " \
1✔
74
                       "see https://github.com/stripe/stripe-ruby" \
75
                       "/wiki/Migration-guide-for-v8)", 2022, 11
76

77
      def self.included(base)
1✔
78
        # Set `metadata` as additive so that when it's set directly we remember
79
        # to clear keys that may have been previously set by sending empty
80
        # values for them.
81
        #
82
        # It's possible that not every object with `Save` has `metadata`, but
83
        # it's a close enough heuristic, and having this option set when there
84
        # is no `metadata` field is not harmful.
85
        base.additive_object_param(:metadata)
58✔
86

87
        base.extend(ClassMethods)
58✔
88
      end
89

90
      private def save_url
1✔
91
        # This switch essentially allows us "upsert"-like functionality. If the
92
        # API resource doesn't have an ID set (suggesting that it's new) and
93
        # its class responds to .create (which comes from
94
        # Stripe::APIOperations::Create), then use the URL to create a new
95
        # resource. Otherwise, generate a URL based on the object's identifier
96
        # for a normal update.
97
        if self[:id].nil? && self.class.respond_to?(:create)
57✔
98
          self.class.resource_url
4✔
99
        else
100
          resource_url
53✔
101
        end
102
      end
103
    end
104
  end
105
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc