Class: Spree::Integrations::Klaviyo
- Inherits:
-
Spree::Integration
- Object
- Spree::Integration
- Spree::Integrations::Klaviyo
- Defined in:
- app/models/spree/integrations/klaviyo.rb
Constant Summary collapse
- NO_PROFILE_FOUND =
'No profile found'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #can_connect? ⇒ Boolean
- #create_back_in_stock_subscription(email:, variant_id:) ⇒ Object
- #create_event(event:, resource:, email:, guest_id: nil) ⇒ Object
- #create_profile(user, guest_id = nil) ⇒ Object
- #fetch_profile(email:) ⇒ Object
- #subscribe_user(email) ⇒ Object
- #unsubscribe_user(email) ⇒ Object
- #update_profile(user, guest_id = nil) ⇒ Object
Class Method Details
.icon_path ⇒ Object
18 19 20 |
# File 'app/models/spree/integrations/klaviyo.rb', line 18 def self.icon_path 'integration_icons/klaviyo-logo.png' end |
.integration_group ⇒ Object
14 15 16 |
# File 'app/models/spree/integrations/klaviyo.rb', line 14 def self.integration_group 'marketing' end |
Instance Method Details
#can_connect? ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/spree/integrations/klaviyo.rb', line 22 def can_connect? # There's no method for checking if credentials are valid, but we can figure it out basing on response, # except Public API Key. result = client.get_request("lists/#{preferred_default_newsletter_list_id}") # 'Missing or invalid private key.' for invalid private key # 'A list with id #{id} does not exist.' for invalid newsletter list id = JSON.parse(result.value)['errors'].first['detail'] if result.failure? result.success? end |
#create_back_in_stock_subscription(email:, variant_id:) ⇒ Object
73 74 75 76 77 78 |
# File 'app/models/spree/integrations/klaviyo.rb', line 73 def create_back_in_stock_subscription(email:, variant_id:) body = ::SpreeKlaviyo::BackInStockSubscriptionPresenter.new(email: email, variant_id: variant_id).call result = client.post_request('back-in-stock-subscriptions/', body) handle_result(result) end |
#create_event(event:, resource:, email:, guest_id: nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/spree/integrations/klaviyo.rb', line 80 def create_event(event:, resource:, email:, guest_id: nil) result = client.post_request( 'events/', ::SpreeKlaviyo::EventPresenter.new( integration: self, event: event, resource: resource, email: email, guest_id: guest_id ).call ) handle_result(result) end |
#create_profile(user, guest_id = nil) ⇒ Object
34 35 36 37 38 39 |
# File 'app/models/spree/integrations/klaviyo.rb', line 34 def create_profile(user, guest_id = nil) user_presenter = ::SpreeKlaviyo::UserPresenter.new(email: user.email, address: user&.bill_address, guest_id: guest_id) result = client.post_request('profiles/', user_presenter.call) handle_result(result) end |
#fetch_profile(email:) ⇒ Object
95 96 97 98 99 100 101 |
# File 'app/models/spree/integrations/klaviyo.rb', line 95 def fetch_profile(email:) result = client.get_request("profiles/?fields[profile]=email&filter=equals(email,'#{CGI.escapeURIComponent(email)}')") return Spree::ServiceModule::Result.new(false, email, NO_PROFILE_FOUND) if result.success? && JSON.parse(result.value)['data'].empty? handle_result(result) end |
#subscribe_user(email) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'app/models/spree/integrations/klaviyo.rb', line 48 def subscribe_user(email) result = client.post_request( 'profile-subscription-bulk-create-jobs/', ::SpreeKlaviyo::SubscribePresenter.new(email: email, list_id: ).call ) handle_result(result) end |
#unsubscribe_user(email) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/spree/integrations/klaviyo.rb', line 57 def unsubscribe_user(email) payload = ::SpreeKlaviyo::SubscribePresenter.new( email: email, list_id: , type: 'profile-subscription-bulk-delete-job', subscribed: false ).call result = client.post_request( 'profile-subscription-bulk-delete-jobs/', payload ) handle_result(result) end |
#update_profile(user, guest_id = nil) ⇒ Object
41 42 43 44 45 46 |
# File 'app/models/spree/integrations/klaviyo.rb', line 41 def update_profile(user, guest_id = nil) user_presenter = ::SpreeKlaviyo::UserPresenter.new(email: user.email, address: user&.bill_address, user: user, guest_id: guest_id) result = client.patch_request("profiles/#{user.klaviyo_id}/", user_presenter.call) handle_result(result) end |