bullet_train-incoming_webhooks 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cca44c64cd5bbdf7bd18f914d3e24a7b4d2429ceb1c6afc1f10cc2378dfd267
4
- data.tar.gz: 0a446ad675c1e044c6b7d886a40f240cf2b04b11659ea168e18add4200e1bc30
3
+ metadata.gz: 9d266a24c49abec06dc6ed74ec895ae76db652e2aa840ccd96b7729fe6a3b874
4
+ data.tar.gz: c179bc769b668e5ae59413bb8f22d8abd81a670ec8fc8b8689d70300a5cf99c3
5
5
  SHA512:
6
- metadata.gz: 922ee62630cda4224cd8cb4a3794a4e801d0fd4802898f72595add7c5b935297ddcb04d07c1887c777c0184a45d8b655792fd6b7712dce3a5ce0447ef5083bb7
7
- data.tar.gz: 27cc13bcf8efb5f531aab7fc71c775af3a8278b83bea15f69049e4a24900a79c8d6d99be586305e5c42f06599fc3de77c8015c294b1d0606bbba9fc1f23613c2
6
+ metadata.gz: ddc431b0bebcdba456c6de8c090918b8af1651c48cfd1e2dba0091b87b850621ddffaf28effc170ff6202b4ffaa5ca31fc938d27cacceb9ce3caa7b9bd107dfe
7
+ data.tar.gz: a357da04e00aa607a9270f160f98bf69524276e074f73ddb6922b4b4d491e6969bccecaac1fa441b189b9484c95d5218e5111573056587060957905a0648fca4
@@ -5,6 +5,7 @@ class Webhooks::Incoming::BulletTrainWebhook < ApplicationRecord
5
5
  # there are many ways a service might ask you to verify the validity of a webhook.
6
6
  # whatever that method is, you would implement it here.
7
7
  def verify_authenticity
8
+ # 🚅 skip this section when scaffolding.
8
9
  # trying to fix integration tests. if this fixes it, i don't know why puma won't accept another connection here.
9
10
  return true if Rails.env.test?
10
11
 
@@ -19,6 +20,7 @@ class Webhooks::Incoming::BulletTrainWebhook < ApplicationRecord
19
20
 
20
21
  # ensure that the payload on the server is the same as the payload we received.
21
22
  JSON.parse(response.body) == data
23
+ # 🚅 stop any skipping we're doing now.
22
24
  end
23
25
 
24
26
  def process
@@ -1,6 +1,16 @@
1
+ require "bullet_train/incoming_webhooks/scaffolders/incoming_webhooks_scaffolder"
2
+ require "bullet_train/super_scaffolding"
3
+
1
4
  module BulletTrain
2
5
  module IncomingWebhooks
3
6
  class Engine < ::Rails::Engine
7
+ initializer "bullet_train.super_scaffolding.incoming_webhooks.templates.register_template_path" do |app|
8
+ # Register the base of this package with the Super Scaffolding engine.
9
+ BulletTrain::SuperScaffolding.template_paths << File.expand_path("../../../..", __FILE__)
10
+ BulletTrain::SuperScaffolding.scaffolders.merge!({
11
+ "incoming-webhooks" => "BulletTrain::IncomingWebhooks::Scaffolders::IncomingWebhooksScaffolder"
12
+ })
13
+ end
4
14
  end
5
15
  end
6
16
  end
@@ -0,0 +1,41 @@
1
+ require "scaffolding/incoming_webhooks_transformer"
2
+ require "bullet_train/super_scaffolding/scaffolder"
3
+
4
+ module BulletTrain
5
+ module IncomingWebhooks
6
+ module Scaffolders
7
+ class IncomingWebhooksScaffolder < SuperScaffolding::Scaffolder
8
+ def run
9
+ unless argv.count >= 1
10
+ puts ""
11
+ puts "🚅 usage: bin/super-scaffold incoming-webhooks SomeProvider"
12
+ puts ""
13
+ puts "E.g. prepare to receive system-level webhooks from ClickFunnels"
14
+ puts " bin/super-scaffold incoming-webhooks ClickFunnels"
15
+ puts ""
16
+ standard_protip
17
+ puts ""
18
+ return
19
+ end
20
+
21
+ provider_name = argv.shift
22
+ transformer = Scaffolding::IncomingWebhooksTransformer.new(provider_name)
23
+
24
+ `yes n | bin/rails g model webhooks_incoming_#{provider_name.tableize.singularize}_webhook data:jsonb processed_at:datetime verified_at:datetime`
25
+
26
+ transformer.scaffold_incoming_webhook
27
+
28
+ puts ""
29
+ puts "1. To receive webhooks in your development environment, you'll need to configure a tunnel.".yellow
30
+ puts " See http://bullettrain.co/docs/tunneling for more information.".yellow
31
+ puts ""
32
+ puts "2. Once you have a tunnel running, you can configure the provider to deliver webhooks to:".yellow
33
+ puts " https://your-tunnel.ngrok.io/webhooks/incoming/#{provider_name.tableize}_webhooks".yellow
34
+ puts ""
35
+
36
+ transformer.restart_server
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  module BulletTrain
2
2
  module IncomingWebhooks
3
- VERSION = "1.0.1"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,99 @@
1
+ require "scaffolding"
2
+ require "scaffolding/transformer"
3
+
4
+ class Scaffolding::IncomingWebhooksTransformer < Scaffolding::Transformer
5
+ attr_accessor :provider_name
6
+
7
+ def initialize(provider_name, cli_options = {})
8
+ super("", "", cli_options)
9
+ self.provider_name = provider_name
10
+ end
11
+
12
+ def scaffold_incoming_webhook
13
+ files = [
14
+ "./app/models/webhooks/incoming/bullet_train_webhook.rb",
15
+ "./app/controllers/webhooks/incoming/bullet_train_webhooks_controller.rb"
16
+ ]
17
+
18
+ files.each do |name|
19
+ if File.directory?(resolve_template_path(name))
20
+ scaffold_directory(name)
21
+ else
22
+ scaffold_file(name)
23
+ end
24
+ end
25
+
26
+ file_name_hook = "bullet_train_webhook"
27
+ new_model_file_name, _ = files.map { |file| file.gsub(file_name_hook, replacement_for(file_name_hook)) }
28
+
29
+ # Since we eject the model from this repository, we don't need the
30
+ # extra model generated by `rails g model` (just the migration).
31
+ File.delete("./app/models/webhooks_incoming_#{replacement_for(file_name_hook)}.rb")
32
+
33
+ # Set up the model's `verify_authenticity` method to return `true`.
34
+ model_file_lines = File.readlines(new_model_file_name)
35
+ comment_lines = [
36
+ "# You can implement your authenticity verification logic in either\n",
37
+ "# the newly scaffolded model or controller for your incoming webhooks.\n"
38
+ ]
39
+ lines_to_ignore = [
40
+ " # there are many ways a service might ask you to verify the validity of a webhook.\n",
41
+ " # whatever that method is, you would implement it here.\n"
42
+ ]
43
+
44
+ model_file_lines = File.readlines(new_model_file_name)
45
+ new_model_file_lines = File.open(new_model_file_name).map.with_index do |line, idx|
46
+ if line.match?("def verify_authenticity")
47
+ indentation = Scaffolding::BlockManipulator.indentation_of(idx, model_file_lines)
48
+ new_comment_lines = comment_lines.map { |comment_line| "#{indentation}#{comment_line}" }.join
49
+
50
+ new_comment_lines +
51
+ "#{line}" \
52
+ "#{indentation} true\n"
53
+ elsif lines_to_ignore.include?(line)
54
+ next
55
+ else
56
+ line
57
+ end
58
+ end
59
+
60
+ Scaffolding::FileManipulator.write(new_model_file_name, new_model_file_lines)
61
+
62
+ # Apply new routes
63
+ begin
64
+ [
65
+ "config/routes.rb",
66
+ # "config/routes/api/v1.rb"
67
+ ].each do |routes_file|
68
+ # Since the webhooks routes don't live under a parent resource, we can't use the `apply` method to apply routes.
69
+ routes_manipulator = Scaffolding::RoutesFileManipulator.new(routes_file, "", "")
70
+ resources_line = " resources :#{replacement_for("bullet_train_webhooks")}"
71
+ new_routes_lines = Scaffolding::BlockManipulator.insert(resources_line, lines: routes_manipulator.lines, after: "namespace :incoming")
72
+ Scaffolding::FileManipulator.write(routes_file, new_routes_lines)
73
+ end
74
+ rescue BulletTrain::SuperScaffolding::CannotFindParentResourceException => exception
75
+ add_additional_step :red, "We were not able to generate the routes for your Incoming Webhook automatically because: \"#{exception.message}\" You'll need to add them manually, which admittedly can be complicated. See https://blog.bullettrain.co/nested-namespaced-rails-routing-examples/ for guidance. 🙇🏻‍♂️"
76
+ end
77
+ end
78
+
79
+ def transform_string(string)
80
+ [
81
+ "bullet_train_webhook",
82
+ "Webhooks::Incoming::BulletTrainWebhook"
83
+ ].each do |needle|
84
+ string = string.gsub(needle, replacement_for(needle))
85
+ end
86
+ string
87
+ end
88
+
89
+ def replacement_for(string)
90
+ case string
91
+ when "bullet_train_webhook"
92
+ "#{provider_name.tableize.singularize}_webhook"
93
+ when "bullet_train_webhooks"
94
+ "#{provider_name.tableize.singularize}_webhooks"
95
+ when "Webhooks::Incoming::BulletTrainWebhook"
96
+ "Webhooks::Incoming::#{provider_name}Webhook"
97
+ end
98
+ end
99
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet_train-incoming_webhooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-11 00:00:00.000000000 Z
11
+ date: 2022-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bullet_train-super_scaffolding
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: Bullet Train Incoming Webhooks
28
42
  email:
29
43
  - andrew.culver@gmail.com
@@ -43,7 +57,9 @@ files:
43
57
  - config/routes.rb
44
58
  - lib/bullet_train/incoming_webhooks.rb
45
59
  - lib/bullet_train/incoming_webhooks/engine.rb
60
+ - lib/bullet_train/incoming_webhooks/scaffolders/incoming_webhooks_scaffolder.rb
46
61
  - lib/bullet_train/incoming_webhooks/version.rb
62
+ - lib/scaffolding/incoming_webhooks_transformer.rb
47
63
  - lib/tasks/bullet_train/incoming_webhooks_tasks.rake
48
64
  homepage: https://github.com/bullet-train-co/bullet_train-incoming_webhooks
49
65
  licenses:
@@ -66,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
82
  - !ruby/object:Gem::Version
67
83
  version: '0'
68
84
  requirements: []
69
- rubygems_version: 3.2.22
85
+ rubygems_version: 3.3.7
70
86
  signing_key:
71
87
  specification_version: 4
72
88
  summary: Bullet Train Incoming Webhooks