naptime 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/lib/naptime/version.rb +3 -0
- data/lib/naptime.rb +80 -0
- data/naptime.gemspec +23 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: daca12f497823f90582b927cd603b428b83dfc05
|
4
|
+
data.tar.gz: d673fb622d6f10582f3bf0525a5be8f4e3bc7144
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2be92734b994ee990b75ce37b4edd64863392b2cf644dfa9923850f58307c3238b1331d1bf82a59b44364d840e8aef299effe7a6fd972e091c8d6decf2df74a
|
7
|
+
data.tar.gz: 8bbe2d9e4b73050a3465ddace57a6f4b9cfc2fa02b2395de0ec16ba6438e249a65bbf88c532ef2007edfe40cea9d0f9b94f0b625aba3f58fc6804967c12076ba
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Etienne de Bruin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
Naptime
|
2
|
+
=======
|
3
|
+
|
4
|
+
Describe your data model using JSON and have naptime spit out the RESTful API endpoints.
|
5
|
+
|
6
|
+
*Why do this?*
|
7
|
+
|
8
|
+
Often times when I start a project, I end up designing the API before thinking through the completeness of my data. This way, I can imagine exactly what I want my data entity to look like using JSON and have `naptime` output all the relevant RESTful API endpoints.
|
9
|
+
|
10
|
+
How to use
|
11
|
+
==========
|
12
|
+
Create a file that contains your JSON description, eg. example.json and run:
|
13
|
+
|
14
|
+
```sh
|
15
|
+
ruby naptime.rb example.json
|
16
|
+
```
|
17
|
+
|
18
|
+
Example
|
19
|
+
=======
|
20
|
+
Here we created a file `person.json` that contains the following description:
|
21
|
+
|
22
|
+
```json
|
23
|
+
{
|
24
|
+
"name": "Richard",
|
25
|
+
"gender": "male",
|
26
|
+
"siblings": {
|
27
|
+
"brother": {
|
28
|
+
"name": "John",
|
29
|
+
"birthdate" : "5/1/1972"
|
30
|
+
},
|
31
|
+
"sister": "Sarah"
|
32
|
+
},
|
33
|
+
"address": {
|
34
|
+
"street": "582 S Grade",
|
35
|
+
"city": "Alpine"
|
36
|
+
},
|
37
|
+
"employed": "yes"
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
Then we ran `ruby naptime.rb person.json` and got the following output:
|
42
|
+
|
43
|
+
```sh
|
44
|
+
POST /person
|
45
|
+
{"name":"Richard","gender":"male","employed":"yes"}
|
46
|
+
|
47
|
+
GET /person/[id]
|
48
|
+
|
49
|
+
POST /person/[id]
|
50
|
+
{"name":"Richard","gender":"male","employed":"yes"}
|
51
|
+
|
52
|
+
DELETE /person/[id]
|
53
|
+
|
54
|
+
POST /person/[id]/siblings
|
55
|
+
{"sister":"Sarah"}
|
56
|
+
|
57
|
+
GET /person/[id]/siblings/[id]
|
58
|
+
|
59
|
+
POST /person/[id]/siblings/[id]
|
60
|
+
{"sister":"Sarah"}
|
61
|
+
|
62
|
+
DELETE /person/[id]/siblings/[id]
|
63
|
+
|
64
|
+
POST /person/[id]/siblings/[id]/brother
|
65
|
+
{"name":"John","birthdate":"5/1/1972"}
|
66
|
+
|
67
|
+
GET /person/[id]/siblings/[id]/brother/[id]
|
68
|
+
|
69
|
+
POST /person/[id]/siblings/[id]/brother/[id]
|
70
|
+
{"name":"John","birthdate":"5/1/1972"}
|
71
|
+
|
72
|
+
DELETE /person/[id]/siblings/[id]/brother/[id]
|
73
|
+
|
74
|
+
POST /person/[id]/address
|
75
|
+
{"street":"582 S Grade","city":"Alpine"}
|
76
|
+
|
77
|
+
GET /person/[id]/address/[id]
|
78
|
+
|
79
|
+
POST /person/[id]/address/[id]
|
80
|
+
{"street":"582 S Grade","city":"Alpine"}
|
81
|
+
|
82
|
+
DELETE /person/[id]/address/[id]
|
83
|
+
```
|
84
|
+
This output describes the necessary API endpoints to do the CRUD for the described data entity.
|
85
|
+
|
data/Rakefile
ADDED
data/lib/naptime.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require "naptime/version"
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Naptime
|
5
|
+
class REST
|
6
|
+
|
7
|
+
@@crud = {
|
8
|
+
'Create' => {
|
9
|
+
'http' => 'POST',
|
10
|
+
'return' => 'id',
|
11
|
+
'requires' => false,
|
12
|
+
'body' => true
|
13
|
+
},
|
14
|
+
'Read' => {
|
15
|
+
'http' => 'GET',
|
16
|
+
'return' => false,
|
17
|
+
'requires' => 'id',
|
18
|
+
'body' => false
|
19
|
+
},
|
20
|
+
'Update' => {
|
21
|
+
'http' => 'POST',
|
22
|
+
'return' => false,
|
23
|
+
'requires' => 'id',
|
24
|
+
'body' => true
|
25
|
+
},
|
26
|
+
'Delete' => {
|
27
|
+
'http' => 'DELETE',
|
28
|
+
'return' => false,
|
29
|
+
'requires' => 'id',
|
30
|
+
'body' => false
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
def initialize(fileName)
|
35
|
+
|
36
|
+
@rest = Hash.new
|
37
|
+
@restPath = Hash.new
|
38
|
+
|
39
|
+
file = File.open(fileName, "rb")
|
40
|
+
|
41
|
+
self.traverse(File.basename(fileName,".*"), JSON.parse(file.read), '/')
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def traverse(noun, desc, path)
|
46
|
+
|
47
|
+
@rest[noun.to_sym] = Hash.new
|
48
|
+
@restPath[noun.to_sym] = Hash.new
|
49
|
+
@restPath[noun.to_sym]['path'] = path
|
50
|
+
|
51
|
+
desc.each do |x,y|
|
52
|
+
if y.respond_to?('each')
|
53
|
+
self.traverse(x, y, path + noun + '/[id]/')
|
54
|
+
else
|
55
|
+
@rest[noun.to_sym][x] ||= y
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
def output
|
62
|
+
|
63
|
+
@rest.each do |noun, payload|
|
64
|
+
|
65
|
+
@@crud.each do |action, info|
|
66
|
+
path = @restPath[noun]['path'] + noun.to_s
|
67
|
+
id = info['requires'] == 'id' ? '/[id]' : ''
|
68
|
+
|
69
|
+
apiUri = "#{info['http']} #{path}#{id}\n"
|
70
|
+
apiPayload = payload.to_json + "\n" if info['body']
|
71
|
+
|
72
|
+
print apiUri
|
73
|
+
print apiPayload
|
74
|
+
print "\n"
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/naptime.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'naptime/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "naptime"
|
8
|
+
spec.version = Naptime::VERSION
|
9
|
+
spec.authors = ["Etienne de Bruin"]
|
10
|
+
spec.email = ["etdebruin@gmail.com"]
|
11
|
+
spec.summary = %q{Describe your data entity before designing your API}
|
12
|
+
spec.description = %q{Describe your data entity using JSON and have naptime generate your RESTFul API endpoints}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: naptime
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Etienne de Bruin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Describe your data entity using JSON and have naptime generate your RESTFul
|
42
|
+
API endpoints
|
43
|
+
email:
|
44
|
+
- etdebruin@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/naptime.rb
|
55
|
+
- lib/naptime/version.rb
|
56
|
+
- naptime.gemspec
|
57
|
+
homepage: ''
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.3.0
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Describe your data entity before designing your API
|
81
|
+
test_files: []
|