knod 0.4.4 → 0.5.2
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/Gemfile.lock +1 -1
- data/knod.gemspec +1 -1
- data/lib/knod.rb +1 -0
- data/lib/knod/patch_merge.rb +21 -0
- data/lib/knod/server.rb +18 -0
- data/lib/knod/version.rb +1 -1
- data/test/test_knod.rb +32 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2b09d9944cc92075dfcc5bc5b55a264cccfaeba
|
4
|
+
data.tar.gz: 10cb27f04c4dd70c79866183b3de3b0515c13d0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dc82b814c22560847fd189f48d83cd8a39873eb84d4d860267057e33aaedb4d33471ff606343a0d0ea6b3f9e3fb7b0c4063e8004ccc090c534394c5633d31bb
|
7
|
+
data.tar.gz: 1e968e34f546090eb583ce3d03458987c6c5c99d9789a4412c5019ed14f8792c20f9f3bddea4a1e1c7660ba714d4fc58e66b3ae0e7dddfc9e33e1f013c2dd78d
|
data/Gemfile.lock
CHANGED
data/knod.gemspec
CHANGED
@@ -19,7 +19,7 @@ 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 = '>= 1
|
22
|
+
gem.required_ruby_version = '>= 2.1'
|
23
23
|
|
24
24
|
gem.add_development_dependency "rake", '~> 10'
|
25
25
|
end
|
data/lib/knod.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module HashWithPatchMerge
|
2
|
+
refine Hash do
|
3
|
+
def patch_merge(other_hash, &block)
|
4
|
+
dup.patch_merge!(other_hash, &block)
|
5
|
+
end
|
6
|
+
|
7
|
+
def patch_merge!(other_hash, &block)
|
8
|
+
other_hash.each_pair do |k,v|
|
9
|
+
tv = self[k]
|
10
|
+
if tv.is_a?(Hash) && v.is_a?(Hash)
|
11
|
+
self[k] = tv.patch_merge(v, &block)
|
12
|
+
else
|
13
|
+
self[k] = block && tv ? block.call(k, tv, v) : v
|
14
|
+
self.delete(k) if self[k].nil?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/lib/knod/server.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Knod
|
2
2
|
class Server
|
3
|
+
using HashWithPatchMerge
|
3
4
|
attr_reader :server, :socket, :request
|
4
5
|
|
5
6
|
DEFAULT_PORT = 4444
|
@@ -63,6 +64,17 @@ module Knod
|
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
67
|
+
def do_PATCH
|
68
|
+
write_to_path(requested_path) do |path|
|
69
|
+
if File.file?(path)
|
70
|
+
merged_data = merge_json(File.read(path), request.body)
|
71
|
+
File.write(path, merged_data)
|
72
|
+
else
|
73
|
+
File.write(path, request.body)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
66
78
|
def do_POST
|
67
79
|
path = requested_path
|
68
80
|
FileUtils.mkdir_p(path)
|
@@ -85,6 +97,12 @@ module Knod
|
|
85
97
|
respond(200, "\"Success\"")
|
86
98
|
end
|
87
99
|
|
100
|
+
def merge_json(file, request_body)
|
101
|
+
file = JSON.parse(file)
|
102
|
+
request_body = JSON.parse(request_body)
|
103
|
+
file.patch_merge(request_body).to_json
|
104
|
+
end
|
105
|
+
|
88
106
|
def log(message)
|
89
107
|
STDERR.puts message if @logging
|
90
108
|
end
|
data/lib/knod/version.rb
CHANGED
data/test/test_knod.rb
CHANGED
@@ -134,6 +134,38 @@ describe Knod, "a tiny http server" do
|
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
+
describe 'PATCH' do
|
138
|
+
let(:directory) {'index'}
|
139
|
+
let(:path) {"#{directory}/13.json"}
|
140
|
+
let(:existing_data) {{base: 3, nested: {a: 1, c: 3}}}
|
141
|
+
let(:patch_data) {{nested: {a: nil, b: 2}}}
|
142
|
+
|
143
|
+
before do
|
144
|
+
FileUtils.mkdir_p(directory)
|
145
|
+
File.write(path, existing_data.to_json)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'creates the file if it does not exist' do
|
149
|
+
File.delete(path)
|
150
|
+
connection.patch path, patch_data
|
151
|
+
File.file?(path).must_equal true
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'responds with 200 on success' do
|
155
|
+
response = connection.patch path, patch_data
|
156
|
+
response.code.must_equal '200'
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'merges the request data with existing data' do
|
160
|
+
connection.patch path, patch_data
|
161
|
+
parse_json_file(path).must_equal({:base=>3, :nested=>{:c=>3, :b=>2}})
|
162
|
+
end
|
163
|
+
|
164
|
+
after do
|
165
|
+
FileUtils.remove_entry(directory, true)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
137
169
|
describe 'error handling' do
|
138
170
|
before do
|
139
171
|
def $knod.do_HEAD
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knod
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Moser
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- bin/knod
|
42
42
|
- knod.gemspec
|
43
43
|
- lib/knod.rb
|
44
|
+
- lib/knod/patch_merge.rb
|
44
45
|
- lib/knod/request.rb
|
45
46
|
- lib/knod/server.rb
|
46
47
|
- lib/knod/version.rb
|
@@ -58,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
59
|
requirements:
|
59
60
|
- - ">="
|
60
61
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1
|
62
|
+
version: '2.1'
|
62
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
64
|
requirements:
|
64
65
|
- - ">="
|