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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02e712cfab8f66fde6adaac61b7362a41c6ab292
4
- data.tar.gz: 66c11aacd9b13f8d8d4216bd5b890f91160a1964
3
+ metadata.gz: d29610e65e8c9fd785f760e9996c5bbcd43d291a
4
+ data.tar.gz: 3934d0c162adfdf35952d5e63afe4ff068082ddf
5
5
  SHA512:
6
- metadata.gz: 00825142271faa45b2b06af86dc9498e9729f18c94b7c628b5f7244733addc8a0f62fe037d2d531703d07016ab8e8e3aa521dcedae8ba892a9c37722bb2799e2
7
- data.tar.gz: 07347c7bd0d059e58a8aeca639f3008d606ecc668169bb96c52a9c84ff36e50584755b02e76a306559695da670c0159104e8090c0df32db416f4603c96de0aa2
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 use for EmberJS development
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 a collection of a selected db
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 thing
35
- http://localhost:4567/api/:thing
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 things
41
- http://localhost:4567/api/:thing
42
+ Returns a list of models
43
+ http://localhost:4567/api/:model
42
44
 
43
- Returns a list of things that matches a specific query
44
- http://localhost:4567/api/:thing?ids[]=id1&ids[]=id2
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 thing with a specific key/value
47
- http://localhost:4567/api/:thing?key=value
48
+ Returns a model with a specific key/value
49
+ http://localhost:4567/api/:model?key=value
48
50
 
49
- Returns a thing with a specific id
50
- http://localhost:4567/api/:thing/:id
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 thing with a specific id
55
- http://localhost:4567/api/:thing/:id
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 thing with a specific id
60
- http://localhost:4567/api/:thing/:id
61
+ Updates a model with a specific id
62
+ http://localhost:4567/api/:model/:id
63
+
64
+
65
+ EmberJS
61
66
 
62
- ## Contributing
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
- mongoDB = Mongo::Connection.new
13
- @@db = nil
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
- @@db = mongoDB.db(params[:db], :pool_size => 5, :timeout => 5)
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
- @@db = mongoDB.db(params[:db], :pool_size => 5, :timeout => 5)
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 @@db == nil
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 = @@db.collection_names
62
+ collections = $db.collection_names
59
63
  "#{collections}"
60
64
  end
61
65
  end
62
66
 
63
67
  #
64
- # Returns a list of things or a list of things that matches a specific query
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/:thing' do
73
+ get '/api/:model' do
70
74
  content_type :json
71
75
  query = {}
72
76
  result = {}
73
- collection = @@db.collection(params[:thing])
77
+ collection = $db.collection(params[:model])
74
78
 
75
79
  if request.query_string.empty?
76
- result = collection.find.to_a.map{|t| frombsonid(t, params[:thing])}.to_json
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 << tobsonid(value)
92
+ ids << toBsonId(value)
89
93
  end
90
94
  else
91
95
  key, value = request.query_string.split('=')
92
- ids << tobsonid(value)
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[:thing]) + "." + key => value}
103
+ query = {modelName(params[:model]) + "." + key => value}
100
104
  end
101
- result = collection.find(query).to_a.map{|t| frombsonid(t, params[:thing])}.to_json
105
+ result = collection.find(query).to_a.map{|t| fromBsonId(t, params[:model])}.to_json
102
106
  end
103
- serializeJSON(result, params[:thing])
107
+ serializeJSON(result, params[:model])
104
108
  end
105
109
 
106
110
  #
107
- # Returns a thing with a specific id
111
+ # Returns a model with a specific id
108
112
  #
109
- get '/api/:thing/:id' do
113
+ get '/api/:model/:id' do
110
114
  content_type :json
111
- find_one(params[:thing], params[:id])
115
+ findOne(params[:model], params[:id])
112
116
  end
113
117
 
114
118
  #
115
- # Create a new thing
119
+ # Create a new model
116
120
  #
117
- post '/api/:thing' do
121
+ post '/api/:model' do
118
122
  content_type :json
119
123
  json = JSON.parse(request.body.read.to_s)
120
- oid = @@db.collection(params[:thing]).insert(json)
121
- find_one(params[:thing], oid.to_s)
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 thing with a specific id
129
+ # Delete a model with a specific id
126
130
  #
127
- delete '/api/:thing/:id' do
131
+ delete '/api/:model/:id' do
128
132
  content_type :json
129
- @@db.collection(params[:thing]).remove({'_id' => tobsonid(params[:id])})
133
+ $db.collection(params[:model]).remove({'_id' => toBsonId(params[:id])})
130
134
  "{}"
131
135
  end
132
136
 
133
137
  #
134
- # Update a thing with a specific id
138
+ # Update a model with a specific id
135
139
  #
136
- put '/api/:thing/:id' do
140
+ put '/api/:model/:id' do
137
141
  content_type :json
138
- selector = {'_id' => tobsonid(params[: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 = @@db.collection(params[:thing]).update(selector, document)
142
- find_one(params[:thing], params[:id])
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 tobsonid(id)
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 frombsonid(obj, thing)
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, thing)
168
+ def serializeJSON(json, model)
165
169
  hash = JSON.parse(json)
166
170
  jsonArray = []
167
- hash.each {|h| jsonArray << h[modelName(params[:thing])]}
168
- newJson = {modelName(params[:thing]) => jsonArray}
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 find_one(thing, id)
176
- result = @@db.collection(thing).find_one(tobsonid(id))
179
+ def findOne(model, id)
180
+ result = $db.collection(model).find_one(toBsonId(id))
177
181
  jsonArray = []
178
182
  if result != nil
179
- normalizedResult = frombsonid(result, thing).to_json
183
+ normalizedResult = fromBsonId(result, model).to_json
180
184
  hash = JSON.parse(normalizedResult)
181
- jsonArray << hash[modelName(thing)]
182
- newJson = {modelName(params[:thing]) => jsonArray}
185
+ jsonArray << hash[modelName(model)]
186
+ newJson = {modelName(params[:model]) => jsonArray}
183
187
  newJson.to_json
184
188
  else
185
- noResults = {modelName(thing) => jsonArray}
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(thing)
194
- #thing.chomp("s")
195
- thing.singularize
197
+ def modelName(model)
198
+ #model.chomp("s")
199
+ model.singularize
196
200
  end
197
201
  end
198
202
  end
@@ -1,3 +1,3 @@
1
1
  module Esapiserver
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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 thing' do
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 things' do
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 things that matches a specific query' do
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 thing with a specific key/value' do
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 thing with a specific id' do
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 thing with a specific id' do
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 thing with a specific id' do
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
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-07-30 00:00:00.000000000 Z
11
+ date: 2014-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler