moonshot-rails 0.0.9 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Moonshot
|
4
|
+
module Convertkit
|
5
|
+
class UpsertJob < ApplicationJob
|
6
|
+
queue_as :default
|
7
|
+
|
8
|
+
delegate :convertkit_api_key, :convertkit_api_secret, :convertkit_form_id, to: 'MoonshotRails.configuration'
|
9
|
+
|
10
|
+
def perform user_object_or_klass, user_id: nil, first_name:, fields:
|
11
|
+
# return unless Rails.env.production?
|
12
|
+
|
13
|
+
user = if user_object_or_klass.is_a?(String)
|
14
|
+
klass = user_object_or_klass.constantize
|
15
|
+
klass.respond_to?(:with_deleted) ? klass.with_deleted.find(user_id) : klass.find(user_id)
|
16
|
+
else
|
17
|
+
user_object_or_klass
|
18
|
+
end
|
19
|
+
|
20
|
+
return unless user.respond_to?(:email) && user.email.present?
|
21
|
+
|
22
|
+
assert_required_config!
|
23
|
+
|
24
|
+
if existing_subscriber = find_by(email: user.email)
|
25
|
+
params = {
|
26
|
+
first_name: user.respond_to?(first_name) ? user.send(first_name) : nil
|
27
|
+
}
|
28
|
+
params.merge!(fields: user.send(fields)) if user.respond_to?(fields)
|
29
|
+
|
30
|
+
put_request "subscribers/#{existing_subscriber['id']}", params
|
31
|
+
else
|
32
|
+
params = {
|
33
|
+
email: user.email,
|
34
|
+
first_name: user.respond_to?(first_name) ? user.send(first_name) : nil
|
35
|
+
}
|
36
|
+
params.merge!(fields: user.send(fields)) if user.respond_to?(fields)
|
37
|
+
|
38
|
+
post_request "forms/#{convertkit_form_id}/subscribe", params
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def find_by email:
|
43
|
+
response = get_request({ email_address: email })
|
44
|
+
|
45
|
+
return if response['total_subscribers'].zero?
|
46
|
+
|
47
|
+
if response['total_subscribers'] > 1
|
48
|
+
raise "Multiple (#{response['total_subscribers']}) subscribers found for email #{email}"
|
49
|
+
end
|
50
|
+
|
51
|
+
response['subscribers'].first
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
|
56
|
+
def get_request query, path: 'subscribers'
|
57
|
+
params = query.merge({
|
58
|
+
api_key: convertkit_api_key,
|
59
|
+
api_secret: convertkit_api_secret
|
60
|
+
}).compact
|
61
|
+
|
62
|
+
url = "https://api.convertkit.com/v3/#{path}?#{params.to_query}"
|
63
|
+
|
64
|
+
Excon
|
65
|
+
.get(url)
|
66
|
+
.tap { |response| raise "Error #{response.status}: #{response.body}" unless response.status == 200 }
|
67
|
+
.then { |response| JSON.parse(response.body) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def post_request path, body
|
71
|
+
json_body = body.merge(api_key: convertkit_api_key).to_json
|
72
|
+
|
73
|
+
Excon
|
74
|
+
.post("https://api.convertkit.com/v3/#{path}", body: json_body, headers: { 'Content-Type' => 'application/json' })
|
75
|
+
.tap { |response| raise "Error #{response.status}: #{response.body}" unless response.status == 200 }
|
76
|
+
.then { |response| JSON.parse(response.body) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def put_request path, body
|
80
|
+
json_body = body.merge(api_secret: convertkit_api_secret).to_json
|
81
|
+
|
82
|
+
Excon
|
83
|
+
.put("https://api.convertkit.com/v3/#{path}", body: json_body, headers: { 'Content-Type' => 'application/json' })
|
84
|
+
.tap { |response| raise "Error #{response.status}: #{response.body}" unless response.status == 200 }
|
85
|
+
.then { |response| JSON.parse(response.body) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def assert_required_config!
|
89
|
+
%i[convertkit_api_key convertkit_api_secret convertkit_form_id].each do |config|
|
90
|
+
raise MoonshotRails::MissingConfig, "#{config} is required" unless send(config).present?
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -5,14 +5,14 @@ module Moonshot
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
class_methods do
|
8
|
-
def convertkit_sync
|
9
|
-
after_save_commit :
|
8
|
+
def convertkit_sync first_name: :name, fields: :convertkit_fields
|
9
|
+
after_save_commit -> { moonshot_convertkit_upsert first_name: first_name, fields: fields }
|
10
10
|
after_destroy_commit :moonshot_convertkit_unsubscribe
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
Moonshot::Convertkit::
|
14
|
+
def moonshot_convertkit_upsert first_name: :name, fields: :convertkit_fields
|
15
|
+
Moonshot::Convertkit::UpsertJob.perform_later self.class.name, user_id: id, first_name: first_name, fields: fields
|
16
16
|
end
|
17
17
|
|
18
18
|
def moonshot_convertkit_unsubscribe
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moonshot-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Catalin Ionescu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: excon
|
@@ -70,8 +70,8 @@ files:
|
|
70
70
|
- app/components/moonshot/app/jobs_to_be_done.rb
|
71
71
|
- app/helpers/moonshot/moonshot_helper.rb
|
72
72
|
- app/jobs/moonshot/application_job.rb
|
73
|
-
- app/jobs/moonshot/convertkit/subscribe_job.rb
|
74
73
|
- app/jobs/moonshot/convertkit/unsubscribe_job.rb
|
74
|
+
- app/jobs/moonshot/convertkit/upsert_job.rb
|
75
75
|
- app/models/moonshot/model_helper.rb
|
76
76
|
- lib/generators/moonshot_rails_generator.rb
|
77
77
|
- lib/moonshot-rails.rb
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Moonshot
|
4
|
-
module Convertkit
|
5
|
-
class SubscribeJob < ApplicationJob
|
6
|
-
queue_as :default
|
7
|
-
|
8
|
-
delegate :convertkit_api_key, :convertkit_api_secret, :convertkit_form_id, to: 'MoonshotRails.configuration'
|
9
|
-
|
10
|
-
def perform user
|
11
|
-
return unless Rails.env.production?
|
12
|
-
|
13
|
-
assert_required_config!
|
14
|
-
|
15
|
-
existing_subscriber = find_by(email: user.email)
|
16
|
-
return existing_subscriber if existing_subscriber
|
17
|
-
|
18
|
-
params = {
|
19
|
-
email: user.email
|
20
|
-
}
|
21
|
-
params.merge!(fields: user.convertkit_fields) if user.respond_to?(:convertkit_fields)
|
22
|
-
|
23
|
-
post_request "forms/#{convertkit_form_id}/subscribe", params
|
24
|
-
end
|
25
|
-
|
26
|
-
def find_by email:
|
27
|
-
response = get_request({ email_address: email })
|
28
|
-
|
29
|
-
return if response['total_subscribers'].zero?
|
30
|
-
|
31
|
-
if response['total_subscribers'] > 1
|
32
|
-
raise "Multiple (#{response['total_subscribers']}) subscribers found for email #{email}"
|
33
|
-
end
|
34
|
-
|
35
|
-
response['subscribers'].first
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
|
40
|
-
def get_request query, path: 'subscribers'
|
41
|
-
params = query.merge({
|
42
|
-
api_key: convertkit_api_key,
|
43
|
-
api_secret: convertkit_api_secret
|
44
|
-
}).compact
|
45
|
-
|
46
|
-
url = "https://api.convertkit.com/v3/#{path}?#{params.to_query}"
|
47
|
-
|
48
|
-
Excon
|
49
|
-
.get(url)
|
50
|
-
.then { |response| JSON.parse(response.body) }
|
51
|
-
end
|
52
|
-
|
53
|
-
def post_request path, body
|
54
|
-
json_body = body.merge(api_key: convertkit_api_key).to_json
|
55
|
-
|
56
|
-
Excon
|
57
|
-
.post("https://api.convertkit.com/v3/#{path}", body: json_body, headers: { 'Content-Type' => 'application/json' })
|
58
|
-
.then { |response| JSON.parse(response.body) }
|
59
|
-
end
|
60
|
-
|
61
|
-
def assert_required_config!
|
62
|
-
%i[convertkit_api_key convertkit_api_secret convertkit_form_id].each do |config|
|
63
|
-
raise MoonshotRails::MissingConfig, "#{config} is required" unless send(config).present?
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|