blest 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -95
- data/lib/blest.rb +808 -143
- data/spec/blest_spec.rb +219 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6307985d2fcaa8aa558a9d6fa55bd61787de23accd0d762d12843cf1202784c8
|
4
|
+
data.tar.gz: b58fc887beb5b6312f14f7332daab521b37baa037750eb3b1b406191160ae0d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f0b5c80b2ae39259d5386c60a7a27deb6b04dc35a5162c30be64056d10581de3b6a6ee47cd800e27f4e4cac1f52a6cb692b47abbda7533ed48dc85c3eafe403
|
7
|
+
data.tar.gz: 964ab1acc3ca2db12a2918bb141efa3f254438c095b47c7e81313c4b5140d7a10b13712b3a4773e6fa1b76158ccc75dffa2af591df45fdb55a767278ea72e470
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
The Ruby reference implementation of BLEST (Batch-able, Lightweight, Encrypted State Transfer), an improved communication protocol for web APIs which leverages JSON, supports request batching and selective returns, and provides a modern alternative to REST.
|
4
4
|
|
5
|
-
To learn more about BLEST, please
|
5
|
+
To learn more about BLEST, please visit the website: https://blest.jhunt.dev
|
6
6
|
|
7
7
|
For a front-end implementation in React, please visit https://github.com/jhuntdev/blest-react
|
8
8
|
|
@@ -25,139 +25,101 @@ gem install blest
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
<!-- Use the `create_http_client` function to create a BLEST HTTP client. -->
|
31
|
-
|
32
|
-
### create_request_handler
|
28
|
+
This core class of this library has an interface somewhat similar to Sinatra. It also provides a `Router` class with a `handle` method for use in an existing Ruby API and an `HttpClient` class with a `request` method for making BLEST HTTP requests.
|
33
29
|
|
34
30
|
```ruby
|
35
|
-
require 'webrick'
|
36
|
-
require 'json'
|
37
31
|
require 'blest'
|
38
32
|
|
33
|
+
app = Blest.new(timeout: 1000, port: 8080, host: 'localhost', cors: 'http://localhost:3000')
|
34
|
+
|
39
35
|
# Create some middleware (optional)
|
40
|
-
|
41
|
-
if params[
|
42
|
-
context[
|
43
|
-
name: params[
|
36
|
+
app.before do |params, context|
|
37
|
+
if params['name'].present?
|
38
|
+
context['user'] = {
|
39
|
+
name: params['name']
|
44
40
|
}
|
45
41
|
nil
|
46
42
|
else
|
47
43
|
raise RuntimeError, "Unauthorized"
|
48
44
|
end
|
49
|
-
|
45
|
+
end
|
50
46
|
|
51
47
|
# Create a route controller
|
52
|
-
|
48
|
+
app.route('greet') do |params, context|
|
53
49
|
{
|
54
|
-
greeting: "Hi, #{context[
|
50
|
+
greeting: "Hi, #{context['user']['name']}!"
|
55
51
|
}
|
56
|
-
}
|
57
|
-
|
58
|
-
# Create a router
|
59
|
-
router = {
|
60
|
-
greet: [auth_middleware, greet_controller]
|
61
|
-
}
|
62
|
-
|
63
|
-
# Create a request handler
|
64
|
-
handler = create_request_handler(router)
|
65
|
-
|
66
|
-
class HttpRequestHandler < WEBrick::HTTPServlet::AbstractServlet
|
67
|
-
def do_OPTIONS(request, response)
|
68
|
-
response.status = 200
|
69
|
-
response['Access-Control-Allow-Origin'] = '*'
|
70
|
-
response['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
|
71
|
-
response['Access-Control-Allow-Headers'] = 'Content-Type'
|
72
|
-
end
|
73
|
-
|
74
|
-
def do_POST(request, response)
|
75
|
-
response['Content-Type'] = 'application/json'
|
76
|
-
response['Access-Control-Allow-Origin'] = '*'
|
77
|
-
|
78
|
-
# Parse JSON body
|
79
|
-
begin
|
80
|
-
payload = JSON.parse(request.body)
|
81
|
-
|
82
|
-
# Define the request context
|
83
|
-
context = {
|
84
|
-
headers: request.headers
|
85
|
-
}
|
86
|
-
|
87
|
-
# Use the request handler
|
88
|
-
result, error = handler.(payload, context)
|
89
|
-
|
90
|
-
# Do something with the result or error
|
91
|
-
if error
|
92
|
-
response_body = error.to_json
|
93
|
-
response.status = 500
|
94
|
-
response.body = response_body
|
95
|
-
elsif result
|
96
|
-
response_body = result.to_json
|
97
|
-
response.status = 200
|
98
|
-
response.body = response_body
|
99
|
-
else
|
100
|
-
response.status = 204
|
101
|
-
end
|
102
|
-
|
103
|
-
rescue JSON::ParserError
|
104
|
-
response.status = 400
|
105
|
-
response.body = { message: 'Invalid JSON' }.to_json
|
106
|
-
end
|
107
|
-
end
|
108
52
|
end
|
109
53
|
|
110
|
-
# Create WEBrick server
|
111
|
-
server = WEBrick::HTTPServer.new(Port: 8000)
|
112
|
-
|
113
|
-
# Mount custom request handler
|
114
|
-
server.mount('/', HttpRequestHandler)
|
115
|
-
|
116
|
-
trap('INT') { server.shutdown }
|
117
|
-
|
118
54
|
# Start the server
|
119
|
-
|
55
|
+
app.listen
|
120
56
|
```
|
121
57
|
|
122
|
-
###
|
58
|
+
### Router
|
59
|
+
|
60
|
+
The following example uses Sinatra.
|
123
61
|
|
124
62
|
```ruby
|
63
|
+
require 'sinatra'
|
64
|
+
require 'json'
|
125
65
|
require 'blest'
|
126
66
|
|
67
|
+
# Instantiate the Router
|
68
|
+
router = Router.new(timeout: 1000)
|
69
|
+
|
127
70
|
# Create some middleware (optional)
|
128
|
-
|
129
|
-
if params[
|
130
|
-
context[
|
131
|
-
name: params[
|
71
|
+
router.before do |params, context|
|
72
|
+
if params['name'].present?
|
73
|
+
context['user'] = {
|
74
|
+
name: params['name']
|
132
75
|
}
|
133
76
|
nil
|
134
77
|
else
|
135
78
|
raise RuntimeError, "Unauthorized"
|
136
79
|
end
|
137
|
-
|
80
|
+
end
|
138
81
|
|
139
82
|
# Create a route controller
|
140
|
-
|
83
|
+
router.route('greet') do |params, context|
|
141
84
|
{
|
142
|
-
greeting: "Hi, #{context[
|
85
|
+
greeting: "Hi, #{context['user']['name']}!"
|
143
86
|
}
|
144
|
-
|
87
|
+
end
|
145
88
|
|
146
|
-
#
|
147
|
-
|
148
|
-
|
149
|
-
}
|
89
|
+
# Handle BLEST requests
|
90
|
+
post '/' do
|
91
|
+
json_body = JSON.parse(request.body.read)
|
92
|
+
headers = request.env.select { |k, _| k.start_with?('HTTP_') }
|
93
|
+
result, error = router.handle.call(json_body, { 'headers' => headers })
|
94
|
+
content_type :json
|
95
|
+
if error
|
96
|
+
raise Sinatra::Error.new(error.status || 500, error)
|
97
|
+
else
|
98
|
+
result
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
150
102
|
|
151
|
-
|
152
|
-
handler = create_request_handler(router)
|
103
|
+
### HttpClient
|
153
104
|
|
154
|
-
|
155
|
-
|
105
|
+
```ruby
|
106
|
+
require 'blest'
|
156
107
|
|
157
|
-
#
|
158
|
-
|
108
|
+
# Create a client
|
109
|
+
client = HttpClient.new('http://localhost:8080', max_batch_size = 25, buffer_delay = 10, headers = {
|
110
|
+
'Authorization': 'Bearer token'
|
111
|
+
})
|
112
|
+
|
113
|
+
# Send a request
|
114
|
+
begin
|
115
|
+
result = client.request('greet', { 'name': 'Steve' }, ['greeting']).value
|
116
|
+
# Do something with the result
|
117
|
+
rescue => error
|
118
|
+
# Do something in case of error
|
119
|
+
end
|
159
120
|
```
|
160
121
|
|
122
|
+
|
161
123
|
## License
|
162
124
|
|
163
125
|
This project is licensed under the [MIT License](LICENSE).
|