forked from YOU54F/pact-ruby-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.rb
44 lines (36 loc) · 1.06 KB
/
provider.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'webrick'
require 'json'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: server.rb [options]'
opts.on('-p', '--port PORT', Integer, 'Port number to listen on (default: 8000)') do |port|
options[:port] = port
end
end.parse!
port = options[:port] || 8000
server = WEBrick::HTTPServer.new(Port: port)
server.mount_proc '/api/books' do |req, res|
if req.request_method == 'POST'
res.status = 201
res['Content-Type'] = 'application/ld+json;charset=utf-8'
res.body = JSON.generate({
"author": 'Margaret Atwood',
"description": 'Brilliantly',
"isbn": '0099740915',
"publicationDate": '1985-07-31T00:00:00+00:00',
"title": "The Handmaid's Tale",
"@type": 'Book',
"@id": '/api/books/0114b2a8-3347-49d8-ad99-0e792c5a30e6',
"reviews": [],
"@context": '/api/contexts/Book'
})
else
res.status = 404
res['Content-Type'] = 'text/plain'
res.body = 'Not found'
end
end
trap('INT') { server.shutdown }
server.start
puts "Server listening on port #{port}"