prismic.io 1.0.0.preview.6 → 1.0.0.preview.7

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: 48b555e3c575061f3461b396c1739459780a690d
4
- data.tar.gz: ec67ac158b329572e7a65e76be77bf5464139c4f
3
+ metadata.gz: 9978093a5e4f4f3529a05df62cbead5aa3bbb31c
4
+ data.tar.gz: 9f89d25cd5a8affedcc997e4fefe1d62908df492
5
5
  SHA512:
6
- metadata.gz: b1f78ed016ec55f62dd5538f688f9ad887cfcb6cf7351840ba1ef2f7f85ab040ace75cd2caebb1546c4d43f1b856acea88d7f33b418a440a31e4910a1f299ba9
7
- data.tar.gz: b523de93f12d9c831ce127cad7629682d4cd4d481ef785c9eaac098e7e95c4b3f038e1392f6d28b021b7c1e47ab5c91a088f14d3c2c935eb88a45bdd93392499
6
+ metadata.gz: 078f0e8b7fa42fd89fbaa6e8af7a16639a5e707194d1815004f540b39efec152e9c7bba3507065f5991a2cfff1e9cf1127a11b56461a2304ae1eda4f54a73a5b
7
+ data.tar.gz: b34a4a54f34c285ee46ca0bd570dd4c7e8f66b6712372eda66b760a02cf484c3156bdf21724ba61873fc0ab8d6c1a5804199b336586edb3567a503e1b892f2c9
data/.travis.yml CHANGED
@@ -4,7 +4,6 @@ rvm:
4
4
  - 1.9.3
5
5
  - 1.9.2
6
6
  - jruby-19mode
7
- - rbx-19mode
8
7
  script: bundle exec rspec spec
9
8
  notifications:
10
9
  email:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prismic.io (1.0.0.preview.6)
