esapiserver 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -23
- data/lib/esapiserver.rb +51 -47
- data/lib/esapiserver/version.rb +1 -1
- data/spec/esapiserver_spec.rb +11 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d29610e65e8c9fd785f760e9996c5bbcd43d291a
|
4
|
+
data.tar.gz: 3934d0c162adfdf35952d5e63afe4ff068082ddf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f774209bcf0cdf3db811011d47221d9f24dbe076ad04ad93d8d491f821a2fedd9b338bfb562ef49fb9a7728fc9f289ddaad51933cd84cb6d5149ef025dca850
|
7
|
+
data.tar.gz: 5b5989832c12b42cef4255ddb2360ce4e4ee3d3d7dad8bafb3c017ea6d9a68010c5e8525b3e84c9fa73632551abf8b8af0e38889b355c9c9108cc7628d2b94a4
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Esapiserver
|
2
2
|
|
3
|
-
A Sinatra/MongoDB API server to
|
3
|
+
A very lightweight Sinatra/MongoDB CRUD API server to be used for EmberJS development and testing. By using MongoDB as the database server, all the tables are created on the fly when POST requests are made, in other words, no tables needs to be created beforehand.
|
4
|
+
|
5
|
+
I started out using the fixture and local storage adapter in Ember, but experienced that the limitations that these adapters have, would turn out be a pain in the butt later when it was time to release my app - I wanted to make sure that what I test, was consistant with what I would release, hence the esapiserver.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -9,7 +11,7 @@ Run:
|
|
9
11
|
Install the gem
|
10
12
|
$ gem install esapiserver
|
11
13
|
|
12
|
-
Start up your mongoDB server
|
14
|
+
Start up your mongoDB server
|
13
15
|
$ mongoD
|
14
16
|
|
15
17
|
Start the Ember Sinatra/MongoDB API server
|
@@ -25,44 +27,45 @@ Database related requests:
|
|
25
27
|
Reset a db - this will drop and reload the DB
|
26
28
|
http://localhost:4567/reset_db/ember_test_db
|
27
29
|
|
28
|
-
List
|
30
|
+
List the collections of the selected db
|
29
31
|
http://localhost:4567/db_collections
|
30
32
|
|
31
33
|
|
32
34
|
POST request:
|
33
35
|
|
34
|
-
Creates a new
|
35
|
-
http://localhost:4567/api/:
|
36
|
+
Creates a new model
|
37
|
+
http://localhost:4567/api/:model
|
36
38
|
|
37
39
|
|
38
40
|
GET requests:
|
39
41
|
|
40
|
-
Returns a list of
|
41
|
-
http://localhost:4567/api/:
|
42
|
+
Returns a list of models
|
43
|
+
http://localhost:4567/api/:model
|
42
44
|
|
43
|
-
Returns a list of
|
44
|
-
http://localhost:4567/api/:
|
45
|
+
Returns a list of models that matches a specific query
|
46
|
+
http://localhost:4567/api/:model?ids[]=id1&ids[]=id2
|
45
47
|
|
46
|
-
Returns a
|
47
|
-
http://localhost:4567/api/:
|
48
|
+
Returns a model with a specific key/value
|
49
|
+
http://localhost:4567/api/:model?key=value
|
48
50
|
|
49
|
-
Returns a
|
50
|
-
http://localhost:4567/api/:
|
51
|
+
Returns a model with a specific id
|
52
|
+
http://localhost:4567/api/:model/:id
|
51
53
|
|
52
54
|
DELETE request:
|
53
55
|
|
54
|
-
Deletes a
|
55
|
-
http://localhost:4567/api/:
|
56
|
+
Deletes a model with a specific id
|
57
|
+
http://localhost:4567/api/:model/:id
|
56
58
|
|
57
59
|
PUT request:
|
58
60
|
|
59
|
-
Updates a
|
60
|
-
http://localhost:4567/api/:
|
61
|
+
Updates a model with a specific id
|
62
|
+
http://localhost:4567/api/:model/:id
|
63
|
+
|
64
|
+
|
65
|
+
EmberJS
|
61
66
|
|
62
|
-
|
67
|
+
App.ApplicationAdapter = DS.RESTAdapter.extend
|
68
|
+
namespace: 'api'
|
69
|
+
host: 'http://127.0.0.1:4567'
|
70
|
+
corsWithCredentials: true
|
63
71
|
|
64
|
-
1. Fork it ( https://github.com/[my-github-username]/esapiserver/fork )
|
65
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
-
5. Create a new Pull Request
|
data/lib/esapiserver.rb
CHANGED
@@ -9,9 +9,13 @@ require "esapiserver/version"
|
|
9
9
|
|
10
10
|
module Esapiserver
|
11
11
|
class Server < Sinatra::Application
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
POOL_SIZE = 5
|
13
|
+
TIMEOUT = 5
|
14
|
+
|
15
|
+
$mongoDB = Mongo::Connection.new
|
16
|
+
$db = nil
|
17
|
+
|
18
|
+
|
15
19
|
#
|
16
20
|
#
|
17
21
|
#
|
@@ -35,7 +39,7 @@ module Esapiserver
|
|
35
39
|
# running tests.
|
36
40
|
#
|
37
41
|
get '/select_db/:db' do
|
38
|
-
|
42
|
+
$db = $mongoDB.db(params[:db], :pool_size => POOL_SIZE, :timeout => TIMEOUT)
|
39
43
|
"DB #{params[:db]} selected"
|
40
44
|
end
|
41
45
|
|
@@ -43,8 +47,8 @@ module Esapiserver
|
|
43
47
|
# This will drop the DB and reload it - useful for cleaning up when running tests
|
44
48
|
#
|
45
49
|
get '/reset_db/:db' do
|
46
|
-
mongoDB.drop_database(params[:db])
|
47
|
-
|
50
|
+
$mongoDB.drop_database(params[:db])
|
51
|
+
$db = $mongoDB.db(params[:db], :pool_size => POOL_SIZE, :timeout => TIMEOUT)
|
48
52
|
"DB #{params[:db]} dropped and reloaded"
|
49
53
|
end
|
50
54
|
|
@@ -52,28 +56,28 @@ module Esapiserver
|
|
52
56
|
# Get a list of collections for a specific DB
|
53
57
|
#
|
54
58
|
get '/db_collections' do
|
55
|
-
if
|
59
|
+
if $db == nil
|
56
60
|
"Please select a DB using /select_db/:db where :db is the name of the database"
|
57
61
|
else
|
58
|
-
collections =
|
62
|
+
collections = $db.collection_names
|
59
63
|
"#{collections}"
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
63
67
|
#
|
64
|
-
# Returns a list of
|
68
|
+
# Returns a list of models or a list of models that matches a specific query
|
65
69
|
# http://localhost:4567/api/focusareas - will retrieve all the focusareas
|
66
70
|
# http://localhost:4567/api/focusareas?theme_id=53bb2eba19cfd247e4000002 - will retrieve all the focusareas
|
67
71
|
# belonging to the specified theme.
|
68
72
|
# Works only with a single query parameter or multiple ids[] parameters
|
69
|
-
get '/api/:
|
73
|
+
get '/api/:model' do
|
70
74
|
content_type :json
|
71
75
|
query = {}
|
72
76
|
result = {}
|
73
|
-
collection =
|
77
|
+
collection = $db.collection(params[:model])
|
74
78
|
|
75
79
|
if request.query_string.empty?
|
76
|
-
result = collection.find.to_a.map{|t|
|
80
|
+
result = collection.find.to_a.map{|t| fromBsonId(t, params[:model])}.to_json
|
77
81
|
else
|
78
82
|
if request.query_string.include? 'ids[]' or request.query_string.include? 'ids%5B%5D' #don't know why it does not get unescaped
|
79
83
|
ids = []
|
@@ -83,76 +87,76 @@ module Esapiserver
|
|
83
87
|
queries.each do |q|
|
84
88
|
key, value = q.split('=')
|
85
89
|
if key != 'ids[]' and key != 'ids%5B%5D' #check to see if all the keys match
|
86
|
-
throw 'multiple query parameters not supported yet, except _ids[]='
|
90
|
+
throw 'multiple query parameters not supported yet, except for _ids[]='
|
87
91
|
end
|
88
|
-
ids <<
|
92
|
+
ids << toBsonId(value)
|
89
93
|
end
|
90
94
|
else
|
91
95
|
key, value = request.query_string.split('=')
|
92
|
-
ids <<
|
96
|
+
ids << toBsonId(value)
|
93
97
|
end
|
94
98
|
query = {"_id" => { "$in" => ids }}
|
95
99
|
elsif request.query_string.include? '&'
|
96
100
|
throw 'multiple query parameters not supported yet, except _ids[]='
|
97
101
|
else
|
98
102
|
key, value = request.query_string.split('=')
|
99
|
-
query = {modelName(params[:
|
103
|
+
query = {modelName(params[:model]) + "." + key => value}
|
100
104
|
end
|
101
|
-
result = collection.find(query).to_a.map{|t|
|
105
|
+
result = collection.find(query).to_a.map{|t| fromBsonId(t, params[:model])}.to_json
|
102
106
|
end
|
103
|
-
serializeJSON(result, params[:
|
107
|
+
serializeJSON(result, params[:model])
|
104
108
|
end
|
105
109
|
|
106
110
|
#
|
107
|
-
# Returns a
|
111
|
+
# Returns a model with a specific id
|
108
112
|
#
|
109
|
-
get '/api/:
|
113
|
+
get '/api/:model/:id' do
|
110
114
|
content_type :json
|
111
|
-
|
115
|
+
findOne(params[:model], params[:id])
|
112
116
|
end
|
113
117
|
|
114
118
|
#
|
115
|
-
# Create a new
|
119
|
+
# Create a new model
|
116
120
|
#
|
117
|
-
post '/api/:
|
121
|
+
post '/api/:model' do
|
118
122
|
content_type :json
|
119
123
|
json = JSON.parse(request.body.read.to_s)
|
120
|
-
oid =
|
121
|
-
|
124
|
+
oid = $db.collection(params[:model]).insert(json)
|
125
|
+
findOne(params[:model], oid.to_s)
|
122
126
|
end
|
123
127
|
|
124
128
|
#
|
125
|
-
# Delete a
|
129
|
+
# Delete a model with a specific id
|
126
130
|
#
|
127
|
-
delete '/api/:
|
131
|
+
delete '/api/:model/:id' do
|
128
132
|
content_type :json
|
129
|
-
|
133
|
+
$db.collection(params[:model]).remove({'_id' => toBsonId(params[:id])})
|
130
134
|
"{}"
|
131
135
|
end
|
132
136
|
|
133
137
|
#
|
134
|
-
# Update a
|
138
|
+
# Update a model with a specific id
|
135
139
|
#
|
136
|
-
put '/api/:
|
140
|
+
put '/api/:model/:id' do
|
137
141
|
content_type :json
|
138
|
-
selector = {'_id' =>
|
142
|
+
selector = {'_id' => toBsonId(params[:id])}
|
139
143
|
json = JSON.parse(request.body.read).reject{|k,v| k == 'id'}
|
140
144
|
document = {'$set' => json}
|
141
|
-
result =
|
142
|
-
|
145
|
+
result = $db.collection(params[:model]).update(selector, document)
|
146
|
+
findOne(params[:model], params[:id])
|
143
147
|
end
|
144
148
|
|
145
149
|
#
|
146
150
|
# Convert the id to a BSON object id
|
147
151
|
#
|
148
|
-
def
|
152
|
+
def toBsonId(id)
|
149
153
|
BSON::ObjectId.from_string(id)
|
150
154
|
end
|
151
155
|
|
152
156
|
#
|
153
157
|
# Extract the BSON id, then replacing the '_id' key with a 'id' key
|
154
158
|
#
|
155
|
-
def
|
159
|
+
def fromBsonId(obj, model)
|
156
160
|
id = obj['_id'].to_s
|
157
161
|
obj.delete("_id")
|
158
162
|
obj.each{|t| t[1]['id'] = id}
|
@@ -161,28 +165,28 @@ module Esapiserver
|
|
161
165
|
#
|
162
166
|
# Serialize the Mongo JSON to Ember friendly JSON
|
163
167
|
#
|
164
|
-
def serializeJSON(json,
|
168
|
+
def serializeJSON(json, model)
|
165
169
|
hash = JSON.parse(json)
|
166
170
|
jsonArray = []
|
167
|
-
hash.each {|h| jsonArray << h[modelName(params[:
|
168
|
-
newJson = {modelName(params[:
|
171
|
+
hash.each {|h| jsonArray << h[modelName(params[:model])]}
|
172
|
+
newJson = {modelName(params[:model]) => jsonArray}
|
169
173
|
newJson.to_json
|
170
174
|
end
|
171
175
|
|
172
176
|
#
|
173
177
|
# Utility method - find one and the sreialize to Ember Friendly JSON
|
174
178
|
#
|
175
|
-
def
|
176
|
-
result =
|
179
|
+
def findOne(model, id)
|
180
|
+
result = $db.collection(model).find_one(toBsonId(id))
|
177
181
|
jsonArray = []
|
178
182
|
if result != nil
|
179
|
-
normalizedResult =
|
183
|
+
normalizedResult = fromBsonId(result, model).to_json
|
180
184
|
hash = JSON.parse(normalizedResult)
|
181
|
-
jsonArray << hash[modelName(
|
182
|
-
newJson = {modelName(params[:
|
185
|
+
jsonArray << hash[modelName(model)]
|
186
|
+
newJson = {modelName(params[:model]) => jsonArray}
|
183
187
|
newJson.to_json
|
184
188
|
else
|
185
|
-
noResults = {modelName(
|
189
|
+
noResults = {modelName(model) => jsonArray}
|
186
190
|
noResults.to_json
|
187
191
|
end
|
188
192
|
end
|
@@ -190,9 +194,9 @@ module Esapiserver
|
|
190
194
|
#
|
191
195
|
# Very crude method to singularize the model name.
|
192
196
|
#
|
193
|
-
def modelName(
|
194
|
-
#
|
195
|
-
|
197
|
+
def modelName(model)
|
198
|
+
#model.chomp("s")
|
199
|
+
model.singularize
|
196
200
|
end
|
197
201
|
end
|
198
202
|
end
|
data/lib/esapiserver/version.rb
CHANGED
data/spec/esapiserver_spec.rb
CHANGED
@@ -23,7 +23,7 @@ describe 'esapiserver that' do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
describe 'handle POST requests that' do
|
26
|
-
it 'creates a new
|
26
|
+
it 'creates a new model' do
|
27
27
|
payload = '{"post": {"name": "test"}}'
|
28
28
|
post '/api/posts', payload, "CONTENT_TYPE" => "application/json"
|
29
29
|
expect(last_response).to be_ok
|
@@ -33,7 +33,7 @@ describe 'esapiserver that' do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
describe 'handle GET requests that' do
|
36
|
-
it 'returns a list of
|
36
|
+
it 'returns a list of models' do
|
37
37
|
get '/api/posts'
|
38
38
|
expect(last_response).to be_ok
|
39
39
|
expect(last_response.body).to include('{"post":[{"name":"test","id"')
|
@@ -41,7 +41,7 @@ describe 'esapiserver that' do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
#http://127.0.0.1:4567/api/focusareas?ids%5B%5D=53d7781819cfd232f4000085&ids%5B%5D=53d778e319cfd232f4000087
|
44
|
-
it 'returns a list of
|
44
|
+
it 'returns a list of models that matches a specific query' do
|
45
45
|
#create another post
|
46
46
|
payload = '{"post": {"name": "test1"}}'
|
47
47
|
post '/api/posts', payload, "CONTENT_TYPE" => "application/json"
|
@@ -62,14 +62,15 @@ describe 'esapiserver that' do
|
|
62
62
|
end
|
63
63
|
|
64
64
|
#http://localhost:4567/api/posts?name=test1
|
65
|
-
it 'returns a
|
65
|
+
it 'returns a model with a specific key/value' do
|
66
66
|
get '/api/posts?name=test1'
|
67
67
|
expect(last_response).to be_ok
|
68
68
|
expect(last_response.body).to include('{"post":[{"name":"test1","id"')
|
69
69
|
end
|
70
70
|
|
71
|
-
it 'returns a
|
71
|
+
it 'returns a model with a specific id' do
|
72
72
|
get '/api/posts'
|
73
|
+
expect(last_response).to be_ok
|
73
74
|
json_hash = JSON.parse(last_response.body)
|
74
75
|
id = json_hash["post"][0]["id"]
|
75
76
|
get '/api/posts/' + id
|
@@ -80,22 +81,23 @@ describe 'esapiserver that' do
|
|
80
81
|
end
|
81
82
|
|
82
83
|
describe 'handle DELETE requests that' do
|
83
|
-
it 'deletes a
|
84
|
+
it 'deletes a model with a specific id' do
|
84
85
|
get '/api/posts'
|
86
|
+
expect(last_response).to be_ok
|
85
87
|
json_hash = JSON.parse(last_response.body)
|
86
88
|
id = json_hash["post"][1]["id"]
|
87
89
|
delete '/api/posts/' + id
|
88
|
-
expect(last_response).to be_ok
|
89
90
|
get '/api/posts'
|
90
|
-
json_hash = JSON.parse(last_response.body)
|
91
91
|
expect(last_response).to be_ok
|
92
|
+
json_hash = JSON.parse(last_response.body)
|
92
93
|
expect(json_hash["post"].length).to equal(1)
|
93
94
|
end
|
94
95
|
end
|
95
96
|
|
96
97
|
describe 'handle PUT requests that' do
|
97
|
-
it 'updates a
|
98
|
+
it 'updates a model with a specific id' do
|
98
99
|
get '/api/posts?name=test'
|
100
|
+
expect(last_response).to be_ok
|
99
101
|
json_hash = JSON.parse(last_response.body)
|
100
102
|
id = json_hash["post"][0]["id"]
|
101
103
|
payload = '{"post": {"name": "updated test"}}'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: esapiserver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Miles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|