Class: StripeModelCallbacks::SyncFromStripe

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/stripe_model_callbacks/sync_from_stripe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

advisory_lock_id, advisory_lock_name, execute_with_advisory_lock!, reported_execute!, with_exception_notifications

Constructor Details

#initialize(stripe_object:) ⇒ SyncFromStripe

Returns a new instance of SyncFromStripe.



4
5
6
# File 'app/services/stripe_model_callbacks/sync_from_stripe.rb', line 4

def initialize(stripe_object:)
  @stripe_object = stripe_object
end

Instance Attribute Details

#stripe_objectObject (readonly)

Returns the value of attribute stripe_object.



2
3
4
# File 'app/services/stripe_model_callbacks/sync_from_stripe.rb', line 2

def stripe_object
  @stripe_object
end

Instance Method Details

#model_classObject



26
27
28
# File 'app/services/stripe_model_callbacks/sync_from_stripe.rb', line 26

def model_class
  @model_class ||= model_class_name.safe_constantize
end

#model_class_nameObject



22
23
24
# File 'app/services/stripe_model_callbacks/sync_from_stripe.rb', line 22

def model_class_name
  @model_class_name ||= stripe_object.class.name.gsub("::", "")
end

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/stripe_model_callbacks/sync_from_stripe.rb', line 8

def perform
  model = model_class.find_by(stripe_id: stripe_object.id)

  if model
    model.stripe_object = stripe_object
    model.assign_from_stripe(stripe_object)
    model.save! if model.changed?
  else
    model = model_class.create_from_stripe!(stripe_object)
  end

  succeed!(model: model)
end