prismic.io 1.0.9 → 1.1.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: f497718ba2bd089e57df07768002414936d16b88
4
- data.tar.gz: ed6c0a61282b5103fa37944d46d404f75d5a54a8
3
+ metadata.gz: 08719b7d27a244af6b9b57a68aee5980db6aeb25
4
+ data.tar.gz: 7ba1a6bcb021a1fc6ba55cc15cd0ca196e440044
5
5
  SHA512:
6
- metadata.gz: 83a15ad2f3f28020e0c36ca73a330f84e29103a914a190224e6006842d4985a676b9b6c8b9f94d5f040a3e63a280c66e56df04cd3082cb633d0519da8c613b9f
7
- data.tar.gz: 4c549baab450a23a98f6101587a76e7364f93eff8c56b97a0534f738235a37cc47b8b9eaee8c08217d0bb847a31916e513494df35f96f55b55999e48a2f69e94
6
+ metadata.gz: c3b0b134abe08cc7a74e92fbf4ee9e2dd8ddc87795f36fa1cfcc0e2b20c4e4a0c5117f53ebc5b6cc601ff73ad8962d4863dd55ed04138e054eb2e24555fd506f
7
+ data.tar.gz: 4366750b86e6a3d109aaf99b65fbe6c13de48764af245a0b6f53f09745ab1cfdf67d40e1cf49af397c9aced0e206f0d1ec3acacb21443c824d65f07904ce70da
data/.travis.yml CHANGED
@@ -4,7 +4,6 @@ rvm:
4
4
  - 2.1.0
5
5
  - 2.0.0
6
6
  - 1.9.3
7
- - 1.9.2
8
7
  - jruby-19mode
9
8
  script: bundle exec rspec spec
10
9
  notifications:
@@ -21,4 +20,3 @@ deploy:
21
20
  on:
22
21
  tags: true
23
22
  all_branches: true
