alephant-preview 0.4.0 → 0.5.0

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: cb29527d9bf9c048e248aa1aacb116c25db7f0ea
4
- data.tar.gz: 2fd1eea531051f54534cb1074c338e3854c368c4
3
+ metadata.gz: c87e2e7b9c82a52f83127e9357ef49697ba5ce50
4
+ data.tar.gz: 22cb67ba4b3fb81dc64de8bbab95b1cc6c68f424
5
5
  SHA512:
6
- metadata.gz: 41fceee23cfb06b4a574c71cb7576549cbb9dbcf6c73a293c3855bd5589c9890773702303396f6c55cc5a5fdd8cdd14e6341893465493373b60f2b26ba0cf91d
7
- data.tar.gz: 8976cf2f8131f63a057bf99c3d9d82c1da9644c32861b296752aec66f0d9ece770619bbb611a6eb39ec3d0547cafe07cfcffc7bad2b2fa26e228068ea5a87233
6
+ metadata.gz: 2eba39230ac965cc3b43e5c4ecbec36b8f7d1bda50f82ad0d8e1649254e4b8932fe495aff1401bd4dbb929b334c45c0f6fc70cb02fa3ab7eb7c7eb956ebb47d4
7
+ data.tar.gz: 74623ea9364a0dcb52534f56b374ae5bfc25e948b964e5cb812317f3a68a32277f6245f2e78cd52e88120e9587ec5cf2ef7df1f9a28706b3455b4371c2ab2853
@@ -32,16 +32,23 @@ module Alephant
32
32
  end
33
33
 
34
34
  get "/preview/:id/:template/:region/?:fixture?" do
35
+ response["X-Sequence"] = sequence_id
36
+
35
37
  render_preview
36
38
  end
37
39
 
38
40
  get "/component/:template/?:fixture?" do
39
41
  params["id"] = find_id_from_template params["template"]
40
42
  params["fixture"] = "responsive" unless params["fixture"]
43
+
44
+ response["X-Sequence"] = sequence_id
45
+
41
46
  render_component
42
47
  end
43
48
 
44
49
  get "/component/:id/:template/?:fixture?" do
50
+ response["X-Sequence"] = sequence_id
51
+
45
52
  render_component
46
53
  end
47
54
 
@@ -100,20 +107,41 @@ module Alephant
100
107
  end
101
108
 
102
109
  def render_component
103
- view_mapper.generate(fixture_data)[template].render
110
+ view_mapper.generate(fixture_data)[template].render.tap do |content|
111
+ response["Content-Type"] = get_content_type(content)
112
+ end
104
113
  end
105
114
 
106
115
  def render_batch_component
116
+ content = render_component
117
+
107
118
  {
108
- :component => template,
109
- :options => {},
110
- :status => 200,
111
- :body => render_component
119
+ :component => template,
120
+ :options => {},
121
+ :status => 200,
122
+ :body => content,
123
+ :content_type => get_content_type(content),
124
+ :sequence_id => sequence_id
112
125
  }
113
126
  end
114
127
 
115
128
  private
116
129
 
130
+ def sequence_id
131
+ Time.now.to_i
132
+ end
133
+
134
+ def get_content_type(content)
135
+ return "application/json" if is_json?(content)
136
+ "text/html"
137
+ end
138
+
139
+ def is_json?(content)
140
+ JSON.parse(content) && true
141
+ rescue Exception
142
+ false
143
+ end
144
+
117
145
  def request_body
118
146
  JSON.parse(request.body.read, :symbolize_names => true) || {}
119
147
  end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Preview
3
- VERSION = "0.4.0".freeze
3
+ VERSION = "0.5.0".freeze
4
4
  end
5
5
  end
@@ -1,9 +1,9 @@
1
- require "alephant/renderer/views/html"
1
+ require "alephant/renderer/views/json"
2
2
 
3
3
  module MyApp
4
- class Foo < ::Alephant::Renderer::Views::Html
5
- def content
6
- "content"
4
+ class Foo < ::Alephant::Renderer::Views::Json
5
+ def to_h
6
+ { "content" => "as json" }
7
7
  end
8
8
  end
9
9
  end
@@ -6,8 +6,12 @@ describe Alephant::Preview::Server do
6
6
 
7
7
  describe "preview endpoint (GET /preview/{id}/{template}/{region}/{fixture})" do
8
8
  describe "content" do
9
+ expected_time = 123_456_789
10
+
9
11
  context "with valid data" do
10
12
  before(:each) do
13
+ allow(Time).to receive(:now).and_return(expected_time)
14
+
11
15
  get "/preview/#{id}/#{template}/#{region}/#{fixture}"
12
16
  end
13
17
  let (:id) { "foo" }
@@ -15,14 +19,31 @@ describe Alephant::Preview::Server do
15
19
  let (:fixture) { id }
16
20
  let (:region) { "page_region" }
17
21
 
18
- specify { expect(last_response.body).to eq("topcontent\nbottom\n") }
22
+ it "should return correct response" do
23
+ expect(last_response.body).to eq(%(top{"content":"as json"}bottom\n))
24
+ end
25
+
26
+ expected_headers = {
27
+ "Content-Type" => "application/json",
28
+ "Access-Control-Allow-Origin" => "*",
29
+ "X-Sequence" => expected_time,
30
+ "Content-Length" => "31"
31
+ }
32
+
33
+ it "should have correct response headers" do
34
+ expect(last_response.headers).to include(expected_headers)
35
+ end
19
36
  end
20
37
  end
21
38
  end
22
39
 
23
40
  describe "component endpoint (GET /component/{id}/{template}/{fixture})" do
24
41
  describe "content" do
42
+ expected_time = 123_456_789
43
+
25
44
  before(:each) do
45
+ allow(Time).to receive(:now).and_return(expected_time)
46
+
26
47
  get "/component/#{id}/#{template}/#{fixture}"
27
48
  end
28
49
  let (:response) { last_response.body.chomp }
@@ -32,7 +53,20 @@ describe Alephant::Preview::Server do
32
53
  let (:template) { id }
33
54
  let (:fixture) { id }
34
55
 
35
- specify { expect(response).to eq("content") }
56
+ it "should return correct response" do
57
+ expect(response).to eq(%({"content":"as json"}))
58
+ end
59
+
60
+ expected_headers = {
61
+ "Content-Type" => "application/json",
62
+ "Access-Control-Allow-Origin" => "*",
63
+ "X-Sequence" => expected_time,
64
+ "Content-Length" => "21"
65
+ }
66
+
67
+ it "should have correct response headers" do
68
+ expect(last_response.headers).to include(expected_headers)
69
+ end
36
70
  end
37
71
 
38
72
  context "with a data mapper" do
@@ -41,7 +75,20 @@ describe Alephant::Preview::Server do
41
75
  let (:template) { id }
42
76
  let (:fixture) { id }
43
77
 
44
- specify { expect(response).to eq("data mapped content") }
78
+ it "should return data mapped content" do
79
+ expect(response).to eq("data mapped content")
80
+ end
81
+
82
+ expected_headers = {
83
+ "Content-Type" => "text/html",
84
+ "Access-Control-Allow-Origin" => "*",
85
+ "X-Sequence" => expected_time,
86
+ "Content-Length" => "20"
87
+ }
88
+
89
+ it "should have correct response headers" do
90
+ expect(last_response.headers).to include(expected_headers)
91
+ end
45
92
  end
46
93
 
47
94
  context "using multiple fixtures" do
@@ -49,7 +96,20 @@ describe Alephant::Preview::Server do
49
96
  let (:template) { id }
50
97
  let (:fixture) { id }
51
98
 
52
- specify { expect(response).to eq("multiple endpoint data mapped content") }
99
+ it "should return multiple mapped content" do
100
+ expect(response).to eq("multiple endpoint data mapped content")
101
+ end
102
+
103
+ expected_headers = {
104
+ "Content-Type" => "text/html",
105
+ "Access-Control-Allow-Origin" => "*",
106
+ "X-Sequence" => expected_time,
107
+ "Content-Length" => "38"
108
+ }
109
+
110
+ it "should have correct response headers" do
111
+ expect(last_response.headers).to include(expected_headers)
112
+ end
53
113
  end
54
114
  end
55
115
  end
@@ -57,7 +117,11 @@ describe Alephant::Preview::Server do
57
117
 
58
118
  describe 'component batch endpoint (GET /components/batch?components[#{id}]=#{id})' do
59
119
  describe "content" do
120
+ expected_time = 123_456_789
121
+
60
122
  before(:each) do
123
+ allow(Time).to receive(:now).and_return(expected_time)
124
+
61
125
  get "/components/batch?components[#{id}][component]=#{id}&components[#{id}][options][fixture]=#{id}"
62
126
  end
63
127
 
@@ -71,15 +135,19 @@ describe Alephant::Preview::Server do
71
135
  expected = {
72
136
  :components => [
73
137
  {
74
- :component => "foo",
75
- :options => {},
76
- :status => 200,
77
- :body => "content\n"
138
+ :component => "foo",
139
+ :options => {},
140
+ :status => 200,
141
+ :body => %({"content":"as json"}),
142
+ :content_type => "application/json",
143
+ :sequence_id => expected_time
78
144
  }
79
145
  ]
80
146
  }
81
147
 
82
- specify { expect(response).to eq(expected) }
148
+ it "should return correct response" do
149
+ expect(response).to eq(expected)
150
+ end
83
151
  end
84
152
 
85
153
  context "with a data mapper" do
@@ -91,15 +159,19 @@ describe Alephant::Preview::Server do
91
159
  expected = {
92
160
  :components => [
93
161
  {
94
- :component => "bar",
95
- :options => {},
96
- :status => 200,
97
- :body => "data mapped content\n"
162
+ :component => "bar",
163
+ :options => {},
164
+ :status => 200,
165
+ :body => "data mapped content\n",
166
+ :content_type => "text/html",
167
+ :sequence_id => expected_time
98
168
  }
99
169
  ]
100
170
  }
101
171
 
102
- specify { expect(response).to eq(expected) }
172
+ it "should return correct response" do
173
+ expect(response).to eq(expected)
174
+ end
103
175
  end
104
176
 
105
177
  context "using multiple fixtures" do
@@ -110,15 +182,19 @@ describe Alephant::Preview::Server do
110
182
  expected = {
111
183
  :components => [
112
184
  {
113
- :component => "baz",
114
- :options => {},
115
- :status => 200,
116
- :body => "multiple endpoint data mapped content\n"
185
+ :component => "baz",
186
+ :options => {},
187
+ :status => 200,
188
+ :body => "multiple endpoint data mapped content\n",
189
+ :content_type => "text/html",
190
+ :sequence_id => expected_time
117
191
  }
118
192
  ]
119
193
  }
120
194
 
121
- specify { expect(response).to eq(expected) }
195
+ it "should return correct response" do
196
+ expect(response).to eq(expected)
197
+ end
122
198
  end
123
199
  end
124
200
  end
@@ -126,7 +202,11 @@ describe Alephant::Preview::Server do
126
202
 
127
203
  describe "component batch endpoint (POST /components/batch" do
128
204
  describe "content" do
205
+ expected_time = 123_456_789
206
+
129
207
  before(:each) do
208
+ allow(Time).to receive(:now).and_return(expected_time)
209
+
130
210
  post "/components/batch", {
131
211
  :components => [
132
212
  {
@@ -147,15 +227,19 @@ describe Alephant::Preview::Server do
147
227
  expected = {
148
228
  :components => [
149
229
  {
150
- :component => "foo",
151
- :options => {},
152
- :status => 200,
153
- :body => "content\n"
230
+ :component => "foo",
231
+ :options => {},
232
+ :status => 200,
233
+ :body => %({"content":"as json"}),
234
+ :content_type => "application/json",
235
+ :sequence_id => expected_time
154
236
  }
155
237
  ]
156
238
  }
157
239
 
158
- specify { expect(response).to eq(expected) }
240
+ it "should return correct response" do
241
+ expect(response).to eq(expected)
242
+ end
159
243
  end
160
244
 
161
245
  context "with a data mapper" do
@@ -165,15 +249,19 @@ describe Alephant::Preview::Server do
165
249
  expected = {
166
250
  :components => [
167
251
  {
168
- :component => "bar",
169
- :options => {},
170
- :status => 200,
171
- :body => "data mapped content\n"
252
+ :component => "bar",
253
+ :options => {},
254
+ :status => 200,
255
+ :body => "data mapped content\n",
256
+ :content_type => "text/html",
257
+ :sequence_id => expected_time
172
258
  }
173
259
  ]
174
260
  }
175
261
 
176
- specify { expect(response).to eq(expected) }
262
+ it "should return correct response" do
263
+ expect(response).to eq(expected)
264
+ end
177
265
  end
178
266
 
179
267
  context "using multiple fixtures" do
@@ -182,15 +270,19 @@ describe Alephant::Preview::Server do
182
270
  expected = {
183
271
  :components => [
184
272
  {
185
- :component => "baz",
186
- :options => {},
187
- :status => 200,
188
- :body => "multiple endpoint data mapped content\n"
273
+ :component => "baz",
274
+ :options => {},
275
+ :status => 200,
276
+ :body => "multiple endpoint data mapped content\n",
277
+ :content_type => "text/html",
278
+ :sequence_id => expected_time
189
279
  }
190
280
  ]
191
281
  }
192
282
 
193
- specify { expect(response).to eq(expected) }
283
+ it "should return correct response" do
284
+ expect(response).to eq(expected)
285
+ end
194
286
  end
195
287
  end
196
288
  end
@@ -202,7 +294,9 @@ describe Alephant::Preview::Server do
202
294
  end
203
295
 
204
296
  context "status code" do
205
- specify { expect(last_response.status).to eq 200 }
297
+ it "should return ok status code" do
298
+ expect(last_response.status).to eq(200)
299
+ end
206
300
  end
207
301
  end
208
302
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-preview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - BBC News
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-31 00:00:00.000000000 Z
11
+ date: 2016-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -275,7 +275,6 @@ files:
275
275
  - spec/fixtures/components/foo/fixtures/foo.json
276
276
  - spec/fixtures/components/foo/models/foo.rb
277
277
  - spec/fixtures/components/foo/templates/.DS_Store
278
- - spec/fixtures/components/foo/templates/foo.mustache
279
278
  - spec/fixtures/lib/templates/preview.mustache
280
279
  - spec/integration/preview_spec.rb
281
280
  - spec/spec_helper.rb
@@ -318,7 +317,6 @@ test_files:
318
317
  - spec/fixtures/components/foo/fixtures/foo.json
319
318
  - spec/fixtures/components/foo/models/foo.rb
320
319
  - spec/fixtures/components/foo/templates/.DS_Store
321
- - spec/fixtures/components/foo/templates/foo.mustache
322
320
  - spec/fixtures/lib/templates/preview.mustache
323
321
  - spec/integration/preview_spec.rb
324
322
  - spec/spec_helper.rb