moonshot-rails 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moonshot
4
+ class ApplicationJob < ActiveJob::Base
5
+ end
6
+ end
@@ -0,0 +1,68 @@
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
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moonshot
4
+ module Convertkit
5
+ class UnsubscribeJob < ApplicationJob
6
+ queue_as :default
7
+
8
+ delegate :convertkit_api_secret, to: 'MoonshotRails.configuration'
9
+
10
+ def perform email
11
+ return unless Rails.env.production?
12
+
13
+ assert_required_config!
14
+
15
+ Excon
16
+ .put(
17
+ 'https://api.convertkit.com/v3/unsubscribe',
18
+ body: {
19
+ email: email,
20
+ api_secret: convertkit_api_secret
21
+ }.to_json,
22
+ headers: { 'Content-Type' => 'application/json' }
23
+ ).then { |response| JSON.parse(response.body) }
24
+ end
25
+
26
+ private
27
+
28
+ def assert_required_config!
29
+ raise MoonshotRails::MissingConfig, 'Missing convertkit_api_secret' unless convertkit_api_secret
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moonshot
4
+ module ModelHelper
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def convertkit_sync
9
+ after_save_commit :moonshot_convertkit_subscribe
10
+ after_destroy_commit :moonshot_convertkit_unsubscribe
11
+ end
12
+ end
13
+
14
+ def moonshot_convertkit_subscribe
15
+ Moonshot::Convertkit::SubscribeJob.perform_later self
16
+ end
17
+
18
+ def moonshot_convertkit_unsubscribe
19
+ Moonshot::Convertkit::UnsubscribeJob.perform_later email
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MoonshotRailsGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../..', __dir__)
5
+
6
+ desc 'Generate an initializer'
7
+ def create_initializer_file
8
+ copy_file 'config/moonshot_rails_initializer.rb', 'config/initializers/moonshot_rails.rb'
9
+ end
10
+ end
@@ -2,5 +2,8 @@
2
2
 
3
3
  module MoonshotRails
4
4
  class Config
5
+ cattr_accessor :convertkit_api_key
6
+ cattr_accessor :convertkit_api_secret
7
+ cattr_accessor :convertkit_form_id
5
8
  end
6
9
  end
@@ -6,6 +6,7 @@ module MoonshotRails
6
6
 
7
7
  config.autoload_paths = %W[
8
8
  #{root}/app/helpers
9
+ #{root}/app/models
9
10
  ]
10
11
 
11
12
  initializer 'moonshot_rails.helpers' do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoonshotRails
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.9'
5
5
  end
@@ -3,6 +3,8 @@ require 'moonshot-rails/engine'
3
3
  require 'moonshot-rails/config'
4
4
 
5
5
  module MoonshotRails
6
+ MissingConfig = Class.new(StandardError)
7
+
6
8
  class << self
7
9
  attr_accessor :configuration
8
10
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonshot-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
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-03-06 00:00:00.000000000 Z
11
+ date: 2024-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: excon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.100'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.100'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rails
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -30,14 +44,14 @@ dependencies:
30
44
  requirements:
31
45
  - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 3.0.0
47
+ version: 2.0.0
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: 3.0.0
54
+ version: 2.0.0
41
55
  description:
42
56
  email:
43
57
  - catalin.ionescu282@gmail.com
@@ -55,6 +69,11 @@ files:
55
69
  - app/components/moonshot/app/jobs_to_be_done.html.haml
56
70
  - app/components/moonshot/app/jobs_to_be_done.rb
57
71
  - app/helpers/moonshot/moonshot_helper.rb
72
+ - app/jobs/moonshot/application_job.rb
73
+ - app/jobs/moonshot/convertkit/subscribe_job.rb
74
+ - app/jobs/moonshot/convertkit/unsubscribe_job.rb
75
+ - app/models/moonshot/model_helper.rb
76
+ - lib/generators/moonshot_rails_generator.rb
58
77
  - lib/moonshot-rails.rb
59
78
  - lib/moonshot-rails/config.rb
60
79
  - lib/moonshot-rails/engine.rb