knod 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71a4533e73396a337e3e2426a2fca6e5e05dcf0a
4
- data.tar.gz: 91958c4fde8b9251de6830f02bb048197c13db65
3
+ metadata.gz: 8446a5531fc6d08eb78e71e980168a931d1ac919
4
+ data.tar.gz: d52ad8908dc15d28be4be47c0730ab173c701209
5
5
  SHA512:
6
- metadata.gz: 7301dfc75b202fc046ecb14ee21f054fec139f9963336452bb70c418708fd2ea89e3d39c26ca6e51efaaa69a534f3170180a3ad7ba540a13155016d92c00cb04
7
- data.tar.gz: 0d61f1ac43a1ba92fd08fed22ecbf9359ea93dbe64480a271a7a20f23dc92f1c0a679ae2069d2508b470537a4dce978c96b7440b6c1dd5430d361adf1f28eb03
6
+ metadata.gz: 9b9a3ef52a2dab0171621c4ee32f19b1f6d1e3e0dc46e04ff037b8e7d0274059ccc1801e6b74f49ff4be9d9b30e00a1e52167812117162083b2f1f08606ea432
7
+ data.tar.gz: fc1a4437cf870a38032161a4b9159d43e8fbe0e35727cfd826d0b3aafebad05f838918d98ad96515794777f0eed3936f978ebe2b150bf603239e6293c12fd87f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- knod (0.6.2)
4
+ knod (0.7.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Readme.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/knod.svg)](http://badge.fury.io/rb/knod) [![Build Status](https://travis-ci.org/moserrya/knod.svg?branch=master)](https://travis-ci.org/moserrya/knod) [![Code Climate](https://codeclimate.com/github/moserrya/knod.png)](https://codeclimate.com/github/moserrya/knod)
4
4
 
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.
5
+ Knod is a simple HTTP server for prototyping rich JavaScript apps. 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
 
@@ -23,7 +23,7 @@ Logging is enabled by default. The server will select an open ephemeral port at
23
23
 
24
24
  Knod sanitizes the path on all requests and does not allow access to folders outside of the root directory where it is run.
25
25
 
26
- GET requests map suffixes into MIME types. Data is considered to be `application/octet-stream` if the content type is unrecognized.
26
+ If the path for a GET request is a file, file will be mapped to its MIME type based on the file suffix. Data is considered to be `application/octet-stream` if the content type is unrecognized. If the path for a GET request is a directory, Knod will concatenate the contents of all files in the directory into a JSON array.
27
27
 
28
28
  All data from PUT, POST, and PATCH requests is stored as JSON. If the pathway specified in the request does not exist, Knod will create it.
29
29
 
@@ -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-01'
9
+ gem.date = '2014-06-15'
10
10
  gem.authors = ['Ryan Moser']
11
11
  gem.email = 'ryanpmoser@gmail.com'
12
12
  gem.homepage = 'https://github.com/moserrya/knod'
@@ -15,7 +15,4 @@ module Knod
15
15
  end
16
16
  end
17
17
 
18
- if __FILE__ == $0
19
- Knod.start
20
- end
21
-
18
+ Knod.start if __FILE__ == $PROGRAM_NAME
@@ -14,5 +14,11 @@ module Knod
14
14
  def file_extension(path)
15
15
  File.extname(path).split('.').last
16
16
  end
17
+
18
+ def concat_json(path)
19
+ files = Dir.glob(join_path(path, "*"))
20
+ data = files.map { |f| File.read(f) }
21
+ json_data = '[' + data.join(',') + ']'
22
+ end
17
23
  end
18
24
  end
@@ -5,17 +5,16 @@ module HashWithPatchMerge
5
5
  end
6
6
 
7
7
  def patch_merge!(other_hash, &block)
8
- other_hash.each_pair do |k,v|
8
+ other_hash.each_pair do |k, v|
9
9
  tv = self[k]
10
10
  if tv.is_a?(Hash) && v.is_a?(Hash)
11
11
  self[k] = tv.patch_merge(v, &block)
12
12
  else
13
13
  self[k] = block && tv ? block.call(k, tv, v) : v
14
- self.delete(k) if self[k].nil?
14
+ delete(k) if self[k].nil?
15
15
  end
16
16
  end
17
17
  self
18
18
  end
19
19
  end
20
20
  end
21
-
@@ -13,18 +13,18 @@ module Knod
13
13
  loop do
14
14
  line = socket.gets
15
15
  break if line == "\r\n"
16
- name, value = line.strip.split(": ")
16
+ name, value = line.strip.split(': ')
17
17
  headers[name] = value
18
18
  end
19
19
  @headers = headers
20
20
  end
21
21
 
22
22
  def content_length
23
- headers["Content-Length"].to_i
23
+ headers['Content-Length'].to_i
24
24
  end
25
25
 
26
26
  def content_type
27
- headers["Content-Type"]
27
+ headers['Content-Type']
28
28
  end
29
29
 
30
30
  def uri
@@ -7,7 +7,7 @@ module Knod
7
7
  DEFAULT_PORT = 4444
8
8
  DEFAULT_WEB_ROOT = './'
9
9
 
10
- def initialize(options={})
10
+ def initialize(options = {})
11
11
  port = options.fetch(:port) { DEFAULT_PORT }
12
12
  @root = options.fetch(:root) { DEFAULT_WEB_ROOT }
13
13
  @logging = options.fetch(:logging) { true }
@@ -34,15 +34,16 @@ module Knod
34
34
  socket.close if socket
35
35
  end
36
36
 
37
- def do_GET(head=false)
37
+ def do_GET(head = false)
38
38
  path = requested_path
39
- path = join_path(path, 'index.html') if directory?(path)
40
39
 
41
40
  if file?(path)
42
41
  File.open(path, 'rb') do |file|
43
42
  socket.print file_response_header(file)
44
43
  IO.copy_stream(file, socket) unless head
45
44
  end
45
+ elsif directory?(path)
46
+ respond(200, concat_json(path))
46
47
  else
47
48
  message = head ? '' : "\"File not found\""
48
49
  respond(404, message)
@@ -50,7 +51,7 @@ module Knod
50
51
  end
51
52
 
52
53
  def do_HEAD
53
- do_GET(head=true)
54
+ do_GET(head = true)
54
55
  end
55
56
 
56
57
  def do_DELETE
@@ -100,8 +101,8 @@ module Knod
100
101
  end
101
102
 
102
103
  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
104
+ records = Dir.glob(path + '/*.json')
105
+ records.map { |r| File.basename(r, '.json') }.map(&:to_i).max || 0
105
106
  end
106
107
 
107
108
  def merge_json(file, request_body)
@@ -119,12 +120,12 @@ module Knod
119
120
  end
120
121
 
121
122
  STATUS_CODE_MAPPINGS = {
122
- 200 => "OK",
123
- 201 => "Created",
124
- 204 => "No Content",
125
- 404 => "Not Found",
126
- 500 => "Internal Server Error",
127
- 501 => "Not Implemented"
123
+ 200 => 'OK',
124
+ 201 => 'Created',
125
+ 204 => 'No Content',
126
+ 404 => 'Not Found',
127
+ 500 => 'Internal Server Error',
128
+ 501 => 'Not Implemented'
128
129
  }
129
130
 
130
131
  def response_header(status_code, message)
@@ -134,15 +135,15 @@ module Knod
134
135
  header << "Connection: close\r\n\r\n"
135
136
  end
136
137
 
137
- def respond(status_code, message='')
138
+ def respond(status_code, message = '')
138
139
  socket.print response_header(status_code, message)
139
140
  socket.print message unless message.empty?
140
141
  end
141
142
 
142
143
  def file_response_header(file)
143
- "HTTP/1.1 200 OK\r\n" <<
144
- "Content-Type: #{content_type(file)}\r\n" <<
145
- "Content-Length: #{file.size}\r\n" <<
144
+ "HTTP/1.1 200 OK\r\n" \
145
+ "Content-Type: #{content_type(file)}\r\n" \
146
+ "Content-Length: #{file.size}\r\n" \
146
147
  "Connection: close\r\n\r\n"
147
148
  end
148
149
 
@@ -170,7 +171,7 @@ module Knod
170
171
 
171
172
  clean = []
172
173
 
173
- parts = local_path.split("/")
174
+ parts = local_path.split('/')
174
175
 
175
176
  parts.each do |part|
176
177
  next if part.empty? || part == '.'
@@ -181,8 +182,8 @@ module Knod
181
182
  end
182
183
 
183
184
  def method_missing(method_sym, *args, &block)
184
- if method_sym.to_s.start_with?("do_")
185
- respond(501, "\"not implemented\"")
185
+ if method_sym.to_s.start_with?('do_')
186
+ respond(501, '"not implemented"')
186
187
  else
187
188
  super
188
189
  end
@@ -1,3 +1,3 @@
1
1
  module Knod
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -37,11 +37,6 @@ describe Knod, "a tiny http server" do
37
37
  response.body.must_equal @body
38
38
  end
39
39
 
40
- it 'implictly serves up the index' do
41
- response = connection.get "/"
42
- response.body.must_equal @body
43
- end
44
-
45
40
  it 'returns a 404 if the file does not exist' do
46
41
  response = connection.get "/squidbat.random"
47
42
  response.code.must_equal '404'
@@ -66,6 +61,25 @@ describe Knod, "a tiny http server" do
66
61
  response = connection.delete @path
67
62
  response.code.must_equal '204'
68
63
  end
64
+
65
+ describe 'contatenates files into a json array' do
66
+ let(:path) {'index'}
67
+ let(:data) { 3.times.map { |i| { id: i+1, state: 'squiddy' } } }
68
+
69
+ before do
70
+ FileUtils.mkdir_p(path)
71
+ data.each_with_index {|d, i| File.write(File.join(".", path, "#{i+1}.json"), d.to_json)}
72
+ end
73
+
74
+ it 'should concatenate file contents into an array' do
75
+ response = connection.get path
76
+ response.body.sort_by { |h| h[:id] }.must_equal data
77
+ end
78
+
79
+ after do
80
+ FileUtils.remove_entry(path, true)
81
+ end
82
+ end
69
83
  end
70
84
 
71
85
  describe 'PUT' do
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.6.2
4
+ version: 0.7.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-01 00:00:00.000000000 Z
11
+ date: 2014-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake