knod 0.8.0 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24db330e2ed1cad1a1765169a47239723f4387da
4
- data.tar.gz: c744a80528dfeb1dc3e9597299239362a7782aaa
3
+ metadata.gz: bdbb4e14fe3660fb4bb030db3d9aa1f866375f68
4
+ data.tar.gz: c7bdf61f293efebf3088d693ff5cedb20299af3e
5
5
  SHA512:
6
- metadata.gz: a5b9650afbf606e35421ca86390e43812db7fe922e74fae0f8a93d55e9aa8f5624c3839260566ed767c779ba3ed0ae8d0032c6207687b5e4e28814a6aa07ba5f
7
- data.tar.gz: f4e6215b5e0d7252427c829e1049140ec9158ec762f7e89adbe1c0d1fc006c27efd71bcc62c9b140a0e2be98806e8f56e5d59582942ffc584db0f306f69ae8f8
6
+ metadata.gz: f167fd7a82546afa06f6e866ac5b3f2be6932e9c3fa24ad088c86decb65af88dad1de86496565e24f09e03261cad79899fcf2e43f40326993ac6bcb7b0e2b074
7
+ data.tar.gz: b6aa207913a06d2d66f8e94259dee7ca4a03c48d1c4c04cdc70e105b74563868caebde8d81805198adcb2562b9e5886c2fba7b7d339f82d6c08df1640cded5ac
@@ -1,6 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1.0
5
- - 2.1.1
6
- - 2.1.2
3
+ - 2.2.5
4
+ - 2.3.1
@@ -1,11 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- knod (0.8.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
@@ -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-06-25'
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.0'
22
+ gem.required_ruby_version = '>= 2.1'
23
23
 
24
- gem.add_development_dependency "rake", '~> 10'
24
+ gem.add_development_dependency 'rake', '~> 10'
25
+ gem.add_development_dependency 'minitest', '~> 5'
25
26
  end
@@ -17,7 +17,7 @@ module Knod
17
17
 
18
18
  def concat_json(path)
19
19
  files = Dir.glob(join_path(path, "*")).sort
20
- data = files.map { |f| File.read(f) }
20
+ data = files.map { |f| read_file(f) }
21
21
  json_data = '[' + data.join(',') + ']'
22
22
  end
23
23
  end
@@ -2,7 +2,7 @@ module Knod
2
2
  class Server
3
3
  include FileUtilities
4
4
 
5
- attr_reader :server, :socket, :request
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
- accept_request_and_respond
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
- @socket = server.accept
26
- @request = Request.new(socket)
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
- socket.close if socket
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
- socket.print file_response_header(file)
43
- IO.copy_stream(file, socket) unless head
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
- if RUBY_VERSION.to_f >= 2.1
69
- using HashWithPatchMerge
70
+ using HashWithPatchMerge
70
71
 
71
- def do_PATCH
72
- path = requested_path
73
- data = if file?(path)
74
- merge_json(read_file(path), request.body)
75
- else
76
- request.body
77
- end
78
- write_to_path(path, data)
79
- respond(200, "\"Success\"")
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
- socket.print response_header(status_code, message)
141
- socket.print message unless message.empty?
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[ext] || DEFAULT_CONTENT_TYPE
168
+ CONTENT_TYPE_MAPPING.fetch(ext) { DEFAULT_CONTENT_TYPE }
169
169
  end
170
170
 
171
171
  def requested_path
@@ -1,3 +1,3 @@
1
1
  module Knod
2
- VERSION = '0.8.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -9,16 +9,16 @@ class Connection
9
9
  end
10
10
 
11
11
  VERB_MAP = {
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,
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.keys.each do |method|
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
- full_path = encode_path_params(path, params)
39
- request = VERB_MAP[method.to_sym].new(full_path)
38
+ encoded_path = encode_path_params(path, params)
39
+ request = VERB_MAP[method].new(encoded_path)
40
40
  else
41
- request = VERB_MAP[method.to_sym].new(path)
41
+ request = VERB_MAP[method].new(path)
42
42
  request.body = params.to_json
43
43
  end
44
44
 
@@ -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 'contatenates files into a json array' do
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
- if RUBY_VERSION.to_f >= 2.1
162
- describe 'PATCH' do
163
- let(:directory) {'index'}
164
- let(:path) {"#{directory}/13.json"}
165
- let(:existing_data) {{base: 3, nested: {a: 1, c: 3}}}
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
- it 'merges the request data with existing data' do
185
- connection.patch path, patch_data
186
- parse_json_file(path).must_equal({:base=>3, :nested=>{:c=>3, :b=>2}})
187
- end
167
+ before do
168
+ FileUtils.mkdir_p(directory)
169
+ File.write(path, existing_data.to_json)
170
+ end
188
171
 
189
- after do
190
- FileUtils.remove_entry(directory, true)
191
- end
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
- describe 'error handling' do
196
- before do
197
- def $knod.do_HEAD
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
- after do
203
- def $knod.do_HEAD
204
- do_GET(head=true)
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
- it 'responds to server errors with a 500' do
209
- response = connection.head '/index.html'
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.8.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: 2014-06-25 00:00:00.000000000 Z
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.0'
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.2.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