Captain 0.0.1 → 0.0.2
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 +4 -4
- data/Captain-0.0.1.gem +0 -0
- data/Captain.gemspec +37 -0
- data/Gemfile +6 -11
- data/Gemfile.lock +16 -29
- data/README.md +22 -12
- data/bin/console +1 -1
- data/config.ru +2 -2
- data/lib/Captain/errors.rb +24 -0
- data/lib/Captain/events_dispatcher.rb +43 -0
- data/lib/Captain/version.rb +1 -1
- data/lib/captain.rb +21 -0
- data/tests/captain_test.rb +47 -0
- data/tests/events_dispatcher_test.rb +17 -0
- data/tests/test.rb +2 -0
- data/tests/test_complex_event.rb +2 -0
- data/tests/test_event.rb +4 -0
- data/tests/test_helper.rb +10 -0
- metadata +13 -4
- data/lib/Captain.rb +0 -18
- data/lib/hooks_controller.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f10388eb371f3c610c51357d7f270974bb67d8a25af262cf1b897a2b78fe086
|
4
|
+
data.tar.gz: c784101ffa46242b42f475b35298f0dca4e022ff5f798f4c9aef0c636ef94a44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecdc0734368d616a79083a8b8ab6ced1cd13cbcecf6f1f3c1a2b558c6062e70dca980a43bebea24738100eec712049d0657962f25be0dfec62b21c9130ada277
|
7
|
+
data.tar.gz: d5c5c5304e056049495f42e169098a6daef774eb6b608cfcb79403198e4aef04b455f1fbbd3e2ae4ddf1a4dd4b02d57736861105d1d8c056208073a4671b222d
|
data/Captain-0.0.1.gem
ADDED
Binary file
|
data/Captain.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/captain/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "Captain"
|
7
|
+
spec.version = Captain::VERSION
|
8
|
+
spec.authors = ["Grégory Prève", "Antoine Barranco"]
|
9
|
+
spec.email = ["gregory@flatlooker.com", "antoine.barranco@flatlooker.com"]
|
10
|
+
|
11
|
+
spec.summary = "Gem to handle incoming hooks"
|
12
|
+
spec.description = "Gem to handle incoming hooks"
|
13
|
+
spec.homepage = "https://github.com/Flatlooker/Captain"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/Flatlooker/Captain"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/Flatlooker/Captain/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Uncomment to register a new dependency of your gem
|
33
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, checkout our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
data/Gemfile
CHANGED
@@ -5,16 +5,11 @@ source "https://rubygems.org"
|
|
5
5
|
# Specify your gem's dependencies in Captain.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem "
|
9
|
-
|
8
|
+
gem "json"
|
10
9
|
gem "minitest", "~> 5.0"
|
10
|
+
gem "rake", "~> 13.0"
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
gem '
|
15
|
-
|
16
|
-
gem 'sinatra'
|
17
|
-
|
18
|
-
gem 'puma'
|
19
|
-
|
20
|
-
gem 'ruby_jard'
|
12
|
+
group :development do
|
13
|
+
gem "rubocop", "~> 1.21"
|
14
|
+
gem 'ruby_jard'
|
15
|
+
end
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
Captain (0.0.
|
4
|
+
Captain (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -9,64 +9,51 @@ GEM
|
|
9
9
|
ast (2.4.2)
|
10
10
|
byebug (11.1.3)
|
11
11
|
coderay (1.1.3)
|
12
|
+
json (2.6.2)
|
12
13
|
method_source (1.0.0)
|
13
|
-
minitest (5.
|
14
|
-
mustermann (1.1.1)
|
15
|
-
ruby2_keywords (~> 0.0.1)
|
16
|
-
nio4r (2.5.8)
|
14
|
+
minitest (5.16.2)
|
17
15
|
parallel (1.22.1)
|
18
|
-
parser (3.
|
16
|
+
parser (3.1.2.0)
|
19
17
|
ast (~> 2.4.1)
|
20
18
|
pry (0.13.1)
|
21
19
|
coderay (~> 1.1)
|
22
20
|
method_source (~> 1.0)
|
23
|
-
puma (5.6.4)
|
24
|
-
nio4r (~> 2.0)
|
25
|
-
rack (2.2.3.1)
|
26
|
-
rack-protection (2.2.0)
|
27
|
-
rack
|
28
21
|
rainbow (3.1.1)
|
29
22
|
rake (13.0.6)
|
30
23
|
regexp_parser (2.5.0)
|
31
24
|
rexml (3.2.5)
|
32
|
-
rubocop (1.
|
25
|
+
rubocop (1.31.2)
|
26
|
+
json (~> 2.3)
|
33
27
|
parallel (~> 1.10)
|
34
|
-
parser (>= 3.
|
28
|
+
parser (>= 3.1.0.0)
|
35
29
|
rainbow (>= 2.2.2, < 4.0)
|
36
30
|
regexp_parser (>= 1.8, < 3.0)
|
37
|
-
rexml
|
38
|
-
rubocop-ast (>= 1.
|
31
|
+
rexml (>= 3.2.5, < 4.0)
|
32
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
39
33
|
ruby-progressbar (~> 1.7)
|
40
34
|
unicode-display_width (>= 1.4.0, < 3.0)
|
41
|
-
rubocop-ast (1.
|
42
|
-
parser (>= 3.
|
35
|
+
rubocop-ast (1.19.1)
|
36
|
+
parser (>= 3.1.1.0)
|
43
37
|
ruby-progressbar (1.11.0)
|
44
|
-
ruby2_keywords (0.0.5)
|
45
38
|
ruby_jard (0.3.1)
|
46
39
|
byebug (>= 9.1, < 12.0)
|
47
40
|
pry (~> 0.13.0)
|
48
41
|
tty-screen (~> 0.8.1)
|
49
|
-
sinatra (2.2.0)
|
50
|
-
mustermann (~> 1.0)
|
51
|
-
rack (~> 2.2)
|
52
|
-
rack-protection (= 2.2.0)
|
53
|
-
tilt (~> 2.0)
|
54
|
-
tilt (2.0.10)
|
55
42
|
tty-screen (0.8.1)
|
56
|
-
unicode-display_width (2.
|
43
|
+
unicode-display_width (2.2.0)
|
57
44
|
|
58
45
|
PLATFORMS
|
46
|
+
universal-darwin-21
|
47
|
+
x86_64-darwin-18
|
59
48
|
x86_64-darwin-20
|
60
49
|
|
61
50
|
DEPENDENCIES
|
62
51
|
Captain!
|
52
|
+
json
|
63
53
|
minitest (~> 5.0)
|
64
|
-
puma
|
65
|
-
rack
|
66
54
|
rake (~> 13.0)
|
67
55
|
rubocop (~> 1.21)
|
68
56
|
ruby_jard
|
69
|
-
sinatra
|
70
57
|
|
71
58
|
BUNDLED WITH
|
72
|
-
2.
|
59
|
+
2.3.18
|
data/README.md
CHANGED
@@ -43,21 +43,31 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
43
43
|
Everyone interacting in the Captain project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/Captain/blob/master/CODE_OF_CONDUCT.md).
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
|
47
|
-
require_relative "./lib/Captain.rb"
|
48
|
-
require_relative "./lib/hooks_controller.rb"
|
49
|
-
|
50
|
-
Captain.define_hook('test')
|
51
|
-
Captain.define_hook('yo')
|
52
|
-
|
46
|
+
# myapp.rb
|
53
47
|
require 'sinatra'
|
54
|
-
|
48
|
+
require 'ruby_jard'
|
49
|
+
require '../lib/captain'
|
50
|
+
|
51
|
+
class HookTest
|
52
|
+
def perform
|
53
|
+
{
|
54
|
+
status: 200,
|
55
|
+
body: { text: "Hello World!" }
|
56
|
+
}
|
57
|
+
end
|
55
58
|
|
56
|
-
|
57
|
-
|
58
|
-
HooksController.new.call(@env)
|
59
|
+
def save
|
60
|
+
puts 'the hook is save.'
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
62
|
-
|
64
|
+
hook_type = proc do |url, content|
|
65
|
+
HookTest
|
66
|
+
end
|
67
|
+
Captain.config[:url_root] = '/hooks'
|
68
|
+
Captain.define_hook('/test_event', hook_type)
|
69
|
+
|
70
|
+
get "/hooks/*" do
|
71
|
+
HooksController.new.call(@env, @params)
|
72
|
+
end
|
63
73
|
```
|
data/bin/console
CHANGED
data/config.ru
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Captain
|
4
|
+
class CaptainError < StandardError
|
5
|
+
attr_accessor :message
|
6
|
+
|
7
|
+
def initialize(message = nil)
|
8
|
+
@message = message
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class NoExistingHookType < MLClientError
|
14
|
+
end
|
15
|
+
|
16
|
+
class NoMethodPerform < MLClientError
|
17
|
+
end
|
18
|
+
|
19
|
+
class NoExistingHook < MLClientError
|
20
|
+
end
|
21
|
+
|
22
|
+
class TooManyMatchedHooksError < MLClientError
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Captain
|
4
|
+
class EventsDispatcher
|
5
|
+
def call(env, params)
|
6
|
+
matched_event = matched_events env['REQUEST_PATH']
|
7
|
+
|
8
|
+
format_response(status: 404, body: { response: "Unknown hook route ! (#{env['PATH_INFO']})" }) if matched_event.nil?
|
9
|
+
|
10
|
+
event = retrieve_event matched_event, params
|
11
|
+
event.save if event.respond_to?(:save)
|
12
|
+
format_response **event.perform
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def matched_events(url)
|
18
|
+
raise NoExistingEvent if Captain.events.size.zero?
|
19
|
+
|
20
|
+
matched_events = Captain.events.select do |event|
|
21
|
+
event[:url] == url
|
22
|
+
end
|
23
|
+
|
24
|
+
raise TooManyMatchedEventsError if matched_events.size > 1
|
25
|
+
|
26
|
+
matched_events.first
|
27
|
+
end
|
28
|
+
|
29
|
+
def retrieve_event(matched_event, params)
|
30
|
+
event_type = matched_event[:event_type]
|
31
|
+
case event_type.class
|
32
|
+
when Proc
|
33
|
+
event_type.call(matched_event[:url], params).new
|
34
|
+
when Class
|
35
|
+
event_type.new
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def format_response(status:, body:, format: 'application/json')
|
40
|
+
[status, { 'Content-Type' => format }, body.to_json]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/Captain/version.rb
CHANGED
data/lib/captain.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "captain/version"
|
4
|
+
require_relative "./captain/events_dispatcher"
|
5
|
+
|
6
|
+
module Captain
|
7
|
+
class << self
|
8
|
+
attr_accessor :events
|
9
|
+
attr_accessor :config
|
10
|
+
|
11
|
+
def define_hook(url, event_type)
|
12
|
+
raise NoMethodPerform if !event_type.nil? && event_type.respond_to?(:perform)
|
13
|
+
|
14
|
+
events << {url: "#{Captain.config[:url_root]}#{url}", event_type: event_type}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Captain.config = {}
|
19
|
+
Captain.config[:url_root] = '/hooks'
|
20
|
+
Captain.events = []
|
21
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "./tests/test_helper"
|
4
|
+
require "rack/test"
|
5
|
+
|
6
|
+
class CaptainTest < Minitest::Test
|
7
|
+
include Rack::Test::Methods
|
8
|
+
|
9
|
+
OUTER_APP = Rack::Builder.parse_file("./config.ru").first
|
10
|
+
|
11
|
+
def setup
|
12
|
+
Captain.define_hook('/test_route', Test::Event)
|
13
|
+
event_type = proc do |url, params|
|
14
|
+
params[:name]
|
15
|
+
end
|
16
|
+
Captain.define_hook('/test_complex_route', event_type)
|
17
|
+
end
|
18
|
+
|
19
|
+
def app
|
20
|
+
OUTER_APP
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_that_it_has_a_version_number
|
24
|
+
refute_nil ::Captain::VERSION
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_defined_route
|
28
|
+
get '/hooks/test_route'
|
29
|
+
puts last_response.body
|
30
|
+
assert_equal true, last_response.ok?
|
31
|
+
assert_equal 'Hello World!', last_response.body.force_encoding('UTF-8')
|
32
|
+
end
|
33
|
+
|
34
|
+
def tet_complex_route
|
35
|
+
get '/hooks/test_complex_route'
|
36
|
+
|
37
|
+
assert_equal true, last_response.ok?
|
38
|
+
assert_equal 'Hello World!', last_response.body.force_encoding('UTF-8')
|
39
|
+
end
|
40
|
+
|
41
|
+
def tet_undefined_route
|
42
|
+
get '/hooks/wrong_route'
|
43
|
+
|
44
|
+
assert_equal false, last_response.ok?
|
45
|
+
assert_equal "Unknown hook route ! (/hooks/wrong_route)", last_response.body.force_encoding('UTF-8')
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './test_helper'
|
4
|
+
|
5
|
+
class EventsDispatcherTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
Captain.define_hook('/test_route', 'Test::Event')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_call_returns_right_output
|
11
|
+
env = { 'PATH_INFO' => '/hooks/test_route' }
|
12
|
+
assert Captain::EventsDispatcher.new.call(env) == [200, {"Content-Type" => "text/html"}, ["Hello World!"]]
|
13
|
+
|
14
|
+
env = { 'PATH_INFO' => '/hooks/wrong_route' }
|
15
|
+
assert Captain::EventsDispatcher.new.call(env) == [404, {"Content-Type" => "text/html"}, ["Unknown hook route ! (/hooks/wrong_route)"]]
|
16
|
+
end
|
17
|
+
end
|
data/tests/test.rb
ADDED
data/tests/test_event.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
4
|
+
require_relative "../lib/captain"
|
5
|
+
require "./lib/captain/events_dispatcher"
|
6
|
+
require "./tests/test.rb"
|
7
|
+
require "./tests/test_event.rb"
|
8
|
+
require "./tests/test_complex_event.rb"
|
9
|
+
|
10
|
+
require "minitest/autorun"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Captain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grégory Prève
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2022-
|
12
|
+
date: 2022-08-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Gem to handle incoming hooks
|
15
15
|
email:
|
@@ -22,6 +22,8 @@ files:
|
|
22
22
|
- ".rubocop.yml"
|
23
23
|
- CHANGELOG.md
|
24
24
|
- CODE_OF_CONDUCT.md
|
25
|
+
- Captain-0.0.1.gem
|
26
|
+
- Captain.gemspec
|
25
27
|
- Gemfile
|
26
28
|
- Gemfile.lock
|
27
29
|
- LICENSE.txt
|
@@ -30,9 +32,16 @@ files:
|
|
30
32
|
- bin/console
|
31
33
|
- bin/setup
|
32
34
|
- config.ru
|
33
|
-
- lib/Captain.rb
|
35
|
+
- lib/Captain/errors.rb
|
36
|
+
- lib/Captain/events_dispatcher.rb
|
34
37
|
- lib/Captain/version.rb
|
35
|
-
- lib/
|
38
|
+
- lib/captain.rb
|
39
|
+
- tests/captain_test.rb
|
40
|
+
- tests/events_dispatcher_test.rb
|
41
|
+
- tests/test.rb
|
42
|
+
- tests/test_complex_event.rb
|
43
|
+
- tests/test_event.rb
|
44
|
+
- tests/test_helper.rb
|
36
45
|
homepage: https://github.com/Flatlooker/Captain
|
37
46
|
licenses:
|
38
47
|
- MIT
|
data/lib/Captain.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "Captain/version"
|
4
|
-
require_relative "hooks_controller"
|
5
|
-
require 'rack'
|
6
|
-
|
7
|
-
module Captain
|
8
|
-
class << self
|
9
|
-
attr_accessor :config
|
10
|
-
|
11
|
-
def define_hook(url)
|
12
|
-
config[:hooks] ||= []
|
13
|
-
config[:hooks] << url
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
Captain.config = {}
|
18
|
-
end
|
data/lib/hooks_controller.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'rack'
|
2
|
-
|
3
|
-
class HooksController
|
4
|
-
def call(env)
|
5
|
-
Captain.config[:hooks].each do |hook_path|
|
6
|
-
return [200, {"Content-Type" => "text/html"}, ["Hello World!"]] if "/hooks/#{hook_path}" == env['PATH_INFO']
|
7
|
-
end
|
8
|
-
|
9
|
-
[404, {"Content-Type" => "text/html"}, ["Unknown hook route !"]]
|
10
|
-
end
|
11
|
-
end
|