knod 0.8.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -4
- data/Gemfile.lock +6 -1
- data/knod.gemspec +4 -3
- data/lib/knod/file_utilities.rb +1 -1
- data/lib/knod/server.rb +23 -23
- data/lib/knod/version.rb +1 -1
- data/test/connection.rb +11 -11
- data/test/test_knod.rb +22 -43
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdbb4e14fe3660fb4bb030db3d9aa1f866375f68
|
4
|
+
data.tar.gz: c7bdf61f293efebf3088d693ff5cedb20299af3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f167fd7a82546afa06f6e866ac5b3f2be6932e9c3fa24ad088c86decb65af88dad1de86496565e24f09e03261cad79899fcf2e43f40326993ac6bcb7b0e2b074
|
7
|
+
data.tar.gz: b6aa207913a06d2d66f8e94259dee7ca4a03c48d1c4c04cdc70e105b74563868caebde8d81805198adcb2562b9e5886c2fba7b7d339f82d6c08df1640cded5ac
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
knod (0.
|
4
|
+
knod (1.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
minitest (5.8.4)
|
9
10
|
rake (10.3.1)
|
10
11
|
|
11
12
|
PLATFORMS
|
@@ -13,4 +14,8 @@ PLATFORMS
|
|
13
14
|
|
14
15
|
DEPENDENCIES
|
15
16
|
knod!
|
17
|
+
minitest (~> 5)
|
16
18
|
rake (~> 10)
|
19
|
+
|
20
|
+
BUNDLED WITH
|
21
|
+
1.11.2
|
data/knod.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'knod/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = 'knod'
|
8
8
|
gem.version = Knod::VERSION
|
9
|
-
gem.date = '
|
9
|
+
gem.date = '2016-09-11'
|
10
10
|
gem.authors = ['Ryan Moser']
|
11
11
|
gem.email = 'ryanpmoser@gmail.com'
|
12
12
|
gem.homepage = 'https://github.com/moserrya/knod'
|
@@ -19,7 +19,8 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
20
|
gem.require_paths = ['lib']
|
21
21
|
|
22
|
-
gem.required_ruby_version = '>= 2.
|
22
|
+
gem.required_ruby_version = '>= 2.1'
|
23
23
|
|
24
|
-
gem.add_development_dependency
|
24
|
+
gem.add_development_dependency 'rake', '~> 10'
|
25
|
+
gem.add_development_dependency 'minitest', '~> 5'
|
25
26
|
end
|
data/lib/knod/file_utilities.rb
CHANGED
data/lib/knod/server.rb
CHANGED
@@ -2,7 +2,7 @@ module Knod
|
|
2
2
|
class Server
|
3
3
|
include FileUtilities
|
4
4
|
|
5
|
-
attr_reader :server, :
|
5
|
+
attr_reader :server, :client, :request
|
6
6
|
|
7
7
|
DEFAULT_PORT = 4444
|
8
8
|
DEFAULT_WEB_ROOT = './'
|
@@ -17,13 +17,15 @@ module Knod
|
|
17
17
|
def start
|
18
18
|
log "Starting server on port #{port}"
|
19
19
|
loop do
|
20
|
-
|
20
|
+
Thread.start(server.accept) do |client|
|
21
|
+
dup.accept_request_and_respond(client)
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
def accept_request_and_respond
|
25
|
-
@
|
26
|
-
@request = Request.new(
|
26
|
+
def accept_request_and_respond(client)
|
27
|
+
@client = client
|
28
|
+
@request = Request.new(client)
|
27
29
|
log request_line
|
28
30
|
public_send "do_#{request.method}"
|
29
31
|
rescue => e
|
@@ -31,7 +33,7 @@ module Knod
|
|
31
33
|
log e.backtrace
|
32
34
|
respond 500
|
33
35
|
ensure
|
34
|
-
|
36
|
+
client.close if client
|
35
37
|
end
|
36
38
|
|
37
39
|
def do_GET(head = false)
|
@@ -39,8 +41,8 @@ module Knod
|
|
39
41
|
|
40
42
|
if file?(path)
|
41
43
|
File.open(path, 'rb') do |file|
|
42
|
-
|
43
|
-
IO.copy_stream(file,
|
44
|
+
client.print file_response_header(file)
|
45
|
+
IO.copy_stream(file, client) unless head
|
44
46
|
end
|
45
47
|
elsif directory?(path)
|
46
48
|
respond(200, concat_json(path))
|
@@ -65,19 +67,17 @@ module Knod
|
|
65
67
|
respond(200, "\"Success\"")
|
66
68
|
end
|
67
69
|
|
68
|
-
|
69
|
-
using HashWithPatchMerge
|
70
|
+
using HashWithPatchMerge
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
72
|
+
def do_PATCH
|
73
|
+
path = requested_path
|
74
|
+
data = if file?(path)
|
75
|
+
merge_json(read_file(path), request.body)
|
76
|
+
else
|
77
|
+
request.body
|
78
|
+
end
|
79
|
+
write_to_path(path, data)
|
80
|
+
respond(200, "\"Success\"")
|
81
81
|
end
|
82
82
|
|
83
83
|
def do_POST
|
@@ -137,8 +137,8 @@ module Knod
|
|
137
137
|
end
|
138
138
|
|
139
139
|
def respond(status_code, message = '')
|
140
|
-
|
141
|
-
|
140
|
+
client.print response_header(status_code, message)
|
141
|
+
client.print message unless message.empty?
|
142
142
|
end
|
143
143
|
|
144
144
|
def file_response_header(file)
|
@@ -165,7 +165,7 @@ module Knod
|
|
165
165
|
|
166
166
|
def content_type(path)
|
167
167
|
ext = file_extension(path)
|
168
|
-
CONTENT_TYPE_MAPPING
|
168
|
+
CONTENT_TYPE_MAPPING.fetch(ext) { DEFAULT_CONTENT_TYPE }
|
169
169
|
end
|
170
170
|
|
171
171
|
def requested_path
|
data/lib/knod/version.rb
CHANGED
data/test/connection.rb
CHANGED
@@ -9,16 +9,16 @@ class Connection
|
|
9
9
|
end
|
10
10
|
|
11
11
|
VERB_MAP = {
|
12
|
-
get:
|
13
|
-
post:
|
14
|
-
put:
|
15
|
-
patch:
|
16
|
-
delete:
|
17
|
-
head:
|
12
|
+
get: Net::HTTP::Get,
|
13
|
+
post: Net::HTTP::Post,
|
14
|
+
put: Net::HTTP::Put,
|
15
|
+
patch: Net::HTTP::Patch,
|
16
|
+
delete: Net::HTTP::Delete,
|
17
|
+
head: Net::HTTP::Head,
|
18
18
|
options: Net::HTTP::Options
|
19
19
|
}
|
20
20
|
|
21
|
-
VERB_MAP.
|
21
|
+
VERB_MAP.each_key do |method|
|
22
22
|
define_method method, ->(path, params=nil) {request_json method, path, params}
|
23
23
|
end
|
24
24
|
|
@@ -32,13 +32,13 @@ class Connection
|
|
32
32
|
response
|
33
33
|
end
|
34
34
|
|
35
|
-
def request(method, path, params
|
35
|
+
def request(method, path, params)
|
36
36
|
case method
|
37
37
|
when :get, :head
|
38
|
-
|
39
|
-
request = VERB_MAP[method
|
38
|
+
encoded_path = encode_path_params(path, params)
|
39
|
+
request = VERB_MAP[method].new(encoded_path)
|
40
40
|
else
|
41
|
-
request = VERB_MAP[method
|
41
|
+
request = VERB_MAP[method].new(path)
|
42
42
|
request.body = params.to_json
|
43
43
|
end
|
44
44
|
|
data/test/test_knod.rb
CHANGED
@@ -67,7 +67,7 @@ describe Knod, "a tiny http server" do
|
|
67
67
|
response['Access-Control-Allow-Origin'].must_equal '*'
|
68
68
|
end
|
69
69
|
|
70
|
-
describe '
|
70
|
+
describe 'concatenates files into a json array' do
|
71
71
|
let(:path) {'index'}
|
72
72
|
let(:data) { 3.times.map { |i| { id: i+1, state: 'squiddy' } } }
|
73
73
|
|
@@ -158,56 +158,35 @@ describe Knod, "a tiny http server" do
|
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
let(:patch_data) {{nested: {a: nil, b: 2}}}
|
167
|
-
|
168
|
-
before do
|
169
|
-
FileUtils.mkdir_p(directory)
|
170
|
-
File.write(path, existing_data.to_json)
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'creates the file if it does not exist' do
|
174
|
-
File.delete(path)
|
175
|
-
connection.patch path, patch_data
|
176
|
-
File.file?(path).must_equal true
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'responds with 200 on success' do
|
180
|
-
response = connection.patch path, patch_data
|
181
|
-
response.code.must_equal '200'
|
182
|
-
end
|
161
|
+
describe 'PATCH' do
|
162
|
+
let(:directory) {'index'}
|
163
|
+
let(:path) {"#{directory}/13.json"}
|
164
|
+
let(:existing_data) {{base: 3, nested: {a: 1, c: 3}}}
|
165
|
+
let(:patch_data) {{nested: {a: nil, b: 2}}}
|
183
166
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
167
|
+
before do
|
168
|
+
FileUtils.mkdir_p(directory)
|
169
|
+
File.write(path, existing_data.to_json)
|
170
|
+
end
|
188
171
|
|
189
|
-
|
190
|
-
|
191
|
-
|
172
|
+
it 'creates the file if it does not exist' do
|
173
|
+
File.delete(path)
|
174
|
+
connection.patch path, patch_data
|
175
|
+
File.file?(path).must_equal true
|
192
176
|
end
|
193
|
-
end
|
194
177
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
raise 'boom!'
|
199
|
-
end
|
178
|
+
it 'responds with 200 on success' do
|
179
|
+
response = connection.patch path, patch_data
|
180
|
+
response.code.must_equal '200'
|
200
181
|
end
|
201
182
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
end
|
183
|
+
it 'merges the request data with existing data' do
|
184
|
+
connection.patch path, patch_data
|
185
|
+
parse_json_file(path).must_equal({:base=>3, :nested=>{:c=>3, :b=>2}})
|
206
186
|
end
|
207
187
|
|
208
|
-
|
209
|
-
|
210
|
-
response.code.must_equal '500'
|
188
|
+
after do
|
189
|
+
FileUtils.remove_entry(directory, true)
|
211
190
|
end
|
212
191
|
end
|
213
192
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Moser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
27
41
|
description: An http server built using Ruby's standard library
|
28
42
|
email: ryanpmoser@gmail.com
|
29
43
|
executables:
|
@@ -60,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
74
|
requirements:
|
61
75
|
- - ">="
|
62
76
|
- !ruby/object:Gem::Version
|
63
|
-
version: '2.
|
77
|
+
version: '2.1'
|
64
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - ">="
|
@@ -68,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
82
|
version: '0'
|
69
83
|
requirements: []
|
70
84
|
rubyforge_project:
|
71
|
-
rubygems_version: 2.
|
85
|
+
rubygems_version: 2.4.5.1
|
72
86
|
signing_key:
|
73
87
|
specification_version: 4
|
74
88
|
summary: A tiny RESTful http server
|