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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2b09d9944cc92075dfcc5bc5b55a264cccfaeba
4
- data.tar.gz: 10cb27f04c4dd70c79866183b3de3b0515c13d0f
3
+ metadata.gz: 981cfd40acef82d01c9cf8be4dca564f15cf4a8e
4
+ data.tar.gz: b5287727a6d34b172d7239108eab4b6317522317
5
5
  SHA512:
6
- metadata.gz: 1dc82b814c22560847fd189f48d83cd8a39873eb84d4d860267057e33aaedb4d33471ff606343a0d0ea6b3f9e3fb7b0c4063e8004ccc090c534394c5633d31bb
7
- data.tar.gz: 1e968e34f546090eb583ce3d03458987c6c5c99d9789a4412c5019ed14f8792c20f9f3bddea4a1e1c7660ba714d4fc58e66b3ae0e7dddfc9e33e1f013c2dd78d
6
+ metadata.gz: a771a86adb892dc852ddfedd37db30f241bcdcd65247defadcd04969f437f21695f14fcd9ea227ab278596b05e4c568f9114ca9ff0ac091733ce2d6660bdc01a
7
+ data.tar.gz: 48eeb718da151b2a51ca6fc6c3da9a2d0533980f7e4096c5a82482d84f8c95ec1d667620bbda003bccdabf0c4b40c2039cdae3897e7f9246c2ea47c3d73d1de9
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 2.1.0
5
+ - 2.1.1
4
6
  - 2.1.2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- knod (0.5.2)
4
+ knod (0.6.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Readme.md CHANGED
@@ -2,12 +2,18 @@
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 facilitate 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 choosing. Knod has no dependencies outside of the Ruby standard library.
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).
@@ -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-05-27'
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.1'
22
+ gem.required_ruby_version = '>= 2.0'
23
23
 
24
24
  gem.add_development_dependency "rake", '~> 10'
25
25
  end
@@ -2,6 +2,8 @@ require 'socket'
2
2
  require 'uri'
3
3
  require 'fileutils'
4
4
  require 'json'
5
+ require 'forwardable'
6
+ require 'knod/file_utilities'
5
7
  require 'knod/request'
6
8
  require 'knod/patch_merge'
7
9
  require 'knod/server'
@@ -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
@@ -1,6 +1,7 @@
1
1
  module Knod
2
2
  class Server
3
- using HashWithPatchMerge
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 = File.join(path, 'index.html') if File.directory?(path)
39
+ path = join_path(path, 'index.html') if directory?(path)
39
40
 
40
- if File.file?(path)
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
- File.delete(path) if File.file?(path)
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) do |path|
63
- File.write(path, request.body)
64
- end
63
+ write_to_path(requested_path, request.body)
64
+ respond(200, "\"Success\"")
65
65
  end
66
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
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
- FileUtils.mkdir_p(path)
81
- records = Dir.glob(path + "/*.json")
82
- next_id = (records.map {|r| File.basename(r, ".json") }.map(&:to_i).max || 0) + 1
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
- directory = File.dirname(path)
95
- FileUtils.mkdir_p(directory)
96
- yield path
97
- respond(200, "\"Success\"")
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 = File.extname(path).split('.').last
164
+ ext = file_extension(path)
158
165
  CONTENT_TYPE_MAPPING[ext] || DEFAULT_CONTENT_TYPE
159
166
  end
160
167
 
@@ -1,3 +1,3 @@
1
1
  module Knod
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -134,35 +134,37 @@ 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
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
- 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
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
- it 'responds with 200 on success' do
155
- response = connection.patch path, patch_data
156
- response.code.must_equal '200'
157
- end
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
- 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
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
- after do
165
- FileUtils.remove_entry(directory, true)
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.5.2
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-05-27 00:00:00.000000000 Z
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.1'
63
+ version: '2.0'
63
64
  required_rubygems_version: !ruby/object:Gem::Requirement
64
65
  requirements:
65
66
  - - ">="