24
-
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prismic.io (1.0.9)
4
+ prismic.io (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
  ### Getting Started
9
9
 
10
+ The prismic kit is compatible with Ruby 1.9.3 or later.
11
+
10
12
  #### Install the kit for your project
11
13
 
12
14
  *(Assuming that [Ruby is installed](https://www.ruby-lang.org/en/downloads/) on your computer, as well as [RubyGems](http://rubygems.org/pages/download))*
@@ -96,4 +98,3 @@ Copyright 2013 Zengularity (http://www.zengularity.com).
96
98
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
97
99
 
98
100
  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
99
-
data/lib/prismic.rb CHANGED
@@ -191,6 +191,16 @@ module Prismic
191
191
  # @param page_size [String,Fixum] The page size
192
192
  # @return [SearchForm] self
193
193
 
194
+ # @!method fetch(fields)
195
+ # Restrict the document fragments to the specified fields
196
+ # @param fields [String] The fields separated by commas (,)
197
+ # @return [SearchForm] self
198
+
199
+ # @!method fetch_links(fields)
200
+ # Include the document fragments correspondong to the specified fields for DocumentLink
201
+ # @param fields [String] The fields separated by commas (,)
202
+ # @return [SearchForm] self
203
+
194
204
  # Create the fields'helper methods
195
205
  def create_field_helper_method(name)
196
206
  return if name == 'ref'
@@ -415,21 +425,14 @@ module Prismic
415
425
  alias :size :length
416
426
  end
417
427
 
418
- class LinkedDocument
419
- attr_accessor :id, :slug, :type, :tags
428
+ class Document
429
+ include Prismic::WithFragments
420
430
 
421
- def initialize(id, slug, type, tags)
422
- @id = id
423
- @slug = slug
424
- @type = type
425
- @tags = tags
426
- end
427
- end
428
-
429
- class Document < Prismic::WithFragments
430
431
  # @return [String]
431
432
  attr_accessor :id
432
433
  # @return [String]
434
+ attr_accessor :uid
435
+ # @return [String]
433
436
  attr_accessor :type
434
437
  # @return [String]
435
438
  attr_accessor :href
@@ -437,31 +440,19 @@ module Prismic
437
440
  attr_accessor :tags
438
441
  # @return [Array<String>]
439
442
  attr_accessor :slugs
440
- # @return [Array<LinkedDocument>]
441
- attr_accessor :linked_documents
443
+ # @return [Array<Fragment>]
444
+ attr_accessor :fragments
442
445
 
443
- def initialize(id, type, href, tags, slugs, linked_documents, fragments)
444
- super(fragments)
446
+ def initialize(id, uid, type, href, tags, slugs, fragments)
445
447
  @id = id
448
+ @uid = uid
446
449
  @type = type
447
450
  @href = href
448
451
  @tags = tags
449
452
  @slugs = slugs
450
- @linked_documents = linked_documents
453
+ @fragments = fragments
451
454
  end
452
455
 
453
- # Get a document's field
454
- # @return [Fragments::Fragment]
455
- def [](field)
456
- array = field.split('.')
457
- if array.length != 2
458
- raise ArgumentError, 'Argument should contain one dot. Example: product.price'
459
- end
460
- return nil if array[0] != self.type
461
- fragments[array[1]]
462
- end
463
- alias :get :[]
464
-
465
456
  # Returns the document's slug
466
457
  #
467
458
  # @return [String]
@@ -532,7 +523,7 @@ module Prismic
532
523
  if doc.is_a? Prismic::Fragments::DocumentLink
533
524
  @blk.call(doc)
534
525
  elsif doc.is_a? Prismic::Document
535
- doc_link = Prismic::Fragments::DocumentLink.new(doc.id, doc.type, doc.tags, doc.slug, false)
526
+ doc_link = Prismic::Fragments::DocumentLink.new(doc.id, doc.uid, doc.type, doc.tags, doc.slug, doc.fragments, false)
536
527
  @blk.call(doc_link)
537
528
  end
538
529
  end
data/lib/prismic/api.rb CHANGED
@@ -104,14 +104,13 @@ module Prismic
104
104
  # @return [String] the URL to redirect the user to
105
105
  def preview_session(token, link_resolver, default_url)
106
106
  response = self.http_client.get(token, {}, 'Accept' => 'application/json')
107
- if response.code.to_s == '200'
108
- json = JSON.load(response.body)
109
- documents = self.form('everything').query(Prismic::Predicates.at('document.id', json['mainDocument'])).submit(token)
110
- if documents.results.size > 0
111
- link_resolver.link_to(documents.results[0])
112
- else
113
- default_url
114
- end
107
+ if response.code.to_s != '200'
108
+ return default_url
109
+ end
110
+ json = JSON.load(response.body)
111
+ documents = self.form('everything').query(Prismic::Predicates.at('document.id', json['mainDocument'])).submit(token)
112
+ if documents.results.size > 0
113
+ link_resolver.link_to(documents.results[0])
115
114
  else
116
115
  default_url
117
116
  end
@@ -2,6 +2,23 @@
2
2
  module Prismic
3
3
  module Fragments
4
4
 
5
+ class GroupDocument
6
+ include Prismic::WithFragments
7
+ attr_accessor :fragments
8
+
9
+ def initialize(fragments)
10
+ @fragments = fragments
11
+ end
12
+
13
+ # Get a document's field
14
+ # @return [Fragments::Fragment]
15
+ def [](field)
16
+ @fragments[field]
17
+ end
18
+ alias :get :[]
19
+
20
+ end
21
+
5
22
  # A fragment of type Group, which contains an array of FragmentList (which
6
23
  # itself is a Hash of fragments).
7
24
  #
@@ -95,13 +95,16 @@ module Prismic
95
95
  end
96
96
 
97
97
  class DocumentLink < Link
98
- attr_accessor :id, :link_type, :tags, :slug, :broken
98
+ include Prismic::WithFragments
99
+ attr_accessor :id, :uid, :type, :tags, :slug, :fragments, :broken
99
100
 
100
- def initialize(id, link_type, tags, slug, broken)
101
+ def initialize(id, uid, type, tags, slug, fragments, broken)
101
102
  @id = id
102
- @link_type = link_type
103
+ @uid = uid
104
+ @type = type
103
105
  @tags = tags
104
106
  @slug = slug
107
+ @fragments = fragments
105
108
  @broken = broken
106
109
  end
107
110
 
@@ -117,6 +120,11 @@ module Prismic
117
120
  %(#{start_html(link_resolver)}#{slug}#{end_html})
118
121
  end
119
122
 
123
+ def link_type
124
+ warn('WARNING: DocumentLink.link_type is deprecated, use DocumentLink.type instead')
125
+ self.type
126
+ end
127
+
120
128
  # Returns the URL of the link
121
129
  #
122
130
  # @overload url(link_resolver)
@@ -29,11 +29,24 @@ module Prismic
29
29
 
30
30
  def document_link_parser(json)
31
31
  doc = json['value']['document']
32
+ type = doc['type']
33
+ fragments = {}
34
+ if doc['data'] and doc['data'][type]
35
+ fragments = Hash[doc['data'][type].map { |name, fragment|
36
+ if fragment.is_a? Array
37
+ [name, multiple_parser(fragment)]
38
+ else
39
+ [name, parsers[fragment['type']].call(fragment)]
40
+ end
41
+ }]
42
+ end
32
43
  Prismic::Fragments::DocumentLink.new(
33
44
  doc['id'],
34
- doc['type'],
45
+ doc['uid'],
46
+ type,
35
47
  doc['tags'],
36
48
  URI.unescape(doc['slug']),
49
+ fragments,
37
50
  json['value']['isBroken'])
38
51
  end
39
52
 
@@ -181,7 +194,7 @@ module Prismic
181
194
  fragment_list_array = []
182
195
  json['value'].each do |group|
183
196
  fragments = Hash[ group.map {|name, fragment| [name, parsers[fragment['type']].call(fragment)] }]
184
- fragment_list_array << Prismic::WithFragments.new(fragments)
197
+ fragment_list_array << Prismic::Fragments::GroupDocument.new(fragments)
185
198
  end
186
199
  Prismic::Fragments::Group.new(fragment_list_array)
187
200
  end
@@ -207,15 +220,13 @@ module Prismic
207
220
  end
208
221
  }]
209
222
 
210
- linked_documents = linked_documents_parser(json['linked_documents'])
211
-
212
223
  Prismic::Document.new(
213
224
  json['id'],
225
+ json['uid'],
214
226
  json['type'],
215
227
  json['href'],
216
228
  json['tags'],
217
229
  json['slugs'].map { |slug| URI.unescape(slug) },
218
- linked_documents,
219
230
  fragments)
220
231
  end
221
232
 
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
 
4
- VERSION = '1.0.9'
4
+ VERSION = '1.1.0'
5
5
 
6
6
  end
@@ -2,14 +2,10 @@
2
2
  module Prismic
3
3
 
4
4
  # A document with Fragments: usually a Prismic.io Document, or a Document within a Group
5
- class WithFragments
5
+ module WithFragments
6
6
  # @return [Hash{String => Fragment}]
7
7
  attr_accessor :fragments
8
8
 
9
- def initialize(fragments)
10
- @fragments = fragments
11
- end
12
-
13
9
  # Generate an HTML representation of the entire document
14
10
  #
15
11
  # @param link_resolver [LinkResolver] The LinkResolver used to build
@@ -48,7 +44,12 @@ module Prismic
48
44
  # Get a document's field
49
45
  # @return [Fragments::Fragment]
50
46
  def [](field)
51
- @fragments[field]
47
+ array = field.split('.')
48
+ if array.length != 2
49
+ raise ArgumentError, 'Argument should contain one dot. Example: product.price'
50
+ end
51
+ return nil if array[0] != self.type
52
+ fragments[array[1]]
52
53
  end
53
54
  alias :get :[]
54
55
 
@@ -135,6 +136,27 @@ module Prismic
135
136
  fragment
136
137
  end
137
138
 
139
+ # @return [Array<LinkedDocument>]
140
+ def linked_documents
141
+ self.fragments.map { |name, fragment|
142
+ if fragment.is_a? Prismic::Fragments::DocumentLink
143
+ [fragment]
144
+ elsif fragment.is_a? Prismic::Fragments::StructuredText
145
+ fragment.blocks
146
+ .select { |block| block.is_a? Prismic::Fragments::StructuredText::Block::Text}
147
+ .map { |block| block.spans }.flatten
148
+ .select { |span| span.is_a? Prismic::Fragments::StructuredText::Span::Hyperlink }
149
+ .map { |span| span.link }
150
+ .select { |link| link.is_a? Prismic::Fragments::DocumentLink }
151
+ elsif fragment.is_a? Prismic::Fragments::Group
152
+ fragment.group_documents
153
+ .map { |groupDoc| groupDoc.linked_documents }
154
+ .flatten
155
+ else
156
+ []
157
+ end
158
+ }.flatten
159
+ end
138
160
  end
139
161
 
140
162
  end
data/prismic.gemspec CHANGED
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'prismic/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "prismic.io"
7
+ spec.name = 'prismic.io'
8
8
  spec.version = Prismic::VERSION
9
- spec.authors = ["Étienne Vallette d'Osia", "Erwan Loisant", "Samy Dindane", "Rudy Rigot"]
10
- spec.email = ["evo@zenexity.com"]
9
+ spec.authors = ["Étienne Vallette d'Osia", 'Erwan Loisant', 'Samy Dindane', 'Rudy Rigot']
10
+ spec.email = ['evo@zenexity.com']
11
11
  spec.description = %q{The standard Prismic.io's API library.}
12
12
  spec.summary = %q{Prismic.io development kit}
13
- spec.homepage = "http://prismic.io"
14
- spec.license = "Apache-2"
13
+ spec.homepage = 'http://prismic.io'
14
+ spec.license = 'Apache-2'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rspec", "~> 2.14"
23
- spec.add_development_dependency "nokogiri", "~> 1.6"
24
- spec.add_development_dependency "simplecov", "~> 0.7"
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rspec', '~> 2.14'
23
+ spec.add_development_dependency 'nokogiri', '~> 1.6'
24
+ spec.add_development_dependency 'simplecov', '~> 0.7'
25
25
  end
data/spec/doc_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe 'Documentation' do
7
7
 
8
8
  it 'api.get' do
9
9
  # startgist:31cf4b514778d5d9d9cc:prismic-api.rb
10
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
10
+ api = Prismic.api('https://lesbonneschoses.cdn.wroom.io/api')
11
11
  # endgist
12
12
  api.should_not be_nil
13
13
  end
@@ -16,16 +16,16 @@ describe 'Documentation' do
16
16
  expect {
17
17
  # startgist:10822fc1befeeea1191a:prismic-apiPrivate.rb
18
18
  # This will fail because the token is invalid, but this is how to access a private API
19
- api = Prismic.api('https://lesbonneschoses.prismic.io/api', 'MC5-XXXXXXX-vRfvv70')
19
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', 'MC5-XXXXXXX-vRfvv70')
20
20
  # endgist
21
- # err.message, "Unexpected status code [401] on URL https://lesbonneschoses.prismic.io/api?access_token=MC5-XXXXXXX-vRfvv70"); // gisthide
21
+ # err.message, "Unexpected status code [401] on URL https://lesbonneschoses.cdn.prismic.io/api?access_token=MC5-XXXXXXX-vRfvv70"); // gisthide
22
22
  }.to raise_error(Prismic::API::PrismicWSAuthError, "Can't connect to Prismic's API: Invalid access token")
23
23
  end
24
24
 
25
25
  it 'references' do
26
26
  # startgist:431e191cabf5e160c701:prismic-references.rb
27
27
  preview_token = 'MC5VbDdXQmtuTTB6Z0hNWHF3.c--_vVbvv73vv73vv73vv71EA--_vS_vv73vv70T77-9Ke-_ve-_vWfvv70ebO-_ve-_ve-_vQN377-9ce-_vRfvv70';
28
- api = Prismic.api('https://lesbonneschoses.prismic.io/api', preview_token)
28
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api', preview_token)
29
29
  st_patrick_ref = api.ref('St-Patrick specials')
30
30
  # Now we'll use this reference for all our calls
31
31
  response = api.form('everything')
@@ -44,7 +44,7 @@ describe 'Documentation' do
44
44
 
45
45
  it 'simple query' do
46
46
  # startgist:8943931ba0cf2bb2c873:prismic-simplequery.rb
47
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
47
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
48
48
  response = api
49
49
  .form('everything')
50
50
  .query(Prismic::Predicates::at('document.type', 'product'))
@@ -56,7 +56,7 @@ describe 'Documentation' do
56
56
 
57
57
  it 'orderings' do
58
58
  # startgist:2866b2e2cae0221f2188:prismic-orderings.rb
59
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
59
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
60
60
  response = api.form('everything')
61
61
  .ref(api.master_ref)
62
62
  .query(Prismic::Predicates::at('document.type', 'product'))
@@ -71,7 +71,7 @@ describe 'Documentation' do
71
71
 
72
72
  it 'predicates' do
73
73
  # startgist:e0501cce9a12fa4b83db:prismic-predicates.rb
74
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
74
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
75
75
  response = api
76
76
  .form('everything')
77
77
  .query(
@@ -104,7 +104,7 @@ describe 'Documentation' do
104
104
  describe 'fragments' do
105
105
 
106
106
  it 'text' do
107
- api = Prismic::api('https://lesbonneschoses.prismic.io/api')
107
+ api = Prismic::api('https://lesbonneschoses.cdn.prismic.io/api')
108
108
  response = api.form('everything')
109
109
  .query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbl'))
110
110
  .ref(api.master_ref)
@@ -122,7 +122,7 @@ describe 'Documentation' do
122
122
  end
123
123
 
124
124
  it 'number' do
125
- api = Prismic::api('https://lesbonneschoses.prismic.io/api')
125
+ api = Prismic::api('https://lesbonneschoses.cdn.prismic.io/api')
126
126
  response = api.form('everything')
127
127
  .query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbO'))
128
128
  .ref(api.master_ref)
@@ -141,7 +141,7 @@ describe 'Documentation' do
141
141
  end
142
142
 
143
143
  it 'images' do
144
- api = Prismic::api('https://lesbonneschoses.prismic.io/api')
144
+ api = Prismic::api('https://lesbonneschoses.cdn.prismic.io/api')
145
145
  response = api.form('everything')
146
146
  .query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbO'))
147
147
  .ref(api.master_ref)
@@ -153,11 +153,11 @@ describe 'Documentation' do
153
153
  # Most of the time you will be using the "main" view
154
154
  url = image.main.url
155
155
  # endgist
156
- url.should == 'https://prismic-io.s3.amazonaws.com/lesbonneschoses/f606ad513fcc2a73b909817119b84d6fd0d61a6d.png'
156
+ url.should == 'https://lesbonneschoses.cdn.prismic.io/lesbonneschoses/f606ad513fcc2a73b909817119b84d6fd0d61a6d.png'
157
157
  end
158
158
 
159
159
  it 'date and timestamp' do
160
- api = Prismic::api('https://lesbonneschoses.prismic.io/api')
160
+ api = Prismic::api('https://lesbonneschoses.cdn.prismic.io/api')
161
161
  response = api.form('everything')
162
162
  .query(Predicates::at('document.id', 'UlfoxUnM0wkXYXbl'))
163
163
  .ref(api.master_ref)
@@ -254,7 +254,7 @@ describe 'Documentation' do
254
254
  end
255
255
 
256
256
  it 'asHtml' do
257
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
257
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
258
258
  response = api
259
259
  .form('everything')
260
260
  .query(Prismic::Predicates::at('document.id', 'UlfoxUnM0wkXYXbX'))
@@ -268,7 +268,7 @@ describe 'Documentation' do
268
268
  end
269
269
 
270
270
  it 'HtmlSerializer' do
271
- api = Prismic.api('https://lesbonneschoses.prismic.io/api')
271
+ api = Prismic.api('https://lesbonneschoses.cdn.prismic.io/api')
272
272
  response = api
273
273
  .form('everything')
274
274
  .query(Prismic::Predicates::at('document.id', 'UlfoxUnM0wkXYXbX'))
@@ -294,7 +294,7 @@ describe 'Documentation' do
294
294
  # You can pass any object implementing the same methods as the BasicCache
295
295
  # https://github.com/prismicio/ruby-kit/blob/master/lib/prismic/cache/basic.rb
296
296
  cache = Prismic::BasicCache.new
297
- api = Prismic::api('https://lesbonneschoses.prismic.io/api', { :cache => cache })
297
+ api = Prismic::api('https://lesbonneschoses.cdn.prismic.io/api', { :cache => cache })
298
298
  # The Api will use the custom cache object
299
299
  # endgist
300
300
  api.should_not be_nil