4
+ prismic.io (1.0.0.preview.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Build Status](https://api.travis-ci.org/prismicio/ruby-kit.png)](https://travis-ci.org/prismicio/ruby-kit)
5
5
  [![Code Climate](https://codeclimate.com/github/prismicio/ruby-kit.png)](https://codeclimate.com/github/prismicio/ruby-kit)
6
6
 
7
- ### Getting Started
7
+ ### Getting Started with the gem
8
8
 
9
9
  #### Install using bundler
10
10
 
@@ -28,15 +28,26 @@ then add in your code:
28
28
  require 'prismic'
29
29
  ```
30
30
 
31
- #### Kit Documentation
31
+ #### Getting started with prismic.io
32
32
 
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:
33
+ On our [prismic.io developer's portal](https://developers.prismic.io/), on top of the complete documentation, you will:
34
+ * get a better understanding of [what prismic.io is and how to get started with it](https://developers.prismic.io/documentation/UjBaQsuvzdIHvE4D/getting-started).
35
+ * get a thorough understanding of [how prismic.io kits work](https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#kits-and-helpers), including this one. There's a couple of specificities for Ruby's case, described below.
36
+ * see [what else is available for Ruby](https://developers.prismic.io/technologies/UjBh6MuvzeMJvE4m/ruby).
34
37
 
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`.
38
+ #### Kit documentation
39
+
40
+ To get a detailed documentation of the Ruby kit's variables and methods, please check out the [prismic.io Ruby kit's documentation](http://prismicio.github.io/ruby-kit/) (click on the "Class list" tab at the top-right)
41
+
42
+ Need to see what changed, or to upgrade your kit? Check out [this kit's changelog](changelog.md).
43
+
44
+ ##### Ruby kit's specificities
45
+
46
+ This Ruby kit contains some mild differences and syntastic sugar over [the "Kits and helpers" section of our API documentation](https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#kits-and-helpers) (which you should read first). They are listed here:
47
+ * 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").ref(@ref).submit()`.
48
+ * When calling the API, a faster way to pass the `ref`: directly as a parameter of the `submit` method (no need to use the `ref` method then): `api.create_search_form("everything").submit(@ref)`.
49
+ * Accessing type-dependent fields from a `document` is done through the `[]` operator (rather than a `get()` method). Printing the HTML version of a field therefore looks like `document["title_user_friendly"].as_html(link_resolver(@ref)).html_safe`.
50
+ * Two of the fields in the `DocumentLink` object (the one used to write your `link_resolver` method, for instance) were renamed to fit Ruby's best practice: `doc.type` is in fact `doc.link_type`, and `doc.isBroken` is in fact `doc.broken?`.
40
51
 
41
52
  #### Use it
42
53
 
data/changelog.md ADDED
@@ -0,0 +1,19 @@
1
+ ## Changelog for prismic.io Ruby development kit
2
+
3
+
4
+ ### prismic.io.1.0.0.preview.7
5
+
6
+ #### Potentially breaking changes
7
+ * Boldness in `Prismic::Fragments::StructuredText#as_html` was represented as `<b>` tags, now as `<strong>` tags (262b5da9219cb990773a5b307f270f899b067ee5)
8
+
9
+ #### New features
10
+ * You had to call `Prismic::Link_resolver#link_to` on a `Prismic::Fragments::DocumentLink` object, but sometimes, you actually have the full `Prismic::Document`; you can now call it on both types of objects (b1a53141e1a962762e00bd904a68d6fcd5c3a6bc).
11
+ * You had to call `api.master_ref` to get the master ref; now you can call it `api.master`, just like in the other dev kits; both methods do the same thing (a9e11bbb735601734512a8cefca72ed29a6bdc81).
12
+ * New method: `Prismic::Fragments::StructuredText#as_text`, renders zero formatting, and ignores images and embeds (d4c8c751a28afef4ef2c2e0d943cf8066ef3c039).
13
+ * `Prismic::SearchForm` only had the `query` method to set the `q` parameter of the API call. Now those methods are dynamically created from the RESTful form found in the `/api` document, so it's now possible to use those methods too: `orderings`, `page`, `pageSize`, as well as all the future non-yet-existing ones (fcb4e3a92dbda1b6901a1065dcf0ef4144871fdd)
14
+
15
+
16
+ ### prismic.io.1.0.0.preview.6
17
+
18
+ #### Potentially breaking changes
19
+ * From an image, you had to call the view with its index, like this `image_object[0]`; now they're stored in a Hash, you have to call them with their proper name: `image_object['large']` (e6312b8d4486c8eacb1de7214563d45c61b5653c).
data/lib/prismic.rb CHANGED
@@ -13,7 +13,7 @@ module Prismic
13
13
  end
14
14
 
15
15
  # Return the full trace of the error (including nested errors)
16
- # @param e=self Parent error (don't use)
16
+ # @param [Exception] e Parent error (for internal use)
17
17
  #
18
18
  # @return [String] The trace
19
19
  def full_trace(e=self)
@@ -34,16 +34,19 @@ module Prismic
34
34
  #
35
35
  # The HTTP Client must responds to same method than {DefaultHTTPClient}.
36
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)
37
+ # @overload api(url)
38
+ # Simpler syntax (no configuration)
39
+ # @param [String] url The URL of the prismic.io repository
40
+ # @overload api(url, opts)
44
41
  # Standard use
42
+ # @param [String] url The URL of the prismic.io repository
43
+ # @param [Hash] opts The options
44
+ # @option opts [String] :access_token (nil) The access_token
45
+ # @option opts :http_client (DefaultHTTPClient) The HTTP client to use
45
46
  # @overload api(url, access_token)
46
47
  # Provide the access_token (only)
48
+ # @param [String] url The URL of the prismic.io repository
49
+ # @param [String] access_token The access token
47
50
  #
48
51
  # @return [API] The API instance related to this repository
49
52
  def self.api(url, opts=nil)
@@ -63,6 +66,18 @@ module Prismic
63
66
  # in the same way than regular HTML forms.
64
67
  #
65
68
  # Get SearchForm instance through the {API#create_search_form} method.
69
+ #
70
+ # The SearchForm instance contains helper methods for each predefined form's fields.
71
+ # Note that these methods are not be created if they risk to add confusion:
72
+ # - only letters, underscore and digits are authorized in the name
73
+ # - name starting with a digit or an underscore are forbidden
74
+ # - generated method can't override existing methods
75
+ #
76
+ # @example
77
+ # search_form = api.create_search_form('everything')
78
+ # search_form.page(3) # specify the field 'page'
79
+ # search_form.page_size("20") # specify the 'page_size' field
80
+ # results = search_form.submit(master_ref) # submit the search form
66
81
  class SearchForm
67
82
  attr_accessor :api, :form, :data, :ref
68
83
 
@@ -70,12 +85,49 @@ module Prismic
70
85
  @api = api
71
86
  @form = form
72
87
  @data = {}
88
+ form.fields.each { |name, _| create_field_helper_method(name) }
73
89
  form.default_data.each { |key, value| set(key, value) }
74
90
  data.each { |key, value| set(key, value) }
75
91
  @ref = ref
76
92
  end
77
93
 
78
- def name
94
+ # @!method query(query)
95
+ # Specify a query for this form.
96
+ # @param query [String] The query
97
+ # @return [SearchForm] self
98
+
99
+ # @!method orderings(orderings)
100
+ # Specify a orderings for this form.
101
+ # @param orderings [String] The orderings
102
+ # @return [SearchForm] self
103
+
104
+ # @!method page(page)
105
+ # Specify a page for this form.
106
+ # @param page [String,Fixum] The page
107
+ # @return [SearchForm] self
108
+
109
+ # @!method page_size(page_size)
110
+ # @generated
111
+ # Specify a page size for this form.
112
+ # @param page_size [String,Fixum] The page size
113
+ # @return [SearchForm] self
114
+
115
+ # Create the fields'helper methods
116
+ def create_field_helper_method(name)
117
+ return if name == 'ref'
118
+ return unless name =~ /\A[a-zA-Z][a-zA-Z0-9_]*\z/
119
+ meth_name = name.gsub(/([A-Z])/, '_\1').downcase
120
+ return if respond_to?(meth_name)
121
+ define_singleton_method(meth_name){|value| set(name, value) }
122
+ if name == 'q'
123
+ class << self
124
+ alias :query :q
125
+ end
126
+ end
127
+ end
128
+ private :create_field_helper_method
129
+
130
+ def form_name
79
131
  form.name
80
132
  end
81
133
 
@@ -83,19 +135,19 @@ module Prismic
83
135
  form.form_method
84
136
  end
85
137
 
86
- def rel
138
+ def form_rel
87
139
  form.rel
88
140
  end
89
141
 
90
- def enctype
142
+ def form_enctype
91
143
  form.enctype
92
144
  end
93
145
 
94
- def action
146
+ def form_action
95
147
  form.action
96
148
  end
97
149
 
98
- def fields
150
+ def form_fields
99
151
  form.fields
100
152
  end
101
153
 
@@ -108,17 +160,17 @@ module Prismic
108
160
  #
109
161
  # @param ref [Ref, String] The {Ref reference} to use (if not already defined)
110
162
  #
111
- # @return [type] [description]
163
+ # @return [Array<Document>] The results
112
164
  def submit(ref = nil)
113
165
  self.ref(ref) if ref
114
166
  raise NoRefSetException unless @ref
115
167
 
116
- if form_method == "GET" && enctype == "application/x-www-form-urlencoded"
168
+ if form_method == "GET" && form_enctype == "application/x-www-form-urlencoded"
117
169
  data['ref'] = @ref
118
170
  data['access_token'] = api.access_token if api.access_token
119
171
  data.delete_if { |k, v| v.nil? }
120
172
 
121
- response = api.http_client.get(action, data, 'Accept' => 'application/json')
173
+ response = api.http_client.get(form_action, data, 'Accept' => 'application/json')
122
174
 
123
175
  if response.code.to_s == "200"
124
176
  Prismic::JsonParser.results_parser(JSON.parse(response.body))
@@ -135,15 +187,6 @@ module Prismic
135
187
  end
136
188
  end
137
189
 
138
- # Specify a query for this form
139
- # @api
140
- # @param query [String] The query
141
- #
142
- # @return [SearchForm] self
143
- def query(query)
144
- set('q', query)
145
- end
146
-
147
190
  # Specify a parameter for this form
148
191
  # @param field_name [String] The parameter's name
149
192
  # @param value [String] The parameter's value
@@ -233,6 +276,13 @@ module Prismic
233
276
  title
234
277
  end
235
278
 
279
+ def [](field)
280
+ array = field.split('.')
281
+ raise ArgumentError, "Argument should contain one dot. Example: product.price" if array.length != 2
282
+ return nil if array[0] != self.type
283
+ fragments[array[1]]
284
+ end
285
+
236
286
  private
237
287
 
238
288
  def parse_fragments(fragments)
@@ -265,12 +315,17 @@ module Prismic
265
315
  @ref = ref
266
316
  @blk = blk
267
317
  end
268
- def link_to(doc_link)
269
- @blk.call(doc_link)
318
+ def link_to(doc)
319
+ if doc.is_a? Prismic::Fragments::DocumentLink
320
+ @blk.call(doc)
321
+ elsif doc.is_a? Prismic::Document
322
+ doc_link = Prismic::Fragments::DocumentLink.new(doc.id, doc.type, doc.tags, doc.slug, false)
323
+ @blk.call(doc_link)
324
+ end
270
325
  end
271
326
  end
272
327
 
273
- # Default HTTP client implementation, using the standard {Net::HTTP} library.
328
+ # Default HTTP client implementation, using the standard Net::HTTP library.
274
329
  module DefaultHTTPClient
275
330
  class << self
276
331
  # Performs a GET call and returns the result
data/lib/prismic/api.rb CHANGED
@@ -2,7 +2,14 @@
2
2
  module Prismic
3
3
  class API
4
4
  attr_reader :json, :access_token, :http_client
5
- attr_accessor :refs, :bookmarks, :forms, :master, :tags, :types, :oauth
5
+ attr_accessor :refs, :bookmarks, :forms, :tags, :types, :oauth
6
+
7
+ # Returns the master {Ref reference}
8
+ # @api
9
+ #
10
+ # @return [Ref] The master {Ref reference}
11
+ attr_accessor :master
12
+ alias :master_ref :master
6
13
 
7
14
  def initialize(json, access_token, http_client)
8
15
  @json = json
@@ -31,15 +38,6 @@ module Prismic
31
38
  refs[name.downcase]
32
39
  end
33
40
 
34
- # Returns the master {Ref reference}
35
- # @api
36
- #
37
- # @return [type] [description]
38
- def master_ref
39
- ref('master')
40
- end
41
-
42
-
43
41
  def form(name)
44
42
  forms[name]
45
43
  end
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+ require 'prismic/fragments/fragment'
2
3
  require 'prismic/fragments/block'
3
4
  require 'prismic/fragments/color'
4
5
  require 'prismic/fragments/date'
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Color
4
+ class Color < Fragment
5
5
  attr_accessor :value
6
6
 
7
7
  def initialize(value)
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Date
4
+ class Date < Fragment
5
5
  attr_accessor :value
6
6
 
7
7
  def initialize(value)
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Embed
4
+ class Embed < Fragment
5
5
  attr_accessor :embed_type, :provider, :url, :html, :o_embed_json
6
6
 
7
7
  def initialize(embed_type, provider, url, html, o_embed_json)
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module Prismic
3
+ module Fragments
4
+ # Generic fragment, this class is to be extended by all fragment classes.
5
+ class Fragment
6
+ # Generic as_html method for fragments, meant to be overriden by
7
+ # specific fragment classes.
8
+ #
9
+ # @return [String] an empty string
10
+ def as_html(link_resolver = nil)
11
+ raise NotImplementedError, "Method #{__method__} is not implemented for #{inspect}", caller
12
+ end
13
+ # Generic as_text method for fragments, meant to be overriden by
14
+ # specific fragment classes.
15
+ #
16
+ # @return [String] an empty string
17
+ def as_text()
18
+ raise NotImplementedError, "Method #{__method__} is not implemented for #{inspect}", caller
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Image
4
+ class Image < Fragment
5
5
  attr_accessor :main, :views
6
6
 
7
7
  def initialize(main, views)
@@ -25,7 +25,7 @@ module Prismic
25
25
 
26
26
  class ViewDoesNotExistException < Error ; end
27
27
 
28
- class View
28
+ class View < Fragment
29
29
  attr_accessor :url, :width, :height, :alt, :copyright
30
30
 
31
31
  def initialize(url, width, height, alt, copyright)
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Link
4
+ class Link < Fragment
5
5
  def as_html(link_resolver=nil)
6
6
  %(<a href="#@url">#@url</a>)
7
7
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Multiple
4
+ class Multiple < Fragment
5
5
  attr_reader :fragments
6
6
 
7
7
  def initialize(fragments=[])
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Number
4
+ class Number < Fragment
5
5
  attr_accessor :value
6
6
 
7
7
  def initialize(value)
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Select
4
+ class Select < Fragment
5
5
  attr_accessor :value
6
6
 
7
7
  def initialize(value)
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class StructuredText
4
+ class StructuredText < Fragment
5
5
  class Group
6
6
  attr_reader :kind, :blocks
7
7
  def initialize(kind)
@@ -47,6 +47,15 @@ module Prismic
47
47
  }.join("\n\n")
48
48
  end
49
49
 
50
+ # Returns the StructuredText as plain text, with zero formatting.
51
+ # Non-textual blocks (like images and embeds) are simply ignored.
52
+ #
53
+ # @param separator [String] The string separator inserted between the blocks (a blank space by default)
54
+ # @return [String] The complete string representing the textual value of the StructuredText field.
55
+ def as_text(separator=' ')
56
+ blocks.map{|block| block.as_text }.compact.join(separator)
57
+ end
58
+
50
59
  # Finds the first highest title in a structured text
51
60
  def first_title
52
61
  max_level = 6 # any title with a higher level kicks the current one out
@@ -81,10 +90,10 @@ module Prismic
81
90
 
82
91
  class Strong < Span
83
92
  def start_html(link_resolver=nil)
84
- "<b>"
93
+ "<strong>"
85
94
  end
86
95
  def end_html(link_resolver=nil)
87
- "</b>"
96
+ "</strong>"
88
97
  end
89
98
  end
90
99
 
@@ -105,6 +114,15 @@ module Prismic
105
114
  end
106
115
 
107
116
  class Block
117
+
118
+ # Returns nil, as a block is not textual by default.
119
+ # This is meant to be overriden by textual blocks (see Prismic::Fragments::StructuredText::Block::Text.as_text, for instance)
120
+ #
121
+ # @return nil, always.
122
+ def as_text
123
+ nil
124
+ end
125
+
108
126
  class Text
109
127
  attr_accessor :text, :spans
110
128
 
@@ -139,6 +157,13 @@ module Prismic
139
157
  end
140
158
  [@start_spans, @end_spans]
141
159
  end
160
+
161
+ # Zero-formatted textual value of the block.
162
+ #
163
+ # @return The textual value.
164
+ def as_text
165
+ @text
166
+ end
142
167
  end
143
168
 
144
169
  class Heading < Text
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
  module Fragments
4
- class Text
4
+ class Text < Fragment
5
5
  attr_accessor :value
6
6
 
7
7
  def initialize(value)
@@ -11,6 +11,10 @@ module Prismic
11
11
  def as_html(link_resolver=nil)
12
12
  %(<span class="text">#{CGI::escapeHTML(@value)}</span>)
13
13
  end
14
+
15
+ def as_text
16
+ @value
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
 
4
- VERSION = "1.0.0.preview.6"
4
+ VERSION = "1.0.0.preview.7"
5
5
 
6
6
  end
@@ -23,13 +23,23 @@ describe 'WebLink' do
23
23
  Nokogiri::XML(@web_link.as_html).child.content.should == 'my_url'
24
24
  end
25
25
  end
26
+
27
+ describe 'as_text' do
28
+ before do
29
+ @web_link = Prismic::Fragments::WebLink.new('my_url')
30
+ end
31
+ it 'raises an NotImplementedError' do
32
+ expect { @web_link.as_text }.to raise_error NotImplementedError
33
+ end
34
+ end
26
35
  end
27
36
 
28
37
  describe 'MediaLink' do
38
+ before do
39
+ @media_link = Prismic::Fragments::MediaLink.new('my_url')
40
+ end
41
+
29
42
  describe 'as_html' do
30
- before do
31
- @media_link = Prismic::Fragments::MediaLink.new('my_url')
32
- end
33
43
 
34
44
  it "returns an <a> HTML element" do
35
45
  Nokogiri::XML(@media_link.as_html).child.name.should == 'a'
@@ -47,13 +57,20 @@ describe 'MediaLink' do
47
57
  Nokogiri::XML(@media_link.as_html).child.content.should == 'my_url'
48
58
  end
49
59
  end
60
+
61
+ describe 'as_text' do
62
+ it 'raises an NotImplementedError' do
63
+ expect { @media_link.as_text }.to raise_error NotImplementedError
64
+ end
65
+ end
50
66
  end
51
67
 
52
68
  describe 'Text' do
69
+ before do
70
+ @text = Prismic::Fragments::Text.new('my_value')
71
+ end
72
+
53
73
  describe 'as_html' do
54
- before do
55
- @text = Prismic::Fragments::Text.new('my_value')
56
- end
57
74
 
58
75
  it "returns a <span> HTML element" do
59
76
  Nokogiri::XML(@text.as_html).child.name.should == 'span'
@@ -72,13 +89,20 @@ describe 'Text' do
72
89
  @text.as_html.should =~ /^<[^>]+>&amp;my &lt;value&gt; #abcde<[^>]+>$/
73
90
  end
74
91
  end
92
+
93
+ describe 'as_text' do
94
+ it 'return the value' do
95
+ @text.as_text.should == 'my_value'
96
+ end
97
+ end
75
98
  end
76
99
 
77
100
  describe 'Select' do
101
+ before do
102
+ @select = Prismic::Fragments::Select.new('my_value')
103
+ end
104
+
78
105
  describe 'as_html' do
79
- before do
80
- @select = Prismic::Fragments::Select.new('my_value')
81
- end
82
106
 
83
107
  it "returns a <span> HTML element" do
84
108
  Nokogiri::XML(@select.as_html).child.name.should == 'span'
@@ -97,6 +121,12 @@ describe 'Select' do
97
121
  @select.as_html(nil).should =~ %r{^<[^>]+>&amp;my &lt;value&gt; #abcde<[^>]+>$}
98
122
  end
99
123
  end
124
+
125
+ describe 'as_text' do
126
+ it 'raises an NotImplementedError' do
127
+ expect { @select.as_text }.to raise_error NotImplementedError
128
+ end
129
+ end
100
130
  end
101
131
 
102
132
  describe 'Date' do
@@ -113,6 +143,12 @@ describe 'Date' do
113
143
  Nokogiri::XML(@date.as_html).child.content.should == '2013-08-07T11:13:07.000+02:00'
114
144
  end
115
145
  end
146
+
147
+ describe 'as_text' do
148
+ it 'raises an NotImplementedError' do
149
+ expect { @date.as_text }.to raise_error NotImplementedError
150
+ end
151
+ end
116
152
  end
117
153
 
118
154
  describe 'Number' do
@@ -143,6 +179,12 @@ describe 'Number' do
143
179
  Nokogiri::XML(@number.as_html).child.content.should == 10.2.to_s
144
180
  end
145
181
  end
182
+
183
+ describe 'as_text' do
184
+ it 'raises an NotImplementedError' do
185
+ expect { @number.as_text }.to raise_error NotImplementedError
186
+ end
187
+ end
146
188
  end
147
189
 
148
190
  describe 'Color' do
@@ -213,6 +255,12 @@ describe 'Color' do
213
255
  end
214
256
  end
215
257
 
258
+ describe 'as_text' do
259
+ it 'raises an NotImplementedError' do
260
+ expect { @color.as_text }.to raise_error NotImplementedError
261
+ end
262
+ end
263
+
216
264
  describe 'self.valid?' do
217
265
  it "returns true if the color is valid" do
218
266
  Prismic::Fragments::Color.valid?(@hex_value).should be_true
@@ -268,6 +316,12 @@ describe 'Embed' do
268
316
  Nokogiri::XML(@embed.as_html).child.content.should == 'my_html'
269
317
  end
270
318
  end
319
+
320
+ describe 'as_text' do
321
+ it 'raises an NotImplementedError' do
322
+ expect { @embed.as_text }.to raise_error NotImplementedError
323
+ end
324
+ end
271
325
  end
272
326
 
273
327
  describe 'Image::View' do
@@ -311,6 +365,12 @@ describe 'Image::View' do
311
365
  # Nokogiri::XML(@view.as_html).child.attribute('alt').should == nil
312
366
  # end
313
367
  end
368
+
369
+ describe 'as_text' do
370
+ it 'raises an NotImplementedError' do
371
+ expect { @view.as_text }.to raise_error NotImplementedError
372
+ end
373
+ end
314
374
  end
315
375
 
316
376
  describe 'Image' do
@@ -343,6 +403,12 @@ describe 'Image' do
343
403
  Nokogiri::XML(@image.as_html).child.attribute('alt').value.should == Nokogiri::XML(@main_view.as_html).child.attribute('alt').value
344
404
  end
345
405
  end
406
+
407
+ describe 'as_text' do
408
+ it 'raises an NotImplementedError' do
409
+ expect { @image.as_text }.to raise_error NotImplementedError
410
+ end
411
+ end
346
412
  end
347
413
 
348
414
  describe 'StructuredText' do
@@ -378,12 +444,12 @@ describe 'StructuredText::Heading' do
378
444
  it 'generates valid h1 html' do
379
445
  @heading = 1
380
446
  @block = Prismic::Fragments::StructuredText::Block::Heading.new(@text, @spans, @heading)
381
- @block.as_html(nil).should == "<h1>This <em>is</em> <b>a</b> simple test.</h1>"
447
+ @block.as_html(nil).should == "<h1>This <em>is</em> <strong>a</strong> simple test.</h1>"
382
448
  end
383
449
  it 'generates valid h2 html' do
384
450
  @heading = 2
385
451
  @block = Prismic::Fragments::StructuredText::Block::Heading.new(@text, @spans, @heading)
386
- @block.as_html(nil).should == "<h2>This <em>is</em> <b>a</b> simple test.</h2>"
452
+ @block.as_html(nil).should == "<h2>This <em>is</em> <strong>a</strong> simple test.</h2>"
387
453
  end
388
454
  end
389
455
 
@@ -397,7 +463,7 @@ describe 'StructuredText::Paragraph' do
397
463
  @block = Prismic::Fragments::StructuredText::Block::Paragraph.new(@text, @spans)
398
464
  end
399
465
  it 'generates valid html' do
400
- @block.as_html(nil).should == "<p>This <em>is</em> <b>a</b> simple test.</p>"
466
+ @block.as_html(nil).should == "<p>This <em>is</em> <strong>a</strong> simple test.</p>"
401
467
  end
402
468
  it "espaces HTML content" do
403
469
  @text = '&my <value> #abcde'
@@ -406,7 +472,7 @@ describe 'StructuredText::Paragraph' do
406
472
  Prismic::Fragments::StructuredText::Span::Strong.new(0, 1),
407
473
  ]
408
474
  @block = Prismic::Fragments::StructuredText::Block::Paragraph.new(@text, @spans)
409
- @block.as_html(nil).should =~ %r{^<[^>]+><b>&amp;</b>my <em>&lt;value&gt;</em> #abcde<[^>]+>$}
475
+ @block.as_html(nil).should =~ %r{^<[^>]+><strong>&amp;</strong>my <em>&lt;value&gt;</em> #abcde<[^>]+>$}
410
476
  end
411
477
  end
412
478
 
@@ -420,7 +486,7 @@ describe 'StructuredText::Preformatted' do
420
486
  @block = Prismic::Fragments::StructuredText::Block::Preformatted.new(@text, @spans)
421
487
  end
422
488
  it 'generates valid html' do
423
- @block.as_html(nil).should == "<pre>This <em>is</em> <b>a</b> simple test.</pre>"
489
+ @block.as_html(nil).should == "<pre>This <em>is</em> <strong>a</strong> simple test.</pre>"
424
490
  end
425
491
  end
426
492
 
@@ -485,10 +551,6 @@ describe 'StructuredText::Hyperlink' do
485
551
  end
486
552
  end
487
553
 
488
- describe 'DocumentLink' do
489
- describe 'as_html'
490
- end
491
-
492
554
  describe 'Multiple' do
493
555
  before do
494
556
  @multiple = Prismic::Fragments::Multiple.new
@@ -34,4 +34,38 @@ describe 'LesBonnesChoses' do
34
34
  .submit(@master_ref).size.should == 11
35
35
  end
36
36
  end
37
+
38
+ describe 'API::Document' do
39
+ before do
40
+ @document = @api.create_search_form('everything').query(%([[:d = at(document.id, "UkL0gMuvzYUANCpf")]])).submit(@master_ref)[0]
41
+ end
42
+
43
+ it 'Operator [] works on document' do
44
+ @document['job-offer.name'].as_html(nil).should == '<h1>Pastry Dresser</h1>'
45
+ end
46
+
47
+ it 'Operator [] returns nil if wrong type' do
48
+ @document['product.name'].should == nil
49
+ end
50
+
51
+ it 'Operator [] raises error if field is nonsense' do
52
+ expect {
53
+ @document['blablabla']
54
+ }.to raise_error(ArgumentError, "Argument should contain one dot. Example: product.price")
55
+ end
56
+ end
57
+
58
+ describe 'fragments' do
59
+ it "returns a correct as_text on a StructuredText" do
60
+ @api.create_search_form("everything")
61
+ .query(%([[:d = at(document.id, "UkL0gMuvzYUANCps")]]))
62
+ .submit(@master_ref)[0].fragments['body'].as_text.should == "The end of a chapter the beginning of a new one Jean-Michel Pastranova, the founder of Les Bonnes Choses, and creator of the whole concept of modern fine pastry, has decided to step down as the CEO and the Director of Workshops of Les Bonnes Choses, to focus on other projects, among which his now best-selling pastry cook books, but also to take on a primary role in a culinary television show to be announced later this year. \"I believe I've taken the Les Bonnes Choses concept as far as it can go. Les Bonnes Choses is already an entity that is driven by its people, thanks to a strong internal culture, so I don't feel like they need me as much as they used to. I'm sure they are greater ways to come, to innovate in pastry, and I'm sure Les Bonnes Choses's coming innovation will be even more mind-blowing than if I had stayed longer.\" He will remain as a senior advisor to the board, and to the workshop artists, as his daughter Selena, who has been working with him for several years, will fulfill the CEO role from now on. \"My father was able not only to create a revolutionary concept, but also a company culture that puts everyone in charge of driving the company's innovation and quality. That gives us years, maybe decades of revolutionary ideas to come, and there's still a long, wonderful path to walk in the fine pastry world.\""
63
+ end
64
+
65
+ it "returns a correct as_text on a StructuredText with a separator" do
66
+ @api.create_search_form("everything")
67
+ .query(%([[:d = at(document.id, "UkL0gMuvzYUANCps")]]))
68
+ .submit(@master_ref)[0].fragments['body'].as_text(' #### ').should == "The end of a chapter the beginning of a new one #### Jean-Michel Pastranova, the founder of Les Bonnes Choses, and creator of the whole concept of modern fine pastry, has decided to step down as the CEO and the Director of Workshops of Les Bonnes Choses, to focus on other projects, among which his now best-selling pastry cook books, but also to take on a primary role in a culinary television show to be announced later this year. #### \"I believe I've taken the Les Bonnes Choses concept as far as it can go. Les Bonnes Choses is already an entity that is driven by its people, thanks to a strong internal culture, so I don't feel like they need me as much as they used to. I'm sure they are greater ways to come, to innovate in pastry, and I'm sure Les Bonnes Choses's coming innovation will be even more mind-blowing than if I had stayed longer.\" #### He will remain as a senior advisor to the board, and to the workshop artists, as his daughter Selena, who has been working with him for several years, will fulfill the CEO role from now on. #### \"My father was able not only to create a revolutionary concept, but also a company culture that puts everyone in charge of driving the company's innovation and quality. That gives us years, maybe decades of revolutionary ideas to come, and there's still a long, wonderful path to walk in the fine pastry world.\""
69
+ end
70
+ end
37
71
  end
data/spec/prismic_spec.rb CHANGED
@@ -46,6 +46,18 @@ describe 'Api' do
46
46
  end
47
47
  end
48
48
 
49
+ describe 'master_ref' do
50
+ it "returns the right Ref" do
51
+ @api.master_ref.label.should == 'label3'
52
+ end
53
+ end
54
+
55
+ describe 'master' do
56
+ it "returns the right Ref" do
57
+ @api.master.label.should == 'label3'
58
+ end
59
+ end
60
+
49
61
  describe 'form' do
50
62
  it "return the right Form" do
51
63
  @api.form('form2').name.should == 'form2'
@@ -222,6 +234,25 @@ describe 'Api' do
222
234
 
223
235
  end
224
236
 
237
+ describe 'LinkResolver' do
238
+ before do
239
+ @doc_link = Prismic::Fragments::DocumentLink.new('id', 'blog-post', ['tag1', 'tag2'], 'my-slug', false)
240
+ @document = Prismic::Document.new('id', 'blog-post', nil, ['tag1', 'tag2'], ['my-slug', 'my-other-slug'], nil)
241
+
242
+ @link_resolver = Prismic::LinkResolver.new(nil) do |doc|
243
+ '/'+doc.link_type+'/'+doc.id+'/'+doc.slug
244
+ end
245
+ end
246
+
247
+ it "builds the right URL from a DocumentLink" do
248
+ @link_resolver.link_to(@doc_link).should == '/blog-post/id/my-slug'
249
+ end
250
+
251
+ it "builds the right URL from a Document" do
252
+ @link_resolver.link_to(@document).should == '/blog-post/id/my-slug'
253
+ end
254
+ end
255
+
225
256
  describe 'Document' do
226
257
  before do
227
258
  fragments = {
@@ -299,6 +330,46 @@ describe 'SearchForm' do
299
330
  form.create_search_form
300
331
  end
301
332
 
333
+ describe 'fields methods' do
334
+
335
+ it "should be created for each valid field names" do
336
+ @form = create_form('a_param' => @field)
337
+ @form.should respond_to(:a_param)
338
+ end
339
+
340
+ it "should be created for each valid field names with number" do
341
+ @form = create_form('a_param2' => @field)
342
+ @form.should respond_to(:a_param2)
343
+ end
344
+
345
+ it "should be created for each camelCase field names" do
346
+ @form = create_form('anExampleParam0A0B' => @field)
347
+ @form.should respond_to(:an_example_param0_a0_b)
348
+ end
349
+
350
+ it "should not be created for field names begining with a number" do
351
+ @form = create_form('2param' => @field)
352
+ @form.should_not respond_to(:'2param')
353
+ end
354
+
355
+ it "should not be created for field names begining with an underscore" do
356
+ @form = create_form('_param' => @field)
357
+ @form.should_not respond_to(:'_param')
358
+ end
359
+
360
+ it "should not be created for field names containing invalid characters" do
361
+ @form = create_form('a-param' => @field)
362
+ @form.should_not respond_to(:'a-param')
363
+ end
364
+
365
+ it "should not be created if a method with same name already exists" do
366
+ Prismic::SearchForm.module_exec { def param_example_for_tests() "ok" end }
367
+ @form = create_form('param_example_for_tests' => @field)
368
+ @form.param_example_for_tests.should == "ok"
369
+ end
370
+
371
+ end
372
+
302
373
  describe 'set() for queries' do
303
374
 
304
375
  it "append value for repeatable fields" do
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.6
4
+ version: 1.0.0.preview.7
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-27 00:00:00.000000000 Z
12
+ date: 2013-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -80,6 +80,7 @@ files:
80
80
  - Gemfile.lock
81
81
  - README.md
82
82
  - Rakefile
83
+ - changelog.md
83
84
  - lib/prismic.rb
84
85
  - lib/prismic/api.rb
85
86
  - lib/prismic/form.rb
@@ -88,6 +89,7 @@ files:
88
89
  - lib/prismic/fragments/color.rb
89
90
  - lib/prismic/fragments/date.rb
90
91
  - lib/prismic/fragments/embed.rb
92
+ - lib/prismic/fragments/fragment.rb
91
93
  - lib/prismic/fragments/image.rb
92
94
  - lib/prismic/fragments/link.rb
93
95
  - lib/prismic/fragments/multiple.rb
@@ -128,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  version: 1.3.1
129
131
  requirements: []
130
132
  rubyforge_project:
131
- rubygems_version: 2.0.6
133
+ rubygems_version: 2.1.11
132
134
  signing_key:
133
135
  specification_version: 4
134
136
  summary: Prismic.io development kit
@@ -143,4 +145,3 @@ test_files:
143
145
  - spec/responses_mocks/structured_text_heading.json
144
146
  - spec/responses_mocks/structured_text_paragraph.json
145
147
  - spec/spec_helper.rb
146
- has_rdoc: