rory 0.5.0 → 0.5.1

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: 55512769abc5665735db9609c9d1dcc541b37e9f
4
- data.tar.gz: c60a1ce6cec4f650cbfee24e77462d8461a01d8d
3
+ metadata.gz: f460afe6851a0ebbff77cd5459bf5dc2e4637c6c
4
+ data.tar.gz: f22a44a3d6141f2db251415ce18724e172fb9ca1
5
5
  SHA512:
6
- metadata.gz: ce94a5c75d20071fc852dcbc71f4c887456ebe76cada4e62e96ae41c1578115229889cd00c83d7187f5e2e159d8047ede5f1d9f0bd46e988f85e76ff489eb27c
7
- data.tar.gz: ff2b069e3e5a65f07610ecd9b32fdea59264e29e7c009f34312a9eacdfbad5217d0db62a7f37ecd1135c0219953d56c8ff2447ec713c8bf848ae2e28ba15b433
6
+ metadata.gz: 101bc4e7b9de76e550b74eae963664d84f04a70e497d3016f6f117b74ef0140a9939362b6a338fadb6b82558b430682a6a677550c9513341b103723ab176325a
7
+ data.tar.gz: afc2f14d5c59c4e4ba834813b3c2e82f5f6cbec9f8518c8a7f29e90c28dc9dc135904e70aa79da71189e5765b9e9826d24c5faba2dcae2766be8f5b94cafe227
@@ -107,7 +107,7 @@ module Rory
107
107
  end
108
108
 
109
109
  def generate_json_from_object(object, opts = {})
110
- object.to_json
110
+ Rory::Support.encode_as_json(object)
111
111
  end
112
112
 
113
113
  def generate_for_render(opts = {})
data/lib/rory/support.rb CHANGED
@@ -29,5 +29,22 @@ module Rory
29
29
  require file
30
30
  end
31
31
  end
32
+
33
+ def encode_as_json(object)
34
+ hashed = if object.is_a?(Array)
35
+ object.map { |o| try_to_hash(o) }
36
+ else
37
+ try_to_hash(object)
38
+ end
39
+ hashed.to_json
40
+ end
41
+
42
+ def try_to_hash(object)
43
+ if object.respond_to?(:to_hash)
44
+ object.to_hash
45
+ else
46
+ object
47
+ end
48
+ end
32
49
  end
33
50
  end
data/lib/rory/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rory
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -1,4 +1,6 @@
1
1
  describe Rory::Controller do
2
+ subject { Rory::Controller.new(@request, @routing) }
3
+
2
4
  before :each do
3
5
  @routing = {
4
6
  :route => Rory::Route.new('', :to => 'test#letsgo')
@@ -18,15 +20,13 @@ describe Rory::Controller do
18
20
 
19
21
  describe '#layout' do
20
22
  it 'defaults to nil' do
21
- controller = Rory::Controller.new(@request, @routing)
22
- expect(controller.layout).to be_nil
23
+ expect(subject.layout).to be_nil
23
24
  end
24
25
  end
25
26
 
26
27
  describe '#params' do
27
28
  it 'returns params from request, converted for indifferent key access' do
28
- controller = Rory::Controller.new(@request, @routing)
29
- expect(controller.params).to eq({
29
+ expect(subject.params).to eq({
30
30
  'violet' => 'invisibility',
31
31
  'dash' => 'superspeed',
32
32
  :violet => 'invisibility',
@@ -39,8 +39,7 @@ describe Rory::Controller do
39
39
  it "delegates to dispatcher from request" do
40
40
  @routing[:dispatcher] = dispatcher = double
41
41
  expect(dispatcher).to receive(:redirect).with(:whatever)
42
- controller = Rory::Controller.new(@request, @routing)
43
- controller.redirect(:whatever)
42
+ subject.redirect(:whatever)
44
43
  end
45
44
  end
46
45
 
@@ -48,117 +47,109 @@ describe Rory::Controller do
48
47
  it "delegates to dispatcher from request" do
49
48
  @routing[:dispatcher] = dispatcher = double
50
49
  expect(dispatcher).to receive(:render_not_found)
51
- controller = Rory::Controller.new(@request, @routing)
52
- controller.render_not_found
50
+ subject.render_not_found
53
51
  end
54
52
  end
55
53
 
56
54
  describe "#base_path" do
57
55
  it "returns script_name from request" do
58
- controller = Rory::Controller.new(@request, @routing)
59
- expect(controller.base_path).to eq 'script_root'
56
+ expect(subject.base_path).to eq 'script_root'
60
57
  end
61
58
  end
62
59
 
63
60
  describe "#present" do
64
- it "calls filters and action from route if exists on controller" do
65
- controller = FilteredController.new(@request, @routing)
66
- [:pickle_something, :make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
67
- expect(controller).to receive(m).ordered
61
+ context "with filters" do
62
+ subject { FilteredController.new(@request, @routing) }
63
+
64
+ it "calls filters and action from route if exists on controller" do
65
+ [:pickle_something, :make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
66
+ expect(subject).to receive(m).ordered
67
+ end
68
+ subject.present
68
69
  end
69
- controller.present
70
- end
71
70
 
72
- it "short circuits if a before_action generates a response" do
73
- controller = FilteredController.new(@request, @routing)
74
- def controller.pickle_something
75
- @response = 'stuff'
76
- end
77
- [:make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
78
- expect(controller).to receive(m).never
71
+ it "short circuits if a before_action generates a response" do
72
+ def subject.pickle_something
73
+ @response = 'stuff'
74
+ end
75
+ [:make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
76
+ expect(subject).to receive(m).never
77
+ end
78
+ subject.present
79
79
  end
80
- controller.present
81
- end
82
80
 
83
- it "does not short circuit after_actions if action generates response" do
84
- controller = FilteredController.new(@request, @routing)
85
- def controller.letsgo
86
- @response = 'stuff'
81
+ it "does not short circuit after_actions if action generates response" do
82
+ def subject.letsgo
83
+ @response = 'stuff'
84
+ end
85
+ expect(subject).to receive(:pickle_something).ordered
86
+ expect(subject).to receive(:make_it_tasty).ordered
87
+ expect(subject).to receive(:letsgo).ordered.and_call_original
88
+ expect(subject).to receive(:rub_tummy).ordered
89
+ expect(subject).to receive(:sleep).ordered
90
+ expect(subject).to receive(:render).never
91
+ subject.present
87
92
  end
88
- expect(controller).to receive(:pickle_something).ordered
89
- expect(controller).to receive(:make_it_tasty).ordered
90
- expect(controller).to receive(:letsgo).ordered.and_call_original
91
- expect(controller).to receive(:rub_tummy).ordered
92
- expect(controller).to receive(:sleep).ordered
93
- expect(controller).to receive(:render).never
94
- controller.present
95
- end
96
93
 
97
- it "doesn't try to call action from route if nonexistent on controller" do
98
- controller = FilteredController.new(@request, @routing)
99
- allow(@routing[:route]).to receive(:action).and_return('no worries')
100
- [:pickle_something, :make_it_tasty, :rub_tummy, :sleep, :render].each do |m|
101
- expect(controller).to receive(m).ordered
94
+ it "doesn't try to call action from route if nonexistent on controller" do
95
+ allow(@routing[:route]).to receive(:action).and_return('no worries')
96
+ [:pickle_something, :make_it_tasty, :rub_tummy, :sleep, :render].each do |m|
97
+ expect(subject).to receive(m).ordered
98
+ end
99
+ expect { subject.present }.not_to raise_error
102
100
  end
103
- expect { controller.present }.not_to raise_error
104
- end
105
101
 
106
- it "filters before and after actions on :only and :except" do
107
- @routing[:route] = Rory::Route.new('', :to => 'test#eat')
108
- controller = FilteredController.new(@request, @routing)
109
- expect(controller).to receive(:make_it_tasty).ordered
110
- expect(controller).to receive(:make_it_nutritious).ordered
111
- expect(controller).to receive(:eat).ordered
112
- expect(controller).to receive(:rub_tummy).ordered
113
- expect(controller).to receive(:smile).ordered
114
- expect(controller).to receive(:sleep).never
115
- expect(controller).to receive(:render).ordered
116
- controller.present
117
- end
102
+ it "filters before and after actions on :only and :except" do
103
+ @routing[:route] = Rory::Route.new('', :to => 'test#eat')
104
+ expect(subject).to receive(:make_it_tasty).ordered
105
+ expect(subject).to receive(:make_it_nutritious).ordered
106
+ expect(subject).to receive(:eat).ordered
107
+ expect(subject).to receive(:rub_tummy).ordered
108
+ expect(subject).to receive(:smile).ordered
109
+ expect(subject).to receive(:sleep).never
110
+ expect(subject).to receive(:render).ordered
111
+ subject.present
112
+ end
118
113
 
119
- it "filters before and after actions on :if and :unless" do
120
- @routing[:route] = Rory::Route.new('', :to => 'test#eat')
121
- @request = double('Rack::Request', {
122
- :params => { 'horses' => 'missing' },
123
- :script_name => 'script_root'
124
- })
125
- controller = FilteredController.new(@request, @routing)
126
- expect(controller).to receive(:make_it_tasty).never
127
- expect(controller).to receive(:make_it_nutritious).ordered
128
- expect(controller).to receive(:eat).ordered
129
- expect(controller).to receive(:rub_tummy).never
130
- expect(controller).to receive(:smile).ordered
131
- expect(controller).to receive(:sleep).never
132
- expect(controller).to receive(:render).ordered
133
- controller.present
114
+ it "filters before and after actions on :if and :unless" do
115
+ @routing[:route] = Rory::Route.new('', :to => 'test#eat')
116
+ @request = double('Rack::Request', {
117
+ :params => { 'horses' => 'missing' },
118
+ :script_name => 'script_root'
119
+ })
120
+ expect(subject).to receive(:make_it_tasty).never
121
+ expect(subject).to receive(:make_it_nutritious).ordered
122
+ expect(subject).to receive(:eat).ordered
123
+ expect(subject).to receive(:rub_tummy).never
124
+ expect(subject).to receive(:smile).ordered
125
+ expect(subject).to receive(:sleep).never
126
+ expect(subject).to receive(:render).ordered
127
+ subject.present
128
+ end
134
129
  end
135
130
 
136
131
  it "just returns a response if @response exists" do
137
- controller = Rory::Controller.new(@request, @routing)
138
- controller.instance_variable_set(:@response, 'Forced response')
139
- expect(controller.present).to eq('Forced response')
132
+ subject.instance_variable_set(:@response, 'Forced response')
133
+ expect(subject.present).to eq('Forced response')
140
134
  end
141
135
 
142
136
  it "sends a previously set @body to render" do
143
- controller = Rory::Controller.new(@request, @routing)
144
- controller.instance_variable_set(:@body, 'Forced body')
145
- allow(controller).to receive(:render).with(:body => 'Forced body').and_return("Forced response")
146
- expect(controller.present).to eq('Forced response')
137
+ subject.instance_variable_set(:@body, 'Forced body')
138
+ allow(subject).to receive(:render).with(:body => 'Forced body').and_return("Forced response")
139
+ expect(subject.present).to eq('Forced response')
147
140
  end
148
141
 
149
142
  it "returns the result of render" do
150
- controller = Rory::Controller.new(@request, @routing)
151
- allow(controller).to receive(:render).with(:body => nil).and_return("The response")
152
- expect(controller.present).to eq('The response')
143
+ allow(subject).to receive(:render).with(:body => nil).and_return("The response")
144
+ expect(subject.present).to eq('The response')
153
145
  end
154
146
  end
155
147
 
156
148
  describe "#render" do
157
149
  it "returns the result of #generate_body_for_render as a rack response" do
158
- controller = Rory::Controller.new(@request, @routing)
159
- allow(controller).to receive(:default_content_type).and_return("a prison")
160
- allow(controller).to receive(:generate_for_render).and_return("Valoop!")
161
- expect(controller.render).to eq([
150
+ allow(subject).to receive(:default_content_type).and_return("a prison")
151
+ allow(subject).to receive(:generate_for_render).and_return("Valoop!")
152
+ expect(subject.render).to eq([
162
153
  200,
163
154
  {'Content-type' => 'a prison', 'charset' => 'UTF-8'},
164
155
  ["Valoop!"]
@@ -166,9 +157,8 @@ describe Rory::Controller do
166
157
  end
167
158
 
168
159
  it "returns given body as a rack response" do
169
- controller = Rory::Controller.new(@request, @routing)
170
- allow(controller).to receive(:default_content_type).and_return("snooj/woz")
171
- expect(controller.render(:body => 'Forced body')).to eq([
160
+ allow(subject).to receive(:default_content_type).and_return("snooj/woz")
161
+ expect(subject.render(:body => 'Forced body')).to eq([
172
162
  200,
173
163
  {'Content-type' => 'snooj/woz', 'charset' => 'UTF-8'},
174
164
  ["Forced body"]
@@ -178,58 +168,51 @@ describe Rory::Controller do
178
168
 
179
169
  describe "#json_requested?" do
180
170
  it "delegates to dispatcher" do
181
- controller = Rory::Controller.new(@request, @routing)
182
- allow(controller).to receive(:dispatcher).and_return(double(:json_requested? => :snakes))
183
- expect(controller.json_requested?).to eq(:snakes)
171
+ allow(subject).to receive(:dispatcher).and_return(double(:json_requested? => :snakes))
172
+ expect(subject.json_requested?).to eq(:snakes)
184
173
  end
185
174
  end
186
175
 
187
176
  describe "#generate_for_render" do
188
177
  it "renders and returns the default template if not json" do
189
- controller = Rory::Controller.new(@request, @routing)
190
- allow(controller).to receive(:generate_body_from_template).with("test/letsgo", {}).and_return("Whee")
191
- expect(controller.generate_for_render).to eq("Whee")
178
+ allow(subject).to receive(:generate_body_from_template).with("test/letsgo", {}).and_return("Whee")
179
+ expect(subject.generate_for_render).to eq("Whee")
192
180
  end
193
181
 
194
182
  it "renders and returns the given template if not json" do
195
- controller = Rory::Controller.new(@request, @routing)
196
- allow(controller).to receive(:generate_body_from_template).with("engines", {}).and_return("Oh dear")
197
- expect(controller.generate_for_render(:template => 'engines')).to eq("Oh dear")
183
+ allow(subject).to receive(:generate_body_from_template).with("engines", {}).and_return("Oh dear")
184
+ expect(subject.generate_for_render(:template => 'engines')).to eq("Oh dear")
198
185
  end
199
186
 
200
187
  it "returns json version of given json object if json" do
201
- controller = Rory::Controller.new(@request, @routing)
202
- allow(controller).to receive(:generate_json_from_object).with(:an_object, {}).and_return("Oh dear")
203
- expect(controller.generate_for_render(:json => :an_object)).to eq("Oh dear")
188
+ allow(subject).to receive(:generate_json_from_object).with(:an_object, {}).and_return("Oh dear")
189
+ expect(subject.generate_for_render(:json => :an_object)).to eq("Oh dear")
204
190
  end
205
191
  end
206
192
 
207
193
  describe "#generate_json_from_object" do
208
- it "returns given object as json" do
209
- controller = Rory::Controller.new(@request, @routing)
210
- object = double(:to_json => :jsonified)
211
- expect(controller.generate_json_from_object(object)).to eq(:jsonified)
194
+ it "encodes given object as json" do
195
+ allow(Rory::Support).to receive(:encode_as_json).with(:foo).and_return(:encoded_foo)
196
+ expect(subject.generate_json_from_object(:foo)).to eq(:encoded_foo)
212
197
  end
213
198
  end
214
199
 
215
200
  describe "#generate_body_from_template" do
216
201
  it "returns rendered template with given name" do
217
- controller = Rory::Controller.new(@request, @routing)
218
- expect(controller.generate_body_from_template('test/letsgo')).to eq("Let's go content")
202
+ expect(subject.generate_body_from_template('test/letsgo')).to eq("Let's go content")
219
203
  end
220
204
 
221
205
  it "returns renderer output" do
222
- controller = Rory::Controller.new(@request, @routing)
223
206
  allow(Rory::Renderer).to receive(:new).
224
- with('not/real', controller.default_renderer_options).
207
+ with('not/real', subject.default_renderer_options).
225
208
  and_return(double('Renderer', :render => 'Here ya go'))
226
- expect(controller.generate_body_from_template('not/real')).to eq('Here ya go')
209
+ expect(subject.generate_body_from_template('not/real')).to eq('Here ya go')
227
210
  end
228
211
 
229
212
  it "passes layout, exposed locals, and app to renderer" do
230
- controller = Rory::Controller.new(@request, @routing, :scooby)
231
- controller.expose(:a => 1)
232
- allow(controller).to receive(:layout).and_return('pretend')
213
+ subject = Rory::Controller.new(@request, @routing, :scooby)
214
+ subject.expose(:a => 1)
215
+ allow(subject).to receive(:layout).and_return('pretend')
233
216
  renderer_options = {
234
217
  :layout => 'pretend',
235
218
  :locals => { :a => 1 },
@@ -239,21 +222,19 @@ describe Rory::Controller do
239
222
  allow(Rory::Renderer).to receive(:new).
240
223
  with('also/fake', renderer_options).
241
224
  and_return(double('Renderer', :render => 'Scamazing!'))
242
- expect(controller.generate_body_from_template('also/fake')).to eq('Scamazing!')
225
+ expect(subject.generate_body_from_template('also/fake')).to eq('Scamazing!')
243
226
  end
244
227
  end
245
228
 
246
229
  describe "#default_content_type" do
247
230
  it "returns 'text/html' if not json" do
248
- controller = Rory::Controller.new(@request, @routing)
249
- allow(controller).to receive(:json_requested?).and_return(false)
250
- expect(controller.default_content_type).to eq('text/html')
231
+ allow(subject).to receive(:json_requested?).and_return(false)
232
+ expect(subject.default_content_type).to eq('text/html')
251
233
  end
252
234
 
253
235
  it "returns 'application/json' if json requested" do
254
- controller = Rory::Controller.new(@request, @routing)
255
- allow(controller).to receive(:json_requested?).and_return(true)
256
- expect(controller.default_content_type).to eq('application/json')
236
+ allow(subject).to receive(:json_requested?).and_return(true)
237
+ expect(subject.default_content_type).to eq('application/json')
257
238
  end
258
239
  end
259
240
  end
@@ -64,4 +64,24 @@ describe Rory::Support do
64
64
  expect(described_class.tokenize(:yourFaceIsNice)).to eq('your_face_is_nice')
65
65
  end
66
66
  end
67
+
68
+ describe ".encode_as_json" do
69
+ it "returns given object as json" do
70
+ object = double(:to_json => :jsonified)
71
+ expect(described_class.encode_as_json(object)).to eq(:jsonified)
72
+ end
73
+
74
+ it "calls to_hash first if object responds to it" do
75
+ object = double(:to_hash => { 'april' => 'friday' })
76
+ expect(described_class.encode_as_json(object)).to eq({ 'april' => 'friday' }.to_json)
77
+ end
78
+
79
+ it "converts each member of an array" do
80
+ object = [
81
+ double(:to_hash => :smurf),
82
+ double(:to_hash => :nerf)
83
+ ]
84
+ expect(described_class.encode_as_json(object)).to eq([:smurf, :nerf].to_json)
85
+ end
86
+ end
67
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Gadad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-12 00:00:00.000000000 Z
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack