knod 0.5.2 → 0.6.1
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/.travis.yml +2 -0
- data/Gemfile.lock +1 -1
- data/Readme.md +7 -1
- data/knod.gemspec +2 -2
- data/lib/knod.rb +2 -0
- data/lib/knod/file_utilities.rb +18 -0
- data/lib/knod/server.rb +32 -25
- data/lib/knod/version.rb +1 -1
- data/test/test_knod.rb +27 -25
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 981cfd40acef82d01c9cf8be4dca564f15cf4a8e
|
4
|
+
data.tar.gz: b5287727a6d34b172d7239108eab4b6317522317
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a771a86adb892dc852ddfedd37db30f241bcdcd65247defadcd04969f437f21695f14fcd9ea227ab278596b05e4c568f9114ca9ff0ac091733ce2d6660bdc01a
|
7
|
+
data.tar.gz: 48eeb718da151b2a51ca6fc6c3da9a2d0533980f7e4096c5a82482d84f8c95ec1d667620bbda003bccdabf0c4b40c2039cdae3897e7f9246c2ea47c3d73d1de9
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/Readme.md
CHANGED
@@ -2,12 +2,18 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/knod) [](https://travis-ci.org/moserrya/knod) [](https://codeclimate.com/github/moserrya/knod)
|
4
4
|
|
5
|
-
Knod is a lightweight HTTP server designed to
|
5
|
+
Knod is a lightweight HTTP server designed to help with front end development when the corresponding back end is missing or incomplete. It responds to GET, PUT, POST, PATCH, and DELETE, serving up, writing to, and deleting from the directory of your choice. Knod has no dependencies outside of the Ruby standard library.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
+
Ruby 2.1 and above:
|
10
|
+
|
9
11
|
```gem install knod```
|
10
12
|
|
13
|
+
Older versions of Ruby:
|
14
|
+
|
15
|
+
```gem install knod -v 0.4.4``` (does not support PATCH)
|
16
|
+
|
11
17
|
## Usage
|
12
18
|
|
13
19
|
The Knod gem comes with an executable; you can run it from the command line with `knod`. Knod will default to port 4444 and the current directory. You can change these with command line arguments (-p and -d, respectively).
|
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 = '2014-
|
9
|
+
gem.date = '2014-06-01'
|
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,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 = '>= 2.
|
22
|
+
gem.required_ruby_version = '>= 2.0'
|
23
23
|
|
24
24
|
gem.add_development_dependency "rake", '~> 10'
|
25
25
|
end
|
data/lib/knod.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Knod
|
2
|
+
module FileUtilities
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegator :FileUtils, :mkdir_p, :create_directory
|
6
|
+
|
7
|
+
def_delegator :File, :delete, :delete_file
|
8
|
+
def_delegator :File, :join, :join_path
|
9
|
+
def_delegator :File, :read, :read_file
|
10
|
+
def_delegator :File, :write, :write_file
|
11
|
+
|
12
|
+
def_delegators :File, :file?, :dirname, :directory?
|
13
|
+
|
14
|
+
def file_extension(path)
|
15
|
+
File.extname(path).split('.').last
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/knod/server.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Knod
|
2
2
|
class Server
|
3
|
-
|
3
|
+
include FileUtilities
|
4
|
+
|
4
5
|
attr_reader :server, :socket, :request
|
5
6
|
|
6
7
|
DEFAULT_PORT = 4444
|
@@ -35,9 +36,9 @@ module Knod
|
|
35
36
|
|
36
37
|
def do_GET(head=false)
|
37
38
|
path = requested_path
|
38
|
-
path =
|
39
|
+
path = join_path(path, 'index.html') if directory?(path)
|
39
40
|
|
40
|
-
if
|
41
|
+
if file?(path)
|
41
42
|
File.open(path, 'rb') do |file|
|
42
43
|
socket.print file_response_header(file)
|
43
44
|
IO.copy_stream(file, socket) unless head
|
@@ -54,33 +55,35 @@ module Knod
|
|
54
55
|
|
55
56
|
def do_DELETE
|
56
57
|
path = requested_path
|
57
|
-
|
58
|
+
delete_file(path) if file?(path)
|
58
59
|
respond 204
|
59
60
|
end
|
60
61
|
|
61
62
|
def do_PUT
|
62
|
-
write_to_path(requested_path)
|
63
|
-
|
64
|
-
end
|
63
|
+
write_to_path(requested_path, request.body)
|
64
|
+
respond(200, "\"Success\"")
|
65
65
|
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
if RUBY_VERSION.to_f >= 2.1
|
68
|
+
using HashWithPatchMerge
|
69
|
+
|
70
|
+
def do_PATCH
|
71
|
+
path = requested_path
|
72
|
+
data = if file?(path)
|
73
|
+
merge_json(read_file(path), request.body)
|
74
|
+
else
|
75
|
+
request.body
|
76
|
+
end
|
77
|
+
write_to_path(path, data)
|
78
|
+
respond(200, "\"Success\"")
|
75
79
|
end
|
76
80
|
end
|
77
81
|
|
78
82
|
def do_POST
|
79
83
|
path = requested_path
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
File.write(File.join(path, "#{next_id}.json"), request.body)
|
84
|
+
create_directory(path)
|
85
|
+
next_id = max_id_in_path(path) + 1
|
86
|
+
write_file(join_path(path, "#{next_id}.json"), request.body)
|
84
87
|
respond(201, "{\"id\":#{next_id}}")
|
85
88
|
end
|
86
89
|
|
@@ -90,11 +93,15 @@ module Knod
|
|
90
93
|
|
91
94
|
private
|
92
95
|
|
93
|
-
def write_to_path(path)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
def write_to_path(path, data)
|
97
|
+
directory_name = dirname(path)
|
98
|
+
create_directory(directory_name)
|
99
|
+
write_file(path, data)
|
100
|
+
end
|
101
|
+
|
102
|
+
def max_id_in_path(path)
|
103
|
+
records = Dir.glob(path + "/*.json")
|
104
|
+
records.map {|r| File.basename(r, ".json") }.map(&:to_i).max || 0
|
98
105
|
end
|
99
106
|
|
100
107
|
def merge_json(file, request_body)
|
@@ -154,7 +161,7 @@ module Knod
|
|
154
161
|
DEFAULT_CONTENT_TYPE = 'application/octet-stream'
|
155
162
|
|
156
163
|
def content_type(path)
|
157
|
-
ext =
|
164
|
+
ext = file_extension(path)
|
158
165
|
CONTENT_TYPE_MAPPING[ext] || DEFAULT_CONTENT_TYPE
|
159
166
|
end
|
160
167
|
|
data/lib/knod/version.rb
CHANGED
data/test/test_knod.rb
CHANGED
@@ -134,35 +134,37 @@ describe Knod, "a tiny http server" do
|
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
137
|
+
if RUBY_VERSION.to_f >= 2.1
|
138
|
+
describe 'PATCH' do
|
139
|
+
let(:directory) {'index'}
|
140
|
+
let(:path) {"#{directory}/13.json"}
|
141
|
+
let(:existing_data) {{base: 3, nested: {a: 1, c: 3}}}
|
142
|
+
let(:patch_data) {{nested: {a: nil, b: 2}}}
|
143
|
+
|
144
|
+
before do
|
145
|
+
FileUtils.mkdir_p(directory)
|
146
|
+
File.write(path, existing_data.to_json)
|
147
|
+
end
|
147
148
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
149
|
+
it 'creates the file if it does not exist' do
|
150
|
+
File.delete(path)
|
151
|
+
connection.patch path, patch_data
|
152
|
+
File.file?(path).must_equal true
|
153
|
+
end
|
153
154
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
155
|
+
it 'responds with 200 on success' do
|
156
|
+
response = connection.patch path, patch_data
|
157
|
+
response.code.must_equal '200'
|
158
|
+
end
|
158
159
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
160
|
+
it 'merges the request data with existing data' do
|
161
|
+
connection.patch path, patch_data
|
162
|
+
parse_json_file(path).must_equal({:base=>3, :nested=>{:c=>3, :b=>2}})
|
163
|
+
end
|
163
164
|
|
164
|
-
|
165
|
-
|
165
|
+
after do
|
166
|
+
FileUtils.remove_entry(directory, true)
|
167
|
+
end
|
166
168
|
end
|
167
169
|
end
|
168
170
|
|
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: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Moser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- bin/knod
|
42
42
|
- knod.gemspec
|
43
43
|
- lib/knod.rb
|
44
|
+
- lib/knod/file_utilities.rb
|
44
45
|
- lib/knod/patch_merge.rb
|
45
46
|
- lib/knod/request.rb
|
46
47
|
- lib/knod/server.rb
|
@@ -59,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
60
|
requirements:
|
60
61
|
- - ">="
|
61
62
|
- !ruby/object:Gem::Version
|
62
|
-
version: '2.
|
63
|
+
version: '2.0'
|
63
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
65
|
requirements:
|
65
66
|
- - ">="
|