prismic.io 1.0.0.preview.5 → 1.0.0.preview.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e3f3af94b5219971b0b0dbdd62b195a0bb9a5ae
4
- data.tar.gz: 157a7210a64dbf0e1a3f5de9b722b6222c7002a5
3
+ metadata.gz: 48b555e3c575061f3461b396c1739459780a690d
4
+ data.tar.gz: ec67ac158b329572e7a65e76be77bf5464139c4f
5
5
  SHA512:
6
- metadata.gz: 88eb3ea747a31a8226c3fb5f5c364e046e85e08013061510d7f5d55eb369bed64e56ef86bcb2f72220134975923fd7ea8becd557303823efc4e6ef9462cc3a95
7
- data.tar.gz: 870e6c272bd7efe97346cdc4186e533a4332c5e2c940e30e183949278b89ccdf22d68942e3731d8d7f4ca61add02119b9ac60659ec7f332f7636a24f67c112d9
6
+ metadata.gz: b1f78ed016ec55f62dd5538f688f9ad887cfcb6cf7351840ba1ef2f7f85ab040ace75cd2caebb1546c4d43f1b856acea88d7f33b418a440a31e4910a1f299ba9
7
+ data.tar.gz: b523de93f12d9c831ce127cad7629682d4cd4d481ef785c9eaac098e7e95c4b3f038e1392f6d28b021b7c1e47ab5c91a088f14d3c2c935eb88a45bdd93392499
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prismic.io (1.0.0.preview.4)
4
+ prismic.io (1.0.0.preview.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -28,9 +28,15 @@ then add in your code:
28
28
  require 'prismic'
29
29
  ```
30
30
 
31
- #### API Documentation
31
+ #### Kit Documentation
32
32
 
33
- You can browse the [API Documentation](https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation).
33
+ You should check out [the "Kits and helpers" section of our API documentation](https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#kits-and-helpers), which sets general information about how our kits work. The Ruby kit is mostly compliant with it, but contains some mild differences:
34
+
35
+ * The api object's type is Prismic::API, and the name of the method to retrieve the master ref is gotten by `api.master_ref`.
36
+ * There's no `ctx` object to pass around; the context is passed in different ways.
37
+ * From the api object, getting a form is done through the `create_search_form` method; a basic querying therefore looks like this: `api.create_search_form("everything").query("[[:d = at(document.type, \"product\")]]").submit(ref)`
38
+ * Advice: the `%()` Ruby notation for strings is great for queries, as they may include quotes and/or double-quotes. The above querying is therefore even better this way: `api.create_search_form("everything").query(%([[:d = at(document.type, "product")]])).submit(ref)`
39
+ * Accessing type-dependent fields from a `document` is done through a `fragments` hash. Printing the HTML version of a field therefore looks like `document.fragments["title_user_friendly"].as_html(link_resolver(ref)).html_safe`.
34
40
 
35
41
  #### Use it
36
42
 
data/lib/prismic/api.rb CHANGED
@@ -1,33 +1,56 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  class API
4
- attr_reader :json, :access_token
4
+ attr_reader :json, :access_token, :http_client
5
5
  attr_accessor :refs, :bookmarks, :forms, :master, :tags, :types, :oauth
6
6
 
7
- def initialize(json, access_token=nil)
7
+ def initialize(json, access_token, http_client)
8
8
  @json = json
9
9
  @access_token = access_token
10
+ @http_client = http_client
10
11
  yield self if block_given?
11
12
  self.master = refs.values && refs.values.map { |ref| ref if ref.master? }.compact.first
12
13
  raise BadPrismicResponseError, "No master Ref found" unless master
13
14
  end
14
15
 
16
+ # Get a bookmark by its name
17
+ # @api
18
+ # @param name [String] The bookmark's name
19
+ #
20
+ # @return [Hash] The bookmark
15
21
  def bookmark(name)
16
22
  bookmarks[name]
17
23
  end
18
24
 
25
+ # Get a {Ref reference} by its alias
26
+ # @api
27
+ # @param name [String] The reference's alias
28
+ #
29
+ # @return [Ref] The reference
19
30
  def ref(name)
20
31
  refs[name.downcase]
21
32
  end
22
33
 
34
+ # Returns the master {Ref reference}
35
+ # @api
36
+ #
37
+ # @return [type] [description]
23
38
  def master_ref
24
39
  ref('master')
25
40
  end
26
41
 
42
+
27
43
  def form(name)
28
44
  forms[name]
29
45
  end
30
46
 
47
+ # Returns a {Prismic::SearchForm search form} by its name
48
+ # @api
49
+ # @param name [String] The name of the form
50
+ # @param data [Hash] Default values
51
+ # @param ref [type] Default {Ref reference}
52
+ #
53
+ # @return [SearchForm] The search form
31
54
  def create_search_form(name, data={}, ref={})
32
55
  form = self.form(name)
33
56
  form and form.create_search_form(data, ref)
@@ -37,29 +60,26 @@ module Prismic
37
60
  @json
38
61
  end
39
62
 
40
- def self.get(url, access_token=nil)
41
- uri = URI(access_token ? "#{url}?access_token=#{access_token}" : url)
42
- http = Net::HTTP.new(uri.host)
43
- req = Net::HTTP::Get.new(uri.path, 'Accept' => 'application/json')
44
- res = http.request(req)
45
-
46
- if res.code == '200'
47
- res
48
- else
49
- raise PrismicWSConnectionError, res
50
- end
63
+ def self.get(url, access_token=nil, http_client=Prismic::DefaultHTTPClient)
64
+ data = {}
65
+ data["access_token"] = access_token if access_token
66
+ res = http_client.get(url, data, 'Accept' => 'application/json')
67
+ raise PrismicWSConnectionError, res unless res.code.to_s == '200'
68
+ res
51
69
  end
52
70
 
53
- def self.start(url, access_token=nil)
54
- resp = get(url, access_token)
71
+ def self.start(url, opts={})
72
+ http_client = opts[:http_client] || Prismic::DefaultHTTPClient
73
+ access_token = opts[:access_token]
74
+ resp = get(url, access_token, http_client)
55
75
  json = JSON.load(resp.body)
56
- parse_api_response(json, access_token)
76
+ parse_api_response(json, access_token, http_client)
57
77
  end
58
78
 
59
- def self.parse_api_response(data, access_token=nil)
79
+ def self.parse_api_response(data, access_token, http_client)
60
80
  data_forms = data['forms'] || []
61
81
  data_refs = data.fetch('refs'){ raise BadPrismicResponseError, "No refs given" }
62
- new(data, access_token) {|api|
82
+ new(data, access_token, http_client) {|api|
63
83
  api.bookmarks = data['bookmarks']
64
84
  api.forms = Hash[data_forms.map { |k, form|
65
85
  [k, Form.new(
@@ -26,12 +26,14 @@ module Prismic
26
26
  class ViewDoesNotExistException < Error ; end
27
27
 
28
28
  class View
29
- attr_accessor :url, :width, :height
29
+ attr_accessor :url, :width, :height, :alt, :copyright
30
30
 
31
- def initialize(url, width, height)
31
+ def initialize(url, width, height, alt, copyright)
32
32
  @url = url
33
33
  @width = width
34
34
  @height = height
35
+ @alt = alt
36
+ @copyright = copyright
35
37
  end
36
38
 
37
39
  def ratio
@@ -39,7 +41,7 @@ module Prismic
39
41
  end
40
42
 
41
43
  def as_html(link_resolver=nil)
42
- %(<img src="#@url" width="#@width" height="#@height" />)
44
+ %(<img src="#@url" alt="#@alt" width="#@width" height="#@height" />)
43
45
  end
44
46
 
45
47
  end
@@ -47,6 +47,21 @@ module Prismic
47
47
  }.join("\n\n")
48
48
  end
49
49
 
50
+ # Finds the first highest title in a structured text
51
+ def first_title
52
+ max_level = 6 # any title with a higher level kicks the current one out
53
+ title = false
54
+ @blocks.each do |block|
55
+ if block.is_a?(Prismic::Fragments::StructuredText::Block::Heading)
56
+ if block.level < max_level
57
+ title = block.text
58
+ max_level = block.level # new maximum
59
+ end
60
+ end
61
+ end
62
+ title
63
+ end
64
+
50
65
  class Span
51
66
  attr_accessor :start, :end
52
67
 
@@ -184,6 +199,14 @@ module Prismic
184
199
  @view.height
185
200
  end
186
201
 
202
+ def alt
203
+ @view.alt
204
+ end
205
+
206
+ def copyright
207
+ @view.copyright
208
+ end
209
+
187
210
  def as_html(link_resolver)
188
211
  view.as_html(link_resolver)
189
212
  end
@@ -63,12 +63,15 @@ module Prismic
63
63
  def self.view_parser(json)
64
64
  Prismic::Fragments::Image::View.new(json['url'],
65
65
  json['dimensions']['width'],
66
- json['dimensions']['height'])
66
+ json['dimensions']['height'],
67
+ json['alt'],
68
+ json['copyright'])
67
69
  end
68
70
 
69
71
  main = view_parser(json['value']['main'])
70
- views = json['value']['views'].map do |name, view|
71
- view_parser(view)
72
+ views = {}
73
+ json['value']['views'].each do |name, view|
74
+ views[name] = view_parser(view)
72
75
  end
73
76
 
74
77
  Prismic::Fragments::Image.new(main, views)
@@ -126,7 +129,9 @@ module Prismic
126
129
  view = Prismic::Fragments::Image::View.new(
127
130
  block['url'],
128
131
  block['dimensions']['width'],
129
- block['dimensions']['height']
132
+ block['dimensions']['height'],
133
+ block['alt'],
134
+ block['copyright']
130
135
  )
131
136
  Prismic::Fragments::StructuredText::Block::Image.new(view)
132
137
  when 'embed'
@@ -154,22 +159,23 @@ module Prismic
154
159
  end
155
160
 
156
161
  def document_parser(json)
157
- fragments = Hash[json['data'].values.first.map do |name, fragment|
162
+ data_json = json['data'].values.first # {"doc_type": data}
163
+ fragments = Hash[data_json.map { |name, fragment|
158
164
  if fragment.is_a? Array
159
165
  [name, multiple_parser(fragment)]
160
166
  else
161
167
  [name, parsers[fragment['type']].call(fragment)]
162
168
  end
163
- end]
169
+ }]
164
170
 
165
171
  Prismic::Document.new(json['id'], json['type'], json['href'], json['tags'],
166
172
  json['slugs'], fragments)
167
173
  end
168
174
 
169
- def results_parser(results)
170
- results = results['results'] unless results.is_a?(Array)
171
- results.map do |doc|
172
- raise FormSearchException, "Error : #{doc['error']}" if doc.include?('error')
175
+ def results_parser(result)
176
+ result = {'results' => result} if result.is_a?(Array)
177
+ raise FormSearchException, "Error : #{result['error']}" if result['error']
178
+ result['results'].map do |doc|
173
179
  document_parser(doc)
174
180
  end
175
181
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
 
4
- VERSION = "1.0.0.preview.5"
4
+ VERSION = "1.0.0.preview.6"
5
5
 
6
6
  end
data/lib/prismic.rb CHANGED
@@ -11,6 +11,11 @@ module Prismic
11
11
  msg ? super(msg) : msg
12
12
  @cause = cause
13
13
  end
14
+
15
+ # Return the full trace of the error (including nested errors)
16
+ # @param e=self Parent error (don't use)
17
+ #
18
+ # @return [String] The trace
14
19
  def full_trace(e=self)
15
20
  first, *backtrace = e.backtrace
16
21
  msg = e == self ? "" : "Caused by "
@@ -22,21 +27,51 @@ module Prismic
22
27
  end
23
28
  end
24
29
 
25
- def self.api(*args)
26
- API.start(*args)
30
+ # Return an API instance
31
+ # @api
32
+ #
33
+ # The access token and HTTP client can be provided.
34
+ #
35
+ # The HTTP Client must responds to same method than {DefaultHTTPClient}.
36
+ #
37
+ # @param url [String] The URL of the prismic.io repository
38
+ # @param [String] access_token The access token
39
+ # @param [Hash] opts The options
40
+ # @option opts [String] :access_token (nil) The access_token
41
+ # @option opts :http_client (DefaultHTTPClient) The HTTP client to use
42
+ #
43
+ # @overload api(url, opts=nil)
44
+ # Standard use
45
+ # @overload api(url, access_token)
46
+ # Provide the access_token (only)
47
+ #
48
+ # @return [API] The API instance related to this repository
49
+ def self.api(url, opts=nil)
50
+ opts ||= {}
51
+ opts = {access_token: opts} if opts.is_a?(String)
52
+ API.start(url, opts)
27
53
  end
28
54
 
29
55
  class ApiData
30
56
  attr_accessor :refs, :bookmarks, :types, :tags, :forms
31
57
  end
32
58
 
59
+
60
+ # A SearchForm represent a Form returned by the prismic.io API.
61
+ #
62
+ # These forms depend on the prismic.io repository, and can be fill and send
63
+ # in the same way than regular HTML forms.
64
+ #
65
+ # Get SearchForm instance through the {API#create_search_form} method.
33
66
  class SearchForm
34
67
  attr_accessor :api, :form, :data, :ref
35
68
 
36
69
  def initialize(api, form, data={}, ref=nil)
37
70
  @api = api
38
71
  @form = form
39
- @data = form.default_data.merge(data)
72
+ @data = {}
73
+ form.default_data.each { |key, value| set(key, value) }
74
+ data.each { |key, value| set(key, value) }
40
75
  @ref = ref
41
76
  end
42
77
 
@@ -64,55 +99,81 @@ module Prismic
64
99
  form.fields
65
100
  end
66
101
 
67
- def submit(ref = @ref)
102
+ # Submit the form
103
+ # @api
104
+ #
105
+ # @note The reference MUST be defined, either by setting it at
106
+ # {API#create_search_form creation}, by using the {#ref} method or by
107
+ # providing the ref parameter.
108
+ #
109
+ # @param ref [Ref, String] The {Ref reference} to use (if not already defined)
110
+ #
111
+ # @return [type] [description]
112
+ def submit(ref = nil)
113
+ self.ref(ref) if ref
68
114
  raise NoRefSetException unless @ref
69
115
 
70
116
  if form_method == "GET" && enctype == "application/x-www-form-urlencoded"
71
- data['ref'] = ref
117
+ data['ref'] = @ref
72
118
  data['access_token'] = api.access_token if api.access_token
73
119
  data.delete_if { |k, v| v.nil? }
74
120
 
75
- uri = URI(action)
76
- uri.query = URI.encode_www_form(data)
77
-
78
- request = Net::HTTP::Get.new(uri.request_uri)
79
- request.add_field('Accept', 'application/json')
80
-
81
- response = Net::HTTP.new(uri.host, uri.port).start do |http|
82
- http.request(request)
121
+ response = api.http_client.get(action, data, 'Accept' => 'application/json')
122
+
123
+ if response.code.to_s == "200"
124
+ Prismic::JsonParser.results_parser(JSON.parse(response.body))
125
+ else
126
+ body = JSON.parse(response.body) rescue nil
127
+ error = body.is_a?(Hash) ? body['error'] : response.body
128
+ raise AuthenticationException, error if response.code.to_s == "401"
129
+ raise AuthorizationException, error if response.code.to_s == "403"
130
+ raise RefNotFoundException, error if response.code.to_s == "404"
131
+ raise FormSearchException, error
83
132
  end
84
-
85
- raise RefNotFoundException, "Ref #{ref} not found" if response.code == "404"
86
-
87
- raise FormSearchException, "Error : #{response.body}" if response.code != "200"
88
-
89
- Prismic::JsonParser.results_parser(JSON.parse(response.body))
90
133
  else
91
134
  raise UnsupportedFormKind, "Unsupported kind of form: #{form_method} / #{enctype}"
92
135
  end
93
136
  end
94
137
 
138
+ # Specify a query for this form
139
+ # @api
140
+ # @param query [String] The query
141
+ #
142
+ # @return [SearchForm] self
95
143
  def query(query)
96
144
  set('q', query)
97
- self
98
145
  end
99
146
 
147
+ # Specify a parameter for this form
148
+ # @param field_name [String] The parameter's name
149
+ # @param value [String] The parameter's value
150
+ #
151
+ # @return [SearchForm] self
100
152
  def set(field_name, value)
101
153
  field = @form.fields[field_name]
102
- if field.repeatable?
154
+ if field && field.repeatable?
103
155
  data[field_name] = [] unless data.include? field_name
104
156
  data[field_name] << value
105
157
  else
106
158
  data[field_name] = value
107
159
  end
160
+ self
108
161
  end
109
162
 
163
+ # Set the {Ref reference} to use
164
+ # @api
165
+ # @param ref [Ref, String] The {Ref reference} to use
166
+ #
167
+ # @return [SearchForm] self
110
168
  def ref(ref)
111
- @ref = ref
169
+ @ref = ref.is_a?(Ref) ? ref.ref : ref
170
+ self
112
171
  end
113
172
 
114
173
  class NoRefSetException < Error ; end
115
174
  class UnsupportedFormKind < Error ; end
175
+ class AuthorizationException < Error ; end
176
+ class AuthenticationException < Error ; end
116
177
  class RefNotFoundException < Error ; end
117
178
  class FormSearchException < Error ; end
118
179
  end
@@ -151,6 +212,27 @@ module Prismic
151
212
  }.join("\n")
152
213
  end
153
214
 
215
+ # Finds the first highest title in a document
216
+ #
217
+ # It is impossible to reuse the StructuredText.first_title method, since we need to test the highest title across the whole document
218
+ def first_title
219
+ title = false
220
+ max_level = 6 # any title with a higher level kicks the current one out
221
+ @fragments.each do |_, fragment|
222
+ if fragment.is_a? Prismic::Fragments::StructuredText
223
+ fragment.blocks.each do |block|
224
+ if block.is_a?(Prismic::Fragments::StructuredText::Block::Heading)
225
+ if block.level < max_level
226
+ title = block.text
227
+ max_level = block.level # new maximum
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
233
+ title
234
+ end
235
+
154
236
  private
155
237
 
156
238
  def parse_fragments(fragments)
@@ -158,6 +240,12 @@ module Prismic
158
240
  end
159
241
  end
160
242
 
243
+
244
+ # Represent a prismic.io reference, a fix point in time.
245
+ #
246
+ # The references must be provided when accessing to any prismic.io resource
247
+ # (except /api) and allow to assert that the URL you use will always
248
+ # returns the same results.
161
249
  class Ref
162
250
  attr_accessor :ref, :label, :is_master, :scheduled_at
163
251
 
@@ -182,9 +270,51 @@ module Prismic
182
270
  end
183
271
  end
184
272
 
273
+ # Default HTTP client implementation, using the standard {Net::HTTP} library.
274
+ module DefaultHTTPClient
275
+ class << self
276
+ # Performs a GET call and returns the result
277
+ #
278
+ # The result must respond to
279
+ # - code: returns the response's HTTP status code (as number or String)
280
+ # - body: returns the response's body (as String)
281
+ def get(uri, data={}, headers={})
282
+ uri = URI(uri) if uri.is_a?(String)
283
+ uri.query = url_encode(data)
284
+ http = Net::HTTP.new(uri.host, uri.port)
285
+ http.use_ssl = uri.scheme =~ /https/i
286
+ http.get(uri.request_uri, headers)
287
+ end
288
+
289
+ def url_encode(data)
290
+ # Can't use URI.encode_www_form (doesn't support multi-values in 1.9.2)
291
+ encode = ->(k, v){ "#{k}=#{CGI::escape(v)}" }
292
+ data.map { |k, vs|
293
+ if vs.is_a?(Array)
294
+ vs.map{|v| encode.(k, v) }.join("&")
295
+ else
296
+ encode.(k, vs)
297
+ end
298
+ }.join("&")
299
+ end
300
+ end
301
+ end
302
+
303
+ # Build a {LinkResolver} instance
304
+ # @api
305
+ #
306
+ # The {LinkResolver} will help to build URL specific to an application, based
307
+ # on a generic prismic.io's {Fragments::DocumentLink Document link}.
308
+ #
309
+ # @param ref [Ref] The ref to use
310
+ # @yieldparam doc_link [Fragments::DocumentLink] A DocumentLink instance
311
+ # @yieldreturn [String] The application specific URL of the given document
312
+ #
313
+ # @return [LinkResolver] [description]
185
314
  def self.link_resolver(ref, &blk)
186
315
  LinkResolver.new(ref, &blk)
187
316
  end
317
+
188
318
  end
189
319
 
190
320
  require 'prismic/api'
@@ -275,7 +275,7 @@ describe 'Image::View' do
275
275
  @url = 'my_url'
276
276
  @width = 10
277
277
  @height = 2
278
- @view = Prismic::Fragments::Image::View.new(@url, @width, @height)
278
+ @view = Prismic::Fragments::Image::View.new(@url, @width, @height, "", "")
279
279
  end
280
280
 
281
281
  describe 'ratio' do
@@ -300,13 +300,23 @@ describe 'Image::View' do
300
300
  it "returns an element whose `height` attribute equals the height" do
301
301
  Nokogiri::XML(@view.as_html).child.attribute('height').value.should == @height.to_s
302
302
  end
303
+
304
+ it "if set, returns an element whose `alt` attribute equals the alt" do
305
+ @alt = "Alternative text"
306
+ @view.alt = @alt
307
+ Nokogiri::XML(@view.as_html).child.attribute('alt').value.should == @alt
308
+ end
309
+
310
+ # it "if not set, alt attribute is absent" do
311
+ # Nokogiri::XML(@view.as_html).child.attribute('alt').should == nil
312
+ # end
303
313
  end
304
314
  end
305
315
 
306
316
  describe 'Image' do
307
317
  before do
308
- @main_view = Prismic::Fragments::Image::View.new('my_url', 10, 10)
309
- @another_view = Prismic::Fragments::Image::View.new('my_url2', 20, 20)
318
+ @main_view = Prismic::Fragments::Image::View.new('my_url', 10, 10, "Alternative", "CC-BY")
319
+ @another_view = Prismic::Fragments::Image::View.new('my_url2', 20, 20, "", "")
310
320
  @image = Prismic::Fragments::Image.new(@main_view, { 'another_view' => @another_view })
311
321
  end
312
322
 
@@ -330,10 +340,33 @@ describe 'Image' do
330
340
  Nokogiri::XML(@image.as_html).child.attribute('src').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('src').value
331
341
  Nokogiri::XML(@image.as_html).child.attribute('width').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('width').value
332
342
  Nokogiri::XML(@image.as_html).child.attribute('height').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('height').value
343
+ Nokogiri::XML(@image.as_html).child.attribute('alt').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('alt').value
333
344
  end
334
345
  end
335
346
  end
336
347
 
348
+ describe 'StructuredText' do
349
+ before do
350
+ @structuredtext = Prismic::Fragments::StructuredText.new([
351
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", []),
352
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is a title, but not the highest", [], 3),
353
+ Prismic::Fragments::StructuredText::Block::Heading.new("Document's title", [], 1),
354
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", [])
355
+ ])
356
+ end
357
+ it 'finds the text of the first block' do
358
+ @structuredtext.blocks[0].text.should == "This is not a title"
359
+ end
360
+ it 'finds the right title if exists' do
361
+ @structuredtext.first_title.should == "Document's title"
362
+ end
363
+ it 'returns false if no title' do
364
+ @structuredtext.blocks[1] = Prismic::Fragments::StructuredText::Block::Text.new("This is not a title either", [])
365
+ @structuredtext.blocks[2] = Prismic::Fragments::StructuredText::Block::Text.new("And this is not a title either", [])
366
+ @structuredtext.first_title.should == false
367
+ end
368
+ end
369
+
337
370
  describe 'StructuredText::Heading' do
338
371
  before do
339
372
  @text = "This is a simple test."
@@ -393,7 +426,7 @@ end
393
426
 
394
427
  describe 'StructuredText::Image' do
395
428
  before do
396
- @view = Prismic::Fragments::Image::View.new('my_url', 10, 10)
429
+ @view = Prismic::Fragments::Image::View.new('my_url', 10, 10, "Aternative", "CC-BY")
397
430
  @image = Prismic::Fragments::StructuredText::Block::Image.new(@view)
398
431
  end
399
432
 
@@ -414,6 +447,18 @@ describe 'StructuredText::Image' do
414
447
  @image.height.should == @view.height
415
448
  end
416
449
  end
450
+
451
+ describe 'alt' do
452
+ it "returns the view's alt" do
453
+ @image.alt.should == @view.alt
454
+ end
455
+ end
456
+
457
+ describe 'copyright' do
458
+ it "returns the view's copyright" do
459
+ @image.copyright.should == @view.copyright
460
+ end
461
+ end
417
462
  end
418
463
 
419
464
  describe 'StructuredText::Hyperlink' do
@@ -141,6 +141,8 @@ describe 'image_parser' do
141
141
  "value": {
142
142
  "main": {
143
143
  "url": "url1",
144
+ "alt" : "Alternative",
145
+ "copyright" : "CC-BY",
144
146
  "dimensions": {
145
147
  "width": 500,
146
148
  "height": 500
@@ -149,6 +151,8 @@ describe 'image_parser' do
149
151
  "views": {
150
152
  "icon": {
151
153
  "url": "url2",
154
+ "alt" : "Alternative2",
155
+ "copyright" : "CC-0",
152
156
  "dimensions": {
153
157
  "width": 250,
154
158
  "height": 250
@@ -166,9 +170,13 @@ json
166
170
  image.main.url.should == "url1"
167
171
  image.main.width.should == 500
168
172
  image.main.height.should == 500
169
- image.views[0].url.should == "url2"
170
- image.views[0].width.should == 250
171
- image.views[0].height.should == 250
173
+ image.main.alt.should == "Alternative"
174
+ image.main.copyright.should == "CC-BY"
175
+ image.views['icon'].url.should == "url2"
176
+ image.views['icon'].width.should == 250
177
+ image.views['icon'].height.should == 250
178
+ image.views['icon'].alt.should == "Alternative2"
179
+ image.views['icon'].copyright.should == "CC-0"
172
180
  end
173
181
  end
174
182
 
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'LesBonnesChoses' do
5
+ before do
6
+ @api = Prismic.api("https://lesbonneschoses.prismic.io/api", nil)
7
+ @master_ref = @api.master_ref
8
+ end
9
+
10
+ describe '/api' do
11
+ it "API works" do
12
+ @api.should_not be_nil
13
+ end
14
+ end
15
+
16
+ describe 'query' do
17
+ it "queries everything and returns 20 documents" do
18
+ @api.create_search_form("everything").submit(@master_ref).size.should == 20
19
+ end
20
+
21
+ it "queries macarons (using a predicate) and returns 7 documents" do
22
+ @api.create_search_form("everything")
23
+ .query(%([[:d = any(document.tags, ["Macaron"])]]))
24
+ .submit(@master_ref).size.should == 7
25
+ end
26
+
27
+ it "queries macarons (using a form) and returns 7 documents" do
28
+ @api.create_search_form("macarons").submit(@master_ref).size.should == 7
29
+ end
30
+
31
+ it "queries macarons or cupcakes (using a form + a predicate) and returns 11 documents" do
32
+ @api.create_search_form("products")
33
+ .query(%([[:d = any(document.tags, ["Cupcake", "Macaron"])]]))
34
+ .submit(@master_ref).size.should == 11
35
+ end
36
+ end
37
+ end
data/spec/prismic_spec.rb CHANGED
@@ -5,7 +5,7 @@ describe 'Api' do
5
5
  before do
6
6
  json_representation = '{"foo": "bar"}'
7
7
  @oauth_initiate_url = "https://lesbonneschoses.prismic.io/auth"
8
- @api = Prismic::API.new(json_representation){|api|
8
+ @api = Prismic::API.new(json_representation, nil, Prismic::DefaultHTTPClient){|api|
9
9
  api.bookmarks = {}
10
10
  api.tags = {}
11
11
  api.types = {}
@@ -17,7 +17,10 @@ describe 'Api' do
17
17
  }
18
18
  api.forms = {
19
19
  'form1' => Prismic::Form.new(@api, 'form1', {}, nil, nil, nil, nil),
20
- 'form2' => Prismic::Form.new(@api, 'form2', {}, nil, nil, nil, nil),
20
+ 'form2' => Prismic::Form.new(@api, 'form2', {
21
+ 'q' => Prismic::Field.new('string', '[[any(document.type, [\"product\"])]]', true),
22
+ 'param1' => Prismic::Field.new('string', 'value1', false),
23
+ }, nil, nil, nil, nil),
21
24
  'form3' => Prismic::Form.new(@api, 'form3', {}, nil, nil, nil, nil),
22
25
  'form4' => Prismic::Form.new(@api, 'form4', {}, nil, nil, nil, nil),
23
26
  }
@@ -54,6 +57,14 @@ describe 'Api' do
54
57
  @form = @api.create_search_form('form2')
55
58
  @form.form.name.should == 'form2'
56
59
  end
60
+ it "store default value as simple value when the field is not repeatable" do
61
+ @form = @api.create_search_form('form2')
62
+ @form.data['param1'].should == 'value1'
63
+ end
64
+ it "store default value as array when the field is repeatable" do
65
+ @form = @api.create_search_form('form2')
66
+ @form.data['q'].should == ['[[any(document.type, [\"product\"])]]']
67
+ end
57
68
  end
58
69
 
59
70
  describe 'forms' do
@@ -72,20 +83,18 @@ describe 'Api' do
72
83
  before do
73
84
  @data = File.read("#{File.dirname(__FILE__)}/responses_mocks/api.json")
74
85
  @json = JSON.parse(@data)
75
- @parsed = Prismic::API.parse_api_response(@json)
86
+ @parsed = Prismic::API.parse_api_response(@json, nil, Prismic::DefaultHTTPClient)
76
87
  end
77
88
 
78
89
  it "does not allow to be created without master Ref" do
79
90
  expect {
80
- Prismic::API.parse_api_response({
81
- "refs" => [],
82
- })
91
+ Prismic::API.parse_api_response({"refs" => []}, nil, Prismic::DefaultHTTPClient)
83
92
  }.to raise_error(Prismic::API::BadPrismicResponseError, "No master Ref found")
84
93
  end
85
94
 
86
95
  it "does not allow to be created without any Ref" do
87
96
  expect {
88
- Prismic::API.parse_api_response({})
97
+ Prismic::API.parse_api_response({}, nil, Prismic::DefaultHTTPClient)
89
98
  }.to raise_error(Prismic::API::BadPrismicResponseError, "No refs given")
90
99
  end
91
100
 
@@ -225,6 +234,34 @@ describe 'Document' do
225
234
  }
226
235
  end
227
236
 
237
+ describe 'first_title' do
238
+ it "returns the right title" do
239
+ @document.fragments['field3'] = Prismic::Fragments::StructuredText.new([
240
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", []),
241
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is a title, but not the highest", [], 3),
242
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is the highest title of the fragment, but not the document", [], 2),
243
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", [])
244
+ ])
245
+ @document.fragments['field4'] = Prismic::Fragments::StructuredText.new([
246
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", []),
247
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is a title, but not the highest", [], 3),
248
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is the highest title of the document", [], 1),
249
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", [])
250
+ ])
251
+ @document.fragments['field5'] = Prismic::Fragments::StructuredText.new([
252
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", []),
253
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is a title, but not the highest", [], 3),
254
+ Prismic::Fragments::StructuredText::Block::Heading.new("This is the highest title of the fragment, but not the document", [], 2),
255
+ Prismic::Fragments::StructuredText::Block::Text.new("This is not a title", [])
256
+ ])
257
+ @document.first_title.should == "This is the highest title of the document"
258
+ end
259
+
260
+ it "returns false if no title" do
261
+ @document.first_title.should == false
262
+ end
263
+ end
264
+
228
265
  describe 'slug' do
229
266
  it "returns the first slug if found" do
230
267
  @document.slug.should == 'my-slug'
@@ -253,7 +290,7 @@ end
253
290
 
254
291
  describe 'SearchForm' do
255
292
  before do
256
- @field = Prismic::Field.new('String', ['foo'], true)
293
+ @field = Prismic::Field.new('String', 'foo', true)
257
294
  @api = nil
258
295
  end
259
296
 
@@ -265,7 +302,7 @@ describe 'SearchForm' do
265
302
  describe 'set() for queries' do
266
303
 
267
304
  it "append value for repeatable fields" do
268
- @field = Prismic::Field.new('String', ['foo'], true)
305
+ @field = Prismic::Field.new('String', 'foo', true)
269
306
  @form = create_form('q' => @field)
270
307
  @form.set('q', 'bar')
271
308
  @form.data.should == { 'q' => ['foo', 'bar'] } # test the 1st call
@@ -56,6 +56,8 @@
56
56
  "value": {
57
57
  "main": {
58
58
  "url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/0417110ebf2dc34a3e8b7b28ee4e06ac82473b70.png",
59
+ "alt" : "Alternative text to image",
60
+ "copyright" : "CC-BY",
59
61
  "dimensions": {
60
62
  "width": 500,
61
63
  "height": 500
@@ -64,6 +66,8 @@
64
66
  "views": {
65
67
  "icon": {
66
68
  "url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/babdc3421037f9af77720d8f5dcf1b84c912c6ba.png",
69
+ "alt" : "Alternative text to view",
70
+ "copyright" : "CC-BY",
67
71
  "dimensions": {
68
72
  "width": 250,
69
73
  "height": 250
@@ -60,6 +60,8 @@
60
60
  "value": {
61
61
  "main": {
62
62
  "url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/0417110ebf2dc34a3e8b7b28ee4e06ac82473b70.png",
63
+ "alt" : "Alternative text to image",
64
+ "copyright" : "CC-BY",
63
65
  "dimensions": {
64
66
  "width": 500,
65
67
  "height": 500
@@ -68,6 +70,8 @@
68
70
  "views": {
69
71
  "icon": {
70
72
  "url": "https://wroomio.s3.amazonaws.com/lesbonneschoses/babdc3421037f9af77720d8f5dcf1b84c912c6ba.png",
73
+ "alt" : "Alternative text to view",
74
+ "copyright" : "CC-BY",
71
75
  "dimensions": {
72
76
  "width": 250,
73
77
  "height": 250
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.preview.5
4
+ version: 1.0.0.preview.6
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: 2013-11-07 00:00:00.000000000 Z
12
+ date: 2013-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -100,6 +100,7 @@ files:
100
100
  - prismic.gemspec
101
101
  - spec/fragments_spec.rb
102
102
  - spec/json_parsers_spec.rb
103
+ - spec/lesbonneschoses_spec.rb
103
104
  - spec/prismic_spec.rb
104
105
  - spec/responses_mocks/api.json
105
106
  - spec/responses_mocks/document.json
@@ -134,6 +135,7 @@ summary: Prismic.io development kit
134
135
  test_files:
135
136
  - spec/fragments_spec.rb
136
137
  - spec/json_parsers_spec.rb
138
+ - spec/lesbonneschoses_spec.rb
137
139
  - spec/prismic_spec.rb
138
140
  - spec/responses_mocks/api.json
139
141
  - spec/responses_mocks/document.json