yodatra 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -1
- data/lib/yodatra/api_formatter.rb +2 -0
- data/lib/yodatra/base.rb +1 -0
- data/lib/yodatra/{model_controller.rb → models_controller.rb} +26 -10
- data/lib/yodatra/utils.rb +16 -0
- data/lib/yodatra/version.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- data/spec/unit/{model_controller_spec.rb → models_controller_spec.rb} +24 -27
- metadata +6 -5
data/Gemfile
CHANGED
data/lib/yodatra/base.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Yodatra
|
2
|
-
class
|
2
|
+
class ModelsController < Sinatra::Base
|
3
3
|
|
4
4
|
before do
|
5
5
|
content_type 'application/json'
|
@@ -8,7 +8,7 @@ module Yodatra
|
|
8
8
|
READ_ALL = :read_all
|
9
9
|
get "/*s" do
|
10
10
|
no_route if disabled? READ_ALL
|
11
|
-
model_name.constantize.all.to_json
|
11
|
+
model_name.constantize.all.as_json(read_scope).to_json
|
12
12
|
end
|
13
13
|
|
14
14
|
READ_ONE = :read
|
@@ -16,17 +16,17 @@ module Yodatra
|
|
16
16
|
no_route if disabled? READ_ONE
|
17
17
|
|
18
18
|
@one = model_name.constantize.find params[:id]
|
19
|
-
@one.to_json
|
19
|
+
@one.as_json(read_scope).to_json
|
20
20
|
end
|
21
21
|
|
22
22
|
CREATE_ONE = :create
|
23
23
|
post "/*s" do
|
24
24
|
no_route if disabled? CREATE_ONE
|
25
25
|
|
26
|
-
@one = model_name.constantize.new
|
26
|
+
@one = model_name.constantize.new self.send("#{model_name.underscore}_params".to_sym)
|
27
27
|
|
28
28
|
if @one.save
|
29
|
-
@one.to_json
|
29
|
+
@one.as_json(read_scope).to_json
|
30
30
|
else
|
31
31
|
status 400
|
32
32
|
@one.errors.full_messages.to_json
|
@@ -40,7 +40,7 @@ module Yodatra
|
|
40
40
|
@one = model_name.constantize.find params[:id]
|
41
41
|
|
42
42
|
if !@one.nil? && @one.update_attributes(params)
|
43
|
-
@one.to_json
|
43
|
+
@one.as_json(read_scope).to_json
|
44
44
|
else
|
45
45
|
status 400
|
46
46
|
if !@one.nil?
|
@@ -58,25 +58,41 @@ module Yodatra
|
|
58
58
|
@one = model_name.constantize.find params[:id]
|
59
59
|
|
60
60
|
if @one.destroy
|
61
|
-
@one.to_json
|
61
|
+
@one.as_json(read_scope).to_json
|
62
62
|
else
|
63
63
|
status 400
|
64
64
|
@one.errors.full_messages.to_json
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
+
class << self
|
69
|
+
def model_name
|
70
|
+
self.name.split('::').last.gsub(/sController/, '')
|
71
|
+
end
|
72
|
+
|
73
|
+
def route_name
|
74
|
+
self.model_name.underscore
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
68
78
|
private
|
69
79
|
|
70
|
-
|
80
|
+
# read_scope defaults to all attrs of the model
|
81
|
+
def read_scope
|
82
|
+
{}
|
83
|
+
end
|
84
|
+
|
85
|
+
# create/update scope defaults to all data given in the POST/PUT
|
86
|
+
define_method "#{model_name.underscore}_params".to_sym do
|
71
87
|
params
|
72
88
|
end
|
73
89
|
|
74
90
|
def model_name
|
75
|
-
self.class.
|
91
|
+
self.class.model_name
|
76
92
|
end
|
77
93
|
|
78
94
|
def route_name
|
79
|
-
self.
|
95
|
+
self.class.route_name
|
80
96
|
end
|
81
97
|
|
82
98
|
def disabled? key
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Yodatra
|
2
|
+
class Utils
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
class String
|
7
|
+
def underscore
|
8
|
+
word = self.dup
|
9
|
+
word.gsub!(/::/, '/')
|
10
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
11
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
12
|
+
word.tr!("-", "_")
|
13
|
+
word.downcase!
|
14
|
+
word
|
15
|
+
end
|
16
|
+
end
|
data/lib/yodatra/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -7,14 +7,14 @@ require 'rack/test'
|
|
7
7
|
require 'rspec'
|
8
8
|
|
9
9
|
require File.expand_path '../../lib/yodatra.rb', __FILE__
|
10
|
-
require File.expand_path '../../lib/yodatra/
|
10
|
+
require File.expand_path '../../lib/yodatra/models_controller.rb', __FILE__
|
11
11
|
|
12
12
|
module RSpecMixin
|
13
13
|
include Rack::Test::Methods
|
14
14
|
def app
|
15
15
|
Sinatra.new {
|
16
16
|
use Yodatra::Base
|
17
|
-
use Yodatra::
|
17
|
+
use Yodatra::ModelsController
|
18
18
|
}
|
19
19
|
end
|
20
20
|
end
|
@@ -1,13 +1,11 @@
|
|
1
1
|
require File.expand_path '../../spec_helper.rb', __FILE__
|
2
|
-
require 'mocha/setup'
|
3
|
-
require 'mocha/api'
|
4
2
|
|
5
3
|
# Mock model constructed for the tests
|
6
|
-
class
|
4
|
+
class Model
|
7
5
|
ALL = %w(a b c)
|
8
6
|
class << self
|
9
|
-
def all; ALL.map{ |e|
|
10
|
-
def find(id);
|
7
|
+
def all; ALL.map{ |e| Model.new({:data=> e }) }; end
|
8
|
+
def find(id); Model.new({:data => ALL[id.to_i]}); end
|
11
9
|
end
|
12
10
|
def initialize(param); @data = param[:data]; end
|
13
11
|
def save
|
@@ -30,30 +28,29 @@ class TestModel
|
|
30
28
|
end
|
31
29
|
|
32
30
|
describe 'Model controller' do
|
31
|
+
|
33
32
|
before do
|
34
|
-
@any_model = 'TestModel'
|
35
33
|
@errors = ['error']
|
36
|
-
|
37
|
-
Array.any_instance.stubs(:full_messages).returns(@errors)
|
34
|
+
allow_any_instance_of(Array).to receive(:full_messages).and_return(@errors)
|
38
35
|
end
|
39
36
|
|
40
37
|
describe 'Getting a collection of the Model' do
|
41
38
|
context 'default' do
|
42
39
|
it 'should have a GET all route' do
|
43
|
-
get '/
|
40
|
+
get '/models'
|
44
41
|
|
45
42
|
last_response.should be_ok
|
46
|
-
expect(last_response.body).to eq(
|
43
|
+
expect(last_response.body).to eq(Model::ALL.map{|e| {:data => e} }.to_json)
|
47
44
|
end
|
48
45
|
end
|
49
46
|
context 'forced GET all route disabled' do
|
50
47
|
before do
|
51
|
-
class Yodatra::
|
48
|
+
class Yodatra::ModelsController
|
52
49
|
disable :read_all
|
53
50
|
end
|
54
51
|
end
|
55
52
|
it 'should fail with no route available' do
|
56
|
-
get '/
|
53
|
+
get '/models'
|
57
54
|
|
58
55
|
last_response.should_not be_ok
|
59
56
|
end
|
@@ -61,19 +58,19 @@ describe 'Model controller' do
|
|
61
58
|
end
|
62
59
|
describe 'getting an specific Model instance' do
|
63
60
|
it 'should have a GET one route' do
|
64
|
-
get '/
|
61
|
+
get '/models/2'
|
65
62
|
|
66
63
|
last_response.should be_ok
|
67
64
|
expect(last_response.body).to eq({ :data => 'c'}.to_json)
|
68
65
|
end
|
69
66
|
context 'forced GET one route disabled' do
|
70
67
|
before do
|
71
|
-
class Yodatra::
|
68
|
+
class Yodatra::ModelsController
|
72
69
|
disable :read
|
73
70
|
end
|
74
71
|
end
|
75
72
|
it 'should fail with no route available' do
|
76
|
-
get '/
|
73
|
+
get '/models/1'
|
77
74
|
|
78
75
|
last_response.should_not be_ok
|
79
76
|
end
|
@@ -83,8 +80,8 @@ describe 'Model controller' do
|
|
83
80
|
context 'with correct model params' do
|
84
81
|
it 'adds creates an instance, saves it and succeed' do
|
85
82
|
expect{
|
86
|
-
post '/
|
87
|
-
}.to change(
|
83
|
+
post '/models', {:data => 'd'}
|
84
|
+
}.to change(Model::ALL, :length).by(1)
|
88
85
|
|
89
86
|
last_response.should be_ok
|
90
87
|
end
|
@@ -92,8 +89,8 @@ describe 'Model controller' do
|
|
92
89
|
context 'with incorrect params' do
|
93
90
|
it 'doesn t create an instance and fails' do
|
94
91
|
expect{
|
95
|
-
post '/
|
96
|
-
}.to change(
|
92
|
+
post '/models', {}
|
93
|
+
}.to change(Model::ALL, :length).by(0)
|
97
94
|
|
98
95
|
last_response.should_not be_ok
|
99
96
|
expect(last_response.body).to eq(@errors.to_json)
|
@@ -101,12 +98,12 @@ describe 'Model controller' do
|
|
101
98
|
end
|
102
99
|
context 'when the creation route is disabled' do
|
103
100
|
before do
|
104
|
-
class Yodatra::
|
101
|
+
class Yodatra::ModelsController
|
105
102
|
disable :create
|
106
103
|
end
|
107
104
|
end
|
108
105
|
it 'should fail with no route available' do
|
109
|
-
post '/
|
106
|
+
post '/models', {:data => 'd'}
|
110
107
|
|
111
108
|
last_response.should_not be_ok
|
112
109
|
end
|
@@ -116,8 +113,8 @@ describe 'Model controller' do
|
|
116
113
|
context 'targeting an existing instance' do
|
117
114
|
it 'deletes the instance and succeed' do
|
118
115
|
expect{
|
119
|
-
delete '/
|
120
|
-
}.to change(
|
116
|
+
delete '/models/1'
|
117
|
+
}.to change(Model::ALL, :length).by(-1)
|
121
118
|
|
122
119
|
last_response.should be_ok
|
123
120
|
end
|
@@ -125,20 +122,20 @@ describe 'Model controller' do
|
|
125
122
|
context 'targeting a not existing instance' do
|
126
123
|
it 'does not delete any instance and fails' do
|
127
124
|
expect{
|
128
|
-
delete '/
|
129
|
-
}.to change(
|
125
|
+
delete '/models/6'
|
126
|
+
}.to change(Model::ALL, :length).by(0)
|
130
127
|
|
131
128
|
last_response.should_not be_ok
|
132
129
|
end
|
133
130
|
end
|
134
131
|
context 'when the deletion route is disabled' do
|
135
132
|
before do
|
136
|
-
class Yodatra::
|
133
|
+
class Yodatra::ModelsController
|
137
134
|
disable :delete
|
138
135
|
end
|
139
136
|
end
|
140
137
|
it 'should fail with no route available' do
|
141
|
-
delete '/
|
138
|
+
delete '/models/2'
|
142
139
|
|
143
140
|
last_response.should_not be_ok
|
144
141
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yodatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -138,10 +138,11 @@ files:
|
|
138
138
|
- lib/yodatra/crypto.rb
|
139
139
|
- lib/yodatra/initializers.rb
|
140
140
|
- lib/yodatra/logger.rb
|
141
|
-
- lib/yodatra/
|
141
|
+
- lib/yodatra/models_controller.rb
|
142
|
+
- lib/yodatra/utils.rb
|
142
143
|
- lib/yodatra/version.rb
|
143
144
|
- spec/spec_helper.rb
|
144
|
-
- spec/unit/
|
145
|
+
- spec/unit/models_controller_spec.rb
|
145
146
|
- yodatra.gemspec
|
146
147
|
homepage: http://squareteam.github.io/yodatra
|
147
148
|
licenses:
|
@@ -176,4 +177,4 @@ signing_key:
|
|
176
177
|
specification_version: 3
|
177
178
|
summary: Classy backend development with the speed of Sinatra and the power of ActiveRecord
|
178
179
|
test_files:
|
179
|
-
- spec/unit/
|
180
|
+
- spec/unit/models_controller_spec.rb
|