auto_api 0.0.5 → 0.0.6

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: 3232aba5325efd6996be75677997684bfda69f7d
4
- data.tar.gz: d14cc7a7e03eb926420b5359d840d1c1bf141247
3
+ metadata.gz: 61da606bd6dd0ba85d235ff83595fcba05634ac1
4
+ data.tar.gz: ce6a77f7f348684e6a260d45b85e71b98fc1b544
5
5
  SHA512:
6
- metadata.gz: 933b8e6ab97db695caa5a7eb83eaf3a67e5bb744a226f463e40eff2deb1eed7fc5503524fd127f66e0e8c41be6bdab3689450df5f86c38f9a186f05eb5e8920b
7
- data.tar.gz: 87be169960c27295c95f504cf48cd130d509fae61b092a11de818843b58191b29df9270e36dcdc21ee84d41d465e1c107f5e7eec7cc21350d740392552f21e5d
6
+ metadata.gz: d4ee01d473f6bffaf149d63abf092bece678600800235f0d9a3599b783148c308a10be287b8a3259b12717f1c24c21a270f28e1516f908c7209201ee0797fc54
7
+ data.tar.gz: 97dd6e121f7b84bb1caa2125d81faf4d5967a8a938a9893b506d4d069fde93fecb2e4c268e12e9eb7d0fc0abb7fb02f62e2ac57a2164a137c5af9083b6728749
data/lib/auto_api/base.rb CHANGED
@@ -1,27 +1,23 @@
1
1
  require 'sinatra'
2
- require "sinatra/reloader"
3
- require "sinatra/json"
2
+ require 'sinatra/reloader'
3
+ require 'sinatra/json'
4
4
  require 'mongoid'
5
5
  require 'active_support/inflector'
6
6
  require 'active_model'
7
- require 'logger'
8
-
9
-
10
7
 
11
8
  class AutoApi::Base < Sinatra::Base
12
- logger = Logger.new(STDOUT)
13
9
  configure :development do
14
10
  register Sinatra::Reloader
15
11
  end
16
12
 
17
13
  Mongoid.load!('./config/mongoid.yml')
18
14
 
19
- #WE ARE RETURNING JSON
15
+ # WE ARE RETURNING JSON
20
16
  before '/*' do
21
17
  content_type 'application/json'
22
18
  end
23
19
 
24
- before '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
20
+ before '/:resource1/:id1/:resource2/?' do |resource1, _id1, resource2|
25
21
  @resource1 = Object.const_set(resource1.classify, Class.new)
26
22
  @resource1.include(Mongoid::Document)
27
23
  @resource1.has_many resource2
@@ -37,66 +33,62 @@ logger = Logger.new(STDOUT)
37
33
  end
38
34
  end
39
35
 
40
- get '/:resource/?' do | resource |
41
- webtry(lambda { json @resource.all })
36
+ get '/:resource/?' do |_resource|
37
+ webtry { json @resource.all }
42
38
  end
43
39
 
44
- get '/:resource/:id/?' do | resource, id |
45
- webtry(lambda { json @resource.find(id) })
40
+ get '/:resource/:id/?' do |_resource, id|
41
+ webtry { json @resource.find(id) }
46
42
  end
47
43
 
48
- post '/:resource/?' do | resource |
49
- logger.debug(request.body.read)
50
- request.body.rewind
51
- webtry(lambda {
52
- resource = @resource.new(JSON.parse request.body.read)
53
- resource.save!
54
- json resource
55
- })
44
+ post '/:resource/?' do |resource|
45
+ webtry do
46
+ resource = @resource.new(request.params)
47
+ resource.save!
48
+ json resource
49
+ end
56
50
  end
57
51
 
58
- put '/:resource/:id/?' do | resource, id |
59
- webtry(lambda {
60
- resource = @resource.find(id)
61
- resource.update_attributes!(JSON.parse request.body.read)
62
- })
52
+ put '/:resource/:id/?' do |resource, id|
53
+ webtry do
54
+ resource = @resource.find(id)
55
+ resource.update_attributes!(request.params)
56
+ end
63
57
  end
64
58
 
65
- delete '/:resource/:id/?' do | resource, id |
66
- webtry(lambda { json @resource.find(id).delete })
59
+ delete '/:resource/:id/?' do |_resource, id|
60
+ webtry { json @resource.find(id).delete }
67
61
  end
68
62
 
69
63
  delete '/:resource/?' do
70
- webtry(lambda { json @resource.all.delete })
64
+ webtry { json @resource.all.delete }
71
65
  end
72
66
 
73
- #NESTED
74
- get '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
75
- webtry(lambda { json @resource1.find(id1).send(resource2) })
67
+ # NESTED
68
+ get '/:resource1/:id1/:resource2/?' do |_resource1, id1, resource2|
69
+ webtry { json @resource1.find(id1).send(resource2) }
76
70
  end
77
71
 
78
- post '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
79
- webtry(lambda {
80
- parent_resource = @resource1.find(id1)
81
- child_resource = @resource2.new(request.body.read)
82
- child_resource.save!
83
- parent_resource.send(resource2) << child_resource
84
- json child_resource
85
- })
72
+ post '/:resource1/:id1/:resource2/?' do |_resource1, id1, resource2|
73
+ webtry do
74
+ parent_resource = @resource1.find(id1)
75
+ child_resource = @resource2.new(request.params)
76
+ child_resource.save!
77
+ parent_resource.send(resource2) << child_resource
78
+ json child_resource
79
+ end
86
80
  end
87
81
 
88
- delete '/:resource1/:id1/:resource2/?' do | resource1, id1, resource2 |
89
- webtry(lambda { json @resource1.find(id1).send(resource2).all.delete })
82
+ delete '/:resource1/:id1/:resource2/?' do |_resource1, id1, resource2|
83
+ webtry { json @resource1.find(id1).send(resource2).all.delete }
90
84
  end
91
85
 
92
- def webtry(block)
93
- begin
94
- status 200
95
- block.call
96
- rescue Mongoid::Errors::DocumentNotFound
97
- status 404
98
- rescue
99
- status 500
100
- end
86
+ def webtry
87
+ status 200
88
+ yield
89
+ rescue Mongoid::Errors::DocumentNotFound
90
+ status 404
91
+ rescue
92
+ status 500
101
93
  end
102
94
  end
@@ -1,3 +1,3 @@
1
1
  module AutoApi
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phillip R. Spitler (Phil)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-19 00:00:00.000000000 Z
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra