prismic.io 1.0.0.rc2 → 1.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/prismic.rb +32 -2
- data/lib/prismic/json_parsers.rb +16 -4
- data/lib/prismic/version.rb +1 -1
- data/spec/json_parsers_spec.rb +20 -12
- data/spec/lesbonneschoses_spec.rb +63 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 406a3849f94ecdfd3516db68fa2ee0d36ecf1c6a
|
4
|
+
data.tar.gz: f6988c94934d3a900af92cf6973dbb3c8d46d135
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f69e94c2b76f58b859ab8fac98c5a7a2ad09f2330fc05e1cf66daebf2d5913ac4d7f514d65f44833a4c10d8e98a3253bd0c6c7716e3bb9351a8218269dfc86b
|
7
|
+
data.tar.gz: 2f09cbb004acd083ffb16ab02dc19ab2e4862d019b105456d658de1f066528c1151a9c41695573dc6d98f7e147bf7eecb4bb9b68d4b55b6939bab041d781aa1e
|
data/Gemfile.lock
CHANGED
data/lib/prismic.rb
CHANGED
@@ -161,7 +161,7 @@ module Prismic
|
|
161
161
|
#
|
162
162
|
# @param ref [Ref, String] The {Ref reference} to use (if not already defined)
|
163
163
|
#
|
164
|
-
# @return [
|
164
|
+
# @return [Documents] The results (array of Document object + pagination specifics)
|
165
165
|
def submit(ref = nil)
|
166
166
|
self.ref(ref) if ref
|
167
167
|
raise NoRefSetException unless @ref
|
@@ -174,7 +174,7 @@ module Prismic
|
|
174
174
|
response = api.http_client.get(form_action, data, 'Accept' => 'application/json')
|
175
175
|
|
176
176
|
if response.code.to_s == "200"
|
177
|
-
Prismic::JsonParser.
|
177
|
+
Prismic::JsonParser.documents_parser(JSON.parse(response.body))
|
178
178
|
else
|
179
179
|
body = JSON.parse(response.body) rescue nil
|
180
180
|
error = body.is_a?(Hash) ? body['error'] : response.body
|
@@ -234,6 +234,36 @@ module Prismic
|
|
234
234
|
alias :repeatable? :repeatable
|
235
235
|
end
|
236
236
|
|
237
|
+
class Documents
|
238
|
+
attr_accessor :page, :results_per_page, :results_size, :total_results_size, :total_pages, :next_page, :prev_page, :results
|
239
|
+
|
240
|
+
def initialize(page, results_per_page, results_size, total_results_size, total_pages, next_page, prev_page, results)
|
241
|
+
@page = page
|
242
|
+
@results_per_page = results_per_page
|
243
|
+
@results_size = results_size
|
244
|
+
@total_results_size = total_results_size
|
245
|
+
@total_pages = total_pages
|
246
|
+
@next_page = next_page
|
247
|
+
@prev_page = prev_page
|
248
|
+
@results = results
|
249
|
+
end
|
250
|
+
|
251
|
+
# Accessing the i-th document in the results
|
252
|
+
def [](i)
|
253
|
+
@results[i]
|
254
|
+
end
|
255
|
+
|
256
|
+
def each(&blk)
|
257
|
+
@results.each(&blk)
|
258
|
+
end
|
259
|
+
include Enumerable # adds map, select, etc
|
260
|
+
|
261
|
+
def length
|
262
|
+
@results.length
|
263
|
+
end
|
264
|
+
alias :size :length
|
265
|
+
end
|
266
|
+
|
237
267
|
class Document
|
238
268
|
attr_accessor :id, :type, :href, :tags, :slugs, :fragments
|
239
269
|
|
data/lib/prismic/json_parsers.rb
CHANGED
@@ -204,14 +204,26 @@ module Prismic
|
|
204
204
|
json['slugs'], fragments)
|
205
205
|
end
|
206
206
|
|
207
|
-
def results_parser(
|
208
|
-
|
209
|
-
raise FormSearchException, "Error : #{result['error']}" if result['error']
|
210
|
-
result['results'].map do |doc|
|
207
|
+
def results_parser(results)
|
208
|
+
results.map do |doc|
|
211
209
|
document_parser(doc)
|
212
210
|
end
|
213
211
|
end
|
214
212
|
|
213
|
+
def documents_parser(documents)
|
214
|
+
raise FormSearchException, "Error : #{documents['error']}" if documents['error']
|
215
|
+
Prismic::Documents.new(
|
216
|
+
documents['page'],
|
217
|
+
documents['results_per_page'],
|
218
|
+
documents['results_size'],
|
219
|
+
documents['total_results_size'],
|
220
|
+
documents['total_pages'],
|
221
|
+
documents['next_page'],
|
222
|
+
documents['prev_page'],
|
223
|
+
results_parser(documents['results'])
|
224
|
+
)
|
225
|
+
end
|
226
|
+
|
215
227
|
private
|
216
228
|
|
217
229
|
def link_parser(json)
|
data/lib/prismic/version.rb
CHANGED
data/spec/json_parsers_spec.rb
CHANGED
@@ -316,18 +316,26 @@ describe 'document_parser' do
|
|
316
316
|
end
|
317
317
|
end
|
318
318
|
|
319
|
-
describe '
|
320
|
-
|
321
|
-
it "accepts
|
322
|
-
@json = JSON.parse('
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
@
|
319
|
+
describe 'documents_parser' do
|
320
|
+
|
321
|
+
it "accepts basic documents response" do
|
322
|
+
@json = JSON.parse('{ "page": 1,
|
323
|
+
"results_per_page": 20,
|
324
|
+
"results_size": 20,
|
325
|
+
"total_results_size": 40,
|
326
|
+
"total_pages": 2,
|
327
|
+
"next_page": "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=2&pageSize=20",
|
328
|
+
"prev_page": null,
|
329
|
+
"results":[]}')
|
330
|
+
@documents = Prismic::JsonParser.documents_parser(@json)
|
331
|
+
@documents.results.should == []
|
332
|
+
@documents.page.should == 1
|
333
|
+
@documents.results_per_page.should == 20
|
334
|
+
@documents.results_size.should == 20
|
335
|
+
@documents.total_results_size.should == 40
|
336
|
+
@documents.total_pages.should == 2
|
337
|
+
@documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=2&pageSize=20"
|
338
|
+
@documents.prev_page.should == nil
|
331
339
|
end
|
332
340
|
|
333
341
|
end
|
@@ -16,25 +16,66 @@ describe 'LesBonnesChoses' do
|
|
16
16
|
describe 'query' do
|
17
17
|
it "queries everything and returns 20 documents" do
|
18
18
|
@api.form("everything").submit(@master_ref).size.should == 20
|
19
|
+
@api.form("everything").submit(@master_ref).results.size.should == 20
|
19
20
|
end
|
20
21
|
|
21
22
|
it "queries macarons (using a predicate) and returns 7 documents" do
|
23
|
+
@api.form("everything")
|
24
|
+
.query(%([[:d = any(document.tags, ["Macaron"])]]))
|
25
|
+
.submit(@master_ref).results.size.should == 7
|
22
26
|
@api.form("everything")
|
23
27
|
.query(%([[:d = any(document.tags, ["Macaron"])]]))
|
24
28
|
.submit(@master_ref).size.should == 7
|
25
29
|
end
|
26
30
|
|
27
31
|
it "queries macarons (using a form) and returns 7 documents" do
|
32
|
+
@api.form("macarons").submit(@master_ref).results.size.should == 7
|
28
33
|
@api.form("macarons").submit(@master_ref).size.should == 7
|
29
34
|
end
|
30
35
|
|
31
36
|
it "queries macarons or cupcakes (using a form + a predicate) and returns 11 documents" do
|
37
|
+
@api.form("products")
|
38
|
+
.query(%([[:d = any(document.tags, ["Cupcake", "Macaron"])]]))
|
39
|
+
.submit(@master_ref).results.size.should == 11
|
32
40
|
@api.form("products")
|
33
41
|
.query(%([[:d = any(document.tags, ["Cupcake", "Macaron"])]]))
|
34
42
|
.submit(@master_ref).size.should == 11
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
46
|
+
describe 'pagination' do
|
47
|
+
it "works in basic cases" do
|
48
|
+
documents = @api.form("everything").submit(@master_ref)
|
49
|
+
documents.page.should == 1
|
50
|
+
documents.results_per_page.should == 20
|
51
|
+
documents.results_size.should == 20
|
52
|
+
documents.total_results_size.should == 40
|
53
|
+
documents.total_pages.should == 2
|
54
|
+
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=2&pageSize=20"
|
55
|
+
documents.prev_page.should == nil
|
56
|
+
end
|
57
|
+
it "works on page 2" do
|
58
|
+
documents = @api.form("everything").page("2").submit(@master_ref)
|
59
|
+
documents.page.should == 2
|
60
|
+
documents.results_per_page.should == 20
|
61
|
+
documents.results_size.should == 20
|
62
|
+
documents.total_results_size.should == 40
|
63
|
+
documents.total_pages.should == 2
|
64
|
+
documents.next_page.should == nil
|
65
|
+
documents.prev_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=1&pageSize=20"
|
66
|
+
end
|
67
|
+
it "works on page 2 with a pagination step of 10" do
|
68
|
+
documents = @api.form("everything").page("2").page_size("10").submit(@master_ref)
|
69
|
+
documents.page.should == 2
|
70
|
+
documents.results_per_page.should == 10
|
71
|
+
documents.results_size.should == 10
|
72
|
+
documents.total_results_size.should == 40
|
73
|
+
documents.total_pages.should == 4
|
74
|
+
documents.next_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=3&pageSize=10"
|
75
|
+
documents.prev_page.should == "https://lesbonneschoses.prismic.io/api/documents/search?ref=UkL0hcuvzYUANCrm&page=1&pageSize=10"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
38
79
|
describe 'API::Document' do
|
39
80
|
before do
|
40
81
|
@document = @api.form('everything').query(%([[:d = at(document.id, "UkL0gMuvzYUANCpf")]])).submit(@master_ref)[0]
|
@@ -55,6 +96,28 @@ describe 'LesBonnesChoses' do
|
|
55
96
|
end
|
56
97
|
end
|
57
98
|
|
99
|
+
describe 'API::Documents' do
|
100
|
+
before do
|
101
|
+
@documents = @api.form('everything').submit(@master_ref)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'has a working [] operator' do
|
105
|
+
@documents[0].slug.should == @documents.results[0].slug
|
106
|
+
end
|
107
|
+
it 'has a proper size' do
|
108
|
+
@documents.length.should == 20
|
109
|
+
@documents.size.should == 20
|
110
|
+
end
|
111
|
+
it 'has a proper each method' do
|
112
|
+
array = []
|
113
|
+
@documents.each {|document| array << document.slug }
|
114
|
+
array.join(' ').should == 'pastry-dresser exotic-kiwi-pie apricot-pie sweet-strawberry-pie woodland-cherry-pie cool-coconut-macaron salted-caramel-macaron the-end-of-a-chapter-the-beginning-of-a-new-one get-the-right-approach-to-ganache one-day-in-the-life-of-a-les-bonnes-choses-pastry les-bonnes-chosess-internship-a-testimony our-world-famous-pastry-art-brainstorm-event tips-to-dress-a-pastry triple-chocolate-cupcake dont-be-a-stranger about-us wedding-gift-box art-director store-intern content-director'
|
115
|
+
end
|
116
|
+
it 'is a proper Enumerable' do
|
117
|
+
@documents.map {|document| document.slug }.join(' ').should == 'pastry-dresser exotic-kiwi-pie apricot-pie sweet-strawberry-pie woodland-cherry-pie cool-coconut-macaron salted-caramel-macaron the-end-of-a-chapter-the-beginning-of-a-new-one get-the-right-approach-to-ganache one-day-in-the-life-of-a-les-bonnes-choses-pastry les-bonnes-chosess-internship-a-testimony our-world-famous-pastry-art-brainstorm-event tips-to-dress-a-pastry triple-chocolate-cupcake dont-be-a-stranger about-us wedding-gift-box art-director store-intern content-director'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
58
121
|
describe 'Fragments' do
|
59
122
|
before do
|
60
123
|
@link_resolver = Prismic.link_resolver("master"){|doc_link| "http://localhost/#{doc_link.id}" }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prismic.io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Étienne Vallette d'Osia
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|