micropub-server-rails 0.1.5 → 0.1.6
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/lib/micropub/homesteading/router.rb +15 -0
- data/lib/micropub/server/rails/middleware.rb +39 -18
- data/lib/micropub/server/rails/version.rb +1 -1
- data/lib/micropub/server/rails.rb +1 -0
- data/micropub-server-rails.gemspec +2 -1
- data/spec/lib/micropub/homesteading/router_spec.rb +15 -0
- data/spec/lib/micropub/server/rails/middleware_spec.rb +37 -4
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fc8a92467ed79dba67eb9424cedaaefd85ce092
|
4
|
+
data.tar.gz: a96aad15017c7aafbee089e8086faebdeb7e745e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad32670e1f9ac91e4de8e2254617624bde56a05226c63df2d9b46dcd9093f64b12466cf0a901c9ce81ffbe1b5f92b11aae4daa819d0459607f39f2e4ac71f550
|
7
|
+
data.tar.gz: 1d53540d13575eba42cf6efe68bdc6874d1f06aab6d1cfd42af39401d81aa8cb9a16fec220d5552c2c488815bc63f9484a9a9a1b7c6179097377f16cdf844e12
|
@@ -2,45 +2,66 @@ require 'micropub'
|
|
2
2
|
|
3
3
|
module Micropub::Server::Rails
|
4
4
|
class Middleware
|
5
|
+
class HTTPError < StandardError
|
6
|
+
def initialize(env, status, body="")
|
7
|
+
@env = env
|
8
|
+
@status = status
|
9
|
+
@body = body
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
def initialize(app)
|
6
14
|
@app = app
|
7
15
|
end
|
8
16
|
|
9
17
|
def call(env)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
18
|
+
begin
|
19
|
+
@params = request(env).params
|
20
|
+
@headers = {"Content-Type" => "text/plain; charset=utf-8"}
|
21
|
+
if env["PATH_INFO"] == "/micropub"
|
22
|
+
check_token(env)
|
23
|
+
micropub_call env
|
24
|
+
else
|
25
|
+
@app.call env
|
26
|
+
end
|
27
|
+
rescue
|
28
|
+
error_response(401)
|
15
29
|
end
|
16
30
|
end
|
17
31
|
|
32
|
+
def check_token(env)
|
33
|
+
token = Micropub::Token.new(auth_token(env))
|
34
|
+
raise HTTPError.new env, 401 if !token.valid?
|
35
|
+
end
|
36
|
+
|
18
37
|
def auth_token(env)
|
19
|
-
input = env["HTTP_AUTHORIZATION"] ||
|
38
|
+
input = env["HTTP_AUTHORIZATION"] || @params["access_token"] || ""
|
20
39
|
input.split("Bearer ").last
|
21
40
|
end
|
22
41
|
|
23
|
-
def
|
24
|
-
|
42
|
+
def micropub_call(env)
|
43
|
+
router = Micropub::Homesteading::Router.new(@params)
|
44
|
+
client = Micropub::Client.new(router.as, token: auth_token(env))
|
45
|
+
response = client.post(@params.except("access_token"))
|
46
|
+
|
47
|
+
raise HTTPError.new env, response.status if !response.successful?
|
48
|
+
|
49
|
+
@headers["Location"] = response.location
|
50
|
+
|
51
|
+
success_response
|
25
52
|
end
|
26
53
|
|
27
|
-
def
|
28
|
-
[
|
54
|
+
def success_response
|
55
|
+
[201, @headers, [""]]
|
29
56
|
end
|
30
57
|
|
31
|
-
def
|
32
|
-
[""]
|
58
|
+
def error_response(status)
|
59
|
+
[status, {}, [""]]
|
33
60
|
end
|
34
61
|
|
35
62
|
def request(env)
|
36
63
|
Rack::Request.new(env)
|
37
64
|
end
|
38
65
|
|
39
|
-
def headers
|
40
|
-
{
|
41
|
-
"Location" => "http://bookisworthy.com/posts/1",
|
42
|
-
"Content-Type" => "text/plain; charset=utf-8"
|
43
|
-
}
|
44
|
-
end
|
45
66
|
end
|
46
67
|
end
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
24
|
spec.add_development_dependency "rack-test"
|
25
|
+
spec.add_development_dependency "webmock"
|
25
26
|
|
26
|
-
spec.add_dependency "micropub", "~> 0.0.
|
27
|
+
spec.add_dependency "micropub", "~> 0.0.6"
|
27
28
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "micropub/homesteading/router"
|
2
|
+
|
3
|
+
describe Micropub::Homesteading::Router do
|
4
|
+
describe "#as" do
|
5
|
+
it "uses the as attr to set as" do
|
6
|
+
router = Micropub::Homesteading::Router.new("as" => "note")
|
7
|
+
expect(router.as).to eq "note"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "can use h to convert the type" do
|
11
|
+
router = Micropub::Homesteading::Router.new("h" => "entry")
|
12
|
+
expect(router.as).to eq "note"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require "micropub/server/rails/middleware"
|
2
|
+
require 'webmock/rspec'
|
2
3
|
require "rack/test"
|
3
4
|
|
5
|
+
# How do we determine what host to hit?
|
6
|
+
# Where does the code go which tells the mapping how to notes to work?
|
7
|
+
|
4
8
|
Micropub.configure do |c|
|
5
9
|
c.token_endpoint = "https://tokens.indieauth.com/token"
|
6
10
|
c.me = "http://bookisworthy.com"
|
@@ -25,21 +29,50 @@ describe Micropub::Server::Rails::Middleware do
|
|
25
29
|
before do
|
26
30
|
expect(Micropub::Token).to receive(:new).with("1234").and_return(token)
|
27
31
|
end
|
32
|
+
|
33
|
+
context "when publisher returns 401" do
|
34
|
+
let(:request) { post "/micropub", {content: "Blah Blah", "as" => "note"}, "HTTP_AUTHORIZATION" => "Bearer 1234" }
|
35
|
+
let(:token) { double("Token", valid?: true) }
|
36
|
+
|
37
|
+
before do
|
38
|
+
stub_request(:post, "http://bookisworthy.com/notes").with(
|
39
|
+
body: {content: "Blah Blah", as: "note"},
|
40
|
+
headers: { 'Authorization' => "Bearer 1234" })
|
41
|
+
.to_return(status: 401, body: "", headers: {})
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns a 401" do
|
45
|
+
request
|
46
|
+
expect(last_response.status).to eq 401
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
28
50
|
context "when given a valid token" do
|
29
51
|
let(:token) { double("Token", valid?: true) }
|
30
52
|
|
53
|
+
# params "as" stands for "activity stream"
|
54
|
+
# this is the experimental value to indicate the post type
|
55
|
+
let(:request) { post "/micropub", {content: "Blah Blah", "as" => "note"}, "HTTP_AUTHORIZATION" => "Bearer 1234" }
|
56
|
+
|
57
|
+
before do
|
58
|
+
stub_request(:post, "http://bookisworthy.com/notes").with(
|
59
|
+
body: {content: "Blah Blah", as: "note"},
|
60
|
+
headers: { 'Authorization' => "Bearer 1234" })
|
61
|
+
.to_return(status: 201, body: "http://bookisworthy.com/notes/1", headers: {"Location" => "http://bookisworthy.com/notes/1"})
|
62
|
+
end
|
63
|
+
|
31
64
|
it "returns a 201" do
|
32
|
-
|
65
|
+
request
|
33
66
|
expect(last_response.status).to eq 201
|
34
67
|
end
|
35
68
|
|
36
69
|
it "returns a Location header" do
|
37
|
-
|
38
|
-
expect(last_response.header["Location"]).to eq "http://bookisworthy.com/
|
70
|
+
request
|
71
|
+
expect(last_response.header["Location"]).to eq "http://bookisworthy.com/notes/1"
|
39
72
|
end
|
40
73
|
|
41
74
|
it "with params access_token returns a 201" do
|
42
|
-
post "/micropub", {access_token: 1234}
|
75
|
+
post "/micropub", {content: "Blah Blah", access_token: 1234, as: "note"}
|
43
76
|
expect(last_response.status).to eq 201
|
44
77
|
end
|
45
78
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micropub-server-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Becker
|
@@ -67,20 +67,34 @@ dependencies:
|
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: webmock
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
70
84
|
- !ruby/object:Gem::Dependency
|
71
85
|
name: micropub
|
72
86
|
requirement: !ruby/object:Gem::Requirement
|
73
87
|
requirements:
|
74
88
|
- - "~>"
|
75
89
|
- !ruby/object:Gem::Version
|
76
|
-
version: 0.0.
|
90
|
+
version: 0.0.6
|
77
91
|
type: :runtime
|
78
92
|
prerelease: false
|
79
93
|
version_requirements: !ruby/object:Gem::Requirement
|
80
94
|
requirements:
|
81
95
|
- - "~>"
|
82
96
|
- !ruby/object:Gem::Version
|
83
|
-
version: 0.0.
|
97
|
+
version: 0.0.6
|
84
98
|
description: Micropub Railtie
|
85
99
|
email:
|
86
100
|
- veganstraightedge@gmail.com
|
@@ -99,11 +113,13 @@ files:
|
|
99
113
|
- Rakefile
|
100
114
|
- bin/console
|
101
115
|
- bin/setup
|
116
|
+
- lib/micropub/homesteading/router.rb
|
102
117
|
- lib/micropub/server/rails.rb
|
103
118
|
- lib/micropub/server/rails/middleware.rb
|
104
119
|
- lib/micropub/server/rails/railtie.rb
|
105
120
|
- lib/micropub/server/rails/version.rb
|
106
121
|
- micropub-server-rails.gemspec
|
122
|
+
- spec/lib/micropub/homesteading/router_spec.rb
|
107
123
|
- spec/lib/micropub/server/rails/middleware_spec.rb
|
108
124
|
homepage:
|
109
125
|
licenses:
|
@@ -130,4 +146,5 @@ signing_key:
|
|
130
146
|
specification_version: 4
|
131
147
|
summary: Micropub Railtie
|
132
148
|
test_files:
|
149
|
+
- spec/lib/micropub/homesteading/router_spec.rb
|
133
150
|
- spec/lib/micropub/server/rails/middleware_spec.rb
|