social_web-activity_pub 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 591532051e24cf902211a715e87b3f3a9ff38a17349e8653cbf222a7c3260b95
4
+ data.tar.gz: 4acf018b31b488fdb388e0ad73cf59436e9ae42464cda74b809ca6d96b6a4113
5
+ SHA512:
6
+ metadata.gz: 85c524a4b2447ca8b13ae8edc90bfd253333773c45052b973009a02ee8e4e691d6a9b379c5016e26fe8b8d90613e16a9446fbe2aeaadcf016690a6d39f86a828
7
+ data.tar.gz: e167782f900d16b44cb41c6723cc01438e15b0e5d77cbf8c8388783d79c989daad3faae87193f3af619e2e707ffe1f58d923ea60db0cdcaf36016a9f6104ef44
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Shane Cavanaugh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'roda'
6
+
7
+ require 'activity_pub/exceptions'
8
+ require 'activity_pub/configuration'
9
+ require 'activity_pub/hooks'
10
+ require 'activity_pub/routes'
11
+
12
+ module ActivityPub; end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActivityPub
4
+ def self.configure
5
+ yield config
6
+ end
7
+
8
+ def self.config
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ class Configuration; end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActivityPub
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActivityPub
4
+ HOOKS = %w[
5
+ activity_pub.inbox.before_post
6
+ activity_pub.inbox.after_post
7
+ activity_pub.well_known.webfinger.before_get
8
+ activity_pub.well_known.webfinger.after_get
9
+ ].freeze
10
+
11
+ module Hooks
12
+ class FailedHook < Error
13
+ MESSAGE_FORMAT = <<~TXT
14
+ %<hook_name>s: %<hook_source_location>s
15
+
16
+ %<exception_class>s: %<exception_message>s
17
+ TXT
18
+ attr_accessor :hook_name, :hook_source_location, :wrapped_exception
19
+
20
+ def initialize(hook_name, hook_source_location, wrapped_exception)
21
+ @hook_name = hook_name
22
+ @hook_source_location = hook_source_location
23
+ @wrapped_exception = wrapped_exception
24
+ super(format_message)
25
+ end
26
+
27
+ def cause
28
+ wrapped_exception || super
29
+ end
30
+
31
+ private
32
+
33
+ def format_message
34
+ format(
35
+ MESSAGE_FORMAT,
36
+ hook_name: hook_name,
37
+ exception_class: wrapped_exception.class,
38
+ exception_message: wrapped_exception.message,
39
+ hook_source_location: hook_source_location.join(':')
40
+ )
41
+ end
42
+ end
43
+
44
+ def self.register(name, &blk)
45
+ Registry.instance.register(name, &blk)
46
+ end
47
+
48
+ def self.run(name, *args, **kwargs)
49
+ hooks = Registry.instance[name]
50
+ hooks&.each do |hook|
51
+ hook.call(*args, **kwargs)
52
+ rescue StandardError => e
53
+ raise FailedHook.new(name, hook.source_location, e)
54
+ end
55
+ end
56
+
57
+ class Registry
58
+ attr_accessor :hooks
59
+
60
+ def self.instance
61
+ @instance ||= new
62
+ end
63
+
64
+ def initialize
65
+ @hooks = {}
66
+ end
67
+
68
+ def [](name)
69
+ @hooks[name]
70
+ end
71
+
72
+ def register(name, &action)
73
+ @hooks[name] ||= []
74
+ @hooks[name] << action.to_proc
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActivityPub
4
+ class Routes < ::Roda
5
+ plugin :response_request
6
+ plugin :halt
7
+ plugin :hash_routes
8
+ plugin :head
9
+ plugin :hooks
10
+ plugin :json
11
+ plugin :json_parser
12
+ plugin :middleware, env_var: 'social_web.activity_pub'
13
+
14
+ %w[
15
+ inbox
16
+ ].each { |route| require File.join('activity_pub', 'routes', route)}
17
+
18
+ route { |r| r.hash_branches }
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActivityPub
4
+ Routes.hash_branch('inbox') do |r|
5
+ r.post do
6
+ Hooks.run(
7
+ 'activity_pub.inbox.before_post',
8
+ Rack::Request.new(r.env)
9
+ )
10
+
11
+ body = r.body.read
12
+ JSON.parse(body)
13
+
14
+ response.status = 201
15
+ ''
16
+ rescue JSON::ParserError
17
+ r.halt 400
18
+ ensure
19
+ Hooks.run(
20
+ 'activity_pub.inbox.after_post',
21
+ Rack::Response.new(response.finish)
22
+ )
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module ActivityPub
6
+ class Routes
7
+ RSpec.describe '/inbox', type: :route do
8
+ context 'POST /inbox' do
9
+ context 'when the request has an empty body' do
10
+ it 'returns a 400 response' do
11
+ post '/inbox'
12
+ expect(last_response.status).to eq(400)
13
+ end
14
+ end
15
+
16
+ context 'when the body is not valid JSON' do
17
+ it 'returns a 400 response' do
18
+ post '/inbox', 'beepboop'
19
+ expect(last_response.status).to eq(400)
20
+ end
21
+ end
22
+
23
+ context 'when body is valid JSON' do
24
+ it 'creates a record and returns a 201 status' do
25
+ post '/inbox', '{}'
26
+ expect(last_response.status).to eq(201)
27
+ end
28
+ end
29
+
30
+ it 'runs hooks before and after the request' do
31
+ expect(Hooks).
32
+ to receive(:run).
33
+ ordered.
34
+ with('activity_pub.inbox.before_post', kind_of(Rack::Request))
35
+ expect(Hooks).
36
+ to receive(:run).
37
+ ordered.
38
+ with('activity_pub.inbox.after_post', kind_of(Rack::Response))
39
+ post '/inbox', '{}'
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe ActivityPub do
6
+ describe '.confgure' do
7
+ it 'yields the configuration' do
8
+ expect(described_class.configure(&:itself)).
9
+ to be_a(ActivityPub::Configuration)
10
+
11
+
12
+ end
13
+ end
14
+
15
+ describe '.configuration' do
16
+ it 'returns configuration' do
17
+ expect(described_class.config).to be_a(ActivityPub::Configuration)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helper
4
+ def app
5
+ builder = Rack::Builder.new
6
+ builder.run ActivityPub::Routes
7
+ end
8
+
9
+ def configure
10
+ mock_config = instance_double('ActivityPub::Configuration')
11
+ allow(ActivityPub).to receive(:config).and_return(mock_config)
12
+ yield mock_config
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+ SimpleCov.add_filter(/spec/)
5
+ SimpleCov.start
6
+
7
+ require 'activity_pub'
8
+ require 'rack/test'
9
+ require 'helper'
10
+
11
+ RSpec.configure do |config|
12
+ config.include Rack::Test::Methods, type: :route
13
+ config.include Helper
14
+
15
+ config.expect_with :rspec do |expectations|
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ config.mock_with :rspec do |mocks|
20
+ mocks.verify_partial_doubles = true
21
+ end
22
+
23
+ config.shared_context_metadata_behavior = :apply_to_host_groups
24
+
25
+ config.example_status_persistence_file_path = 'tmp/examples.txt'
26
+
27
+ config.disable_monkey_patching!
28
+
29
+ config.warnings = false
30
+ config.default_formatter = 'doc' if config.files_to_run.one?
31
+
32
+ # Run specs in random order to surface order dependencies. If you find an
33
+ # order dependency and want to debug it, you can fix the order by providing
34
+ # the seed, which is printed after each run.
35
+ # --seed 1234
36
+ config.order = :random
37
+
38
+ # Seed global randomization in this process using the `--seed` CLI option.
39
+ # Setting this allows you to use `--seed` to deterministically reproduce
40
+ # test failures related to randomization by passing the same `--seed` value
41
+ # as the one that triggered the failure.
42
+ Kernel.srand config.seed
43
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_web-activity_pub
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Shane Cavanaugh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: roda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ description: Rack app for ActivityPub endpoints
84
+ email:
85
+ - shane@shanecav.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - lib/activity_pub.rb
92
+ - lib/activity_pub/configuration.rb
93
+ - lib/activity_pub/exceptions.rb
94
+ - lib/activity_pub/hooks.rb
95
+ - lib/activity_pub/routes.rb
96
+ - lib/activity_pub/routes/inbox.rb
97
+ - spec/activity_pub/routes/inbox_spec.rb
98
+ - spec/activity_pub_spec.rb
99
+ - spec/helper.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/shanecav84/social_web/activity_pub
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ homepage_uri: https://github.com/shanecav84/social_web/activity_pub
106
+ source_code_uri: https://github.com/shanecav84/social_web/activity_pub
107
+ changelog_uri: https://github.com/shanecav84/social_web/activity_pub/tree/master/CHANGELOG.md
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 1.9.2
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubygems_version: 3.0.4
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rack app for ActivityPub endpoints
127
+ test_files: []