prismic.io 1.0.0.preview.3 → 1.0.0.preview.5

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: 7115c0e2648174904a54f5b9eca2dee638c334a6
4
- data.tar.gz: a6d17ff3c181ae1a273cbdc8956f114262854354
3
+ metadata.gz: 6e3f3af94b5219971b0b0dbdd62b195a0bb9a5ae
4
+ data.tar.gz: 157a7210a64dbf0e1a3f5de9b722b6222c7002a5
5
5
  SHA512:
6
- metadata.gz: 5316b6aac4442b9a90f505a7d949ca07f6b0ca970700e081837c618f10a5a41e280ac86aec1a9214a70e04b4631c41fa233063e86cdde89338a2195448259a62
7
- data.tar.gz: 98666ce830de416b0327a009cbb182df89acbe533eaf048a8d723b56fcbdd88ec303251c23771d28397295fc77347159bfa2beb1bfc86adc74ab57c646edb205
6
+ metadata.gz: 88eb3ea747a31a8226c3fb5f5c364e046e85e08013061510d7f5d55eb369bed64e56ef86bcb2f72220134975923fd7ea8becd557303823efc4e6ef9462cc3a95
7
+ data.tar.gz: 870e6c272bd7efe97346cdc4186e533a4332c5e2c940e30e183949278b89ccdf22d68942e3731d8d7f4ca61add02119b9ac60659ec7f332f7636a24f67c112d9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prismic.io (1.0.0.preview.3)
4
+ prismic.io (1.0.0.preview.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -30,7 +30,7 @@ require 'prismic'
30
30
 
31
31
  #### API Documentation
32
32
 
33
- You can browse the [API Documentation](http://prismicio.github.io/ruby-kit/).
33
+ You can browse the [API Documentation](https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation).
34
34
 
35
35
  #### Use it
36
36
 
data/lib/prismic/api.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  class API
3
4
  attr_reader :json, :access_token
@@ -27,6 +28,11 @@ module Prismic
27
28
  forms[name]
28
29
  end
29
30
 
31
+ def create_search_form(name, data={}, ref={})
32
+ form = self.form(name)
33
+ form and form.create_search_form(data, ref)
34
+ end
35
+
30
36
  def as_json
31
37
  @json
32
38
  end
@@ -56,16 +62,17 @@ module Prismic
56
62
  new(data, access_token) {|api|
57
63
  api.bookmarks = data['bookmarks']
58
64
  api.forms = Hash[data_forms.map { |k, form|
59
- [k, SearchForm.new(api, Form.new(
65
+ [k, Form.new(
66
+ api,
60
67
  form['name'],
61
68
  Hash[form['fields'].map { |k2, field|
62
- [k2, Field.new(field['type'], field['default'])]
69
+ [k2, Field.new(field['type'], field['default'], k2 == 'q')]
63
70
  }],
64
71
  form['method'],
65
72
  form['rel'],
66
73
  form['enctype'],
67
74
  form['action'],
68
- ))]
75
+ )]
69
76
  }]
70
77
  api.refs = Hash[data_refs.map { |ref|
71
78
  scheduled_at = ref['scheduledAt']
data/lib/prismic/form.rb CHANGED
@@ -1,8 +1,10 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  class Form
3
4
  attr_accessor :name, :form_method, :rel, :enctype, :action, :fields
4
5
 
5
- def initialize(name, fields, form_method, rel, enctype, action)
6
+ def initialize(api, name, fields, form_method, rel, enctype, action)
7
+ @api = api
6
8
  @name = name
7
9
  @fields = fields
8
10
  @form_method = form_method
@@ -12,8 +14,11 @@ module Prismic
12
14
  end
13
15
 
14
16
  def default_data
15
- Hash[fields.map{|key, field| [key, field.default] }.compact]
17
+ Hash[fields.map{|key, field| [key, field.default] if field.default }.compact]
16
18
  end
17
19
 
20
+ def create_search_form(data={}, ref=nil)
21
+ SearchForm.new(@api, self, data, ref)
22
+ end
18
23
  end
19
24
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Color
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Date
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Embed
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Image
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Link
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Multiple
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Number
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Select
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class StructuredText
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module Fragments
3
4
  class Text
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'prismic/fragments/block'
2
3
  require 'prismic/fragments/color'
3
4
  require 'prismic/fragments/date'
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
  module JsonParser
3
4
  class << self
@@ -165,7 +166,13 @@ module Prismic
165
166
  json['slugs'], fragments)
166
167
  end
167
168
 
168
- private
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')
173
+ document_parser(doc)
174
+ end
175
+ end
169
176
 
170
177
  private
171
178
 
@@ -1,5 +1,6 @@
1
+ # encoding: utf-8
1
2
  module Prismic
2
3
 
3
- VERSION = "1.0.0.preview.3"
4
+ VERSION = "1.0.0.preview.5"
4
5
 
5
6
  end
data/lib/prismic.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'net/http'
2
3
  require 'uri'
3
4
 
@@ -30,12 +31,13 @@ module Prismic
30
31
  end
31
32
 
32
33
  class SearchForm
33
- attr_accessor :api, :form, :data
34
+ attr_accessor :api, :form, :data, :ref
34
35
 
35
- def initialize(api, form, data={})
36
+ def initialize(api, form, data={}, ref=nil)
36
37
  @api = api
37
38
  @form = form
38
39
  @data = form.default_data.merge(data)
40
+ @ref = ref
39
41
  end
40
42
 
41
43
  def name
@@ -62,17 +64,18 @@ module Prismic
62
64
  form.fields
63
65
  end
64
66
 
65
- def submit(ref)
67
+ def submit(ref = @ref)
68
+ raise NoRefSetException unless @ref
69
+
66
70
  if form_method == "GET" && enctype == "application/x-www-form-urlencoded"
67
71
  data['ref'] = ref
68
- data.delete_if { |k, v| !v }
72
+ data['access_token'] = api.access_token if api.access_token
73
+ data.delete_if { |k, v| v.nil? }
69
74
 
70
75
  uri = URI(action)
71
76
  uri.query = URI.encode_www_form(data)
72
77
 
73
- request_uri = uri.request_uri
74
- request_uri += (request_uri =~ /\?/ ? '&' : '?') + "access_token=#{api.access_token}" if api.access_token
75
- request = Net::HTTP::Get.new(request_uri)
78
+ request = Net::HTTP::Get.new(uri.request_uri)
76
79
  request.add_field('Accept', 'application/json')
77
80
 
78
81
  response = Net::HTTP.new(uri.host, uri.port).start do |http|
@@ -83,35 +86,47 @@ module Prismic
83
86
 
84
87
  raise FormSearchException, "Error : #{response.body}" if response.code != "200"
85
88
 
86
- JSON.parse(response.body).map do |doc|
87
- raise FormSearchException, "Error : #{doc['error']}" if doc.include?('error')
88
- Prismic::JsonParser.document_parser(doc)
89
- end
89
+ Prismic::JsonParser.results_parser(JSON.parse(response.body))
90
90
  else
91
91
  raise UnsupportedFormKind, "Unsupported kind of form: #{form_method} / #{enctype}"
92
92
  end
93
93
  end
94
94
 
95
95
  def query(query)
96
- strip_brakets = ->(str) { str =~ /^\[(.*)\]$/ ? $1 : str }
97
- previous_query = form.fields['q'] ? form.fields['q'].default.to_s : ''
98
- data['q'] = "[%s%s]" % [strip_brakets.(previous_query), strip_brakets.(query)]
96
+ set('q', query)
99
97
  self
100
98
  end
101
99
 
100
+ def set(field_name, value)
101
+ field = @form.fields[field_name]
102
+ if field.repeatable?
103
+ data[field_name] = [] unless data.include? field_name
104
+ data[field_name] << value
105
+ else
106
+ data[field_name] = value
107
+ end
108
+ end
109
+
110
+ def ref(ref)
111
+ @ref = ref
112
+ end
113
+
114
+ class NoRefSetException < Error ; end
102
115
  class UnsupportedFormKind < Error ; end
103
116
  class RefNotFoundException < Error ; end
104
117
  class FormSearchException < Error ; end
105
118
  end
106
119
 
107
120
  class Field
108
- attr_accessor :field_type, :default
121
+ attr_accessor :field_type, :default, :repeatable
109
122
 
110
- def initialize(field_type, default)
123
+ def initialize(field_type, default, repeatable = false)
111
124
  @field_type = field_type
112
125
  @default = default
126
+ @repeatable = repeatable
113
127
  end
114
128
 
129
+ alias :repeatable? :repeatable
115
130
  end
116
131
 
117
132
  class Document
@@ -170,7 +185,6 @@ module Prismic
170
185
  def self.link_resolver(ref, &blk)
171
186
  LinkResolver.new(ref, &blk)
172
187
  end
173
-
174
188
  end
175
189
 
176
190
  require 'prismic/api'
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
 
3
4
  describe 'WebLink' do
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  describe 'document_link_parser' do
2
3
  before do
3
4
  raw_json = <<json
@@ -277,6 +278,22 @@ describe 'document_parser' do
277
278
  end
278
279
  end
279
280
 
281
+ describe 'results_parser' do
282
+
283
+ it "accepts results as an array" do
284
+ @json = JSON.parse('[]')
285
+ @results = Prismic::JsonParser.results_parser(@json)
286
+ @results.should == []
287
+ end
288
+
289
+ it "accepts results as an object" do
290
+ @json = JSON.parse('{"results":[]}')
291
+ @results = Prismic::JsonParser.results_parser(@json)
292
+ @results.should == []
293
+ end
294
+
295
+ end
296
+
280
297
  describe 'multiple_parser' do
281
298
  before do
282
299
  raw_json = <<json
data/spec/prismic_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
 
3
4
  describe 'Api' do
@@ -15,10 +16,10 @@ describe 'Api' do
15
16
  'key4' => Prismic::Ref.new('ref4', 'label4'),
16
17
  }
17
18
  api.forms = {
18
- 'form1' => Prismic::SearchForm.new(@api, Prismic::Form.new('form1', {}, nil, nil, nil, nil)),
19
- 'form2' => Prismic::SearchForm.new(@api, Prismic::Form.new('form2', {}, nil, nil, nil, nil)),
20
- 'form3' => Prismic::SearchForm.new(@api, Prismic::Form.new('form3', {}, nil, nil, nil, nil)),
21
- 'form4' => Prismic::SearchForm.new(@api, Prismic::Form.new('form4', {}, nil, nil, nil, nil)),
19
+ 'form1' => Prismic::Form.new(@api, 'form1', {}, nil, nil, nil, nil),
20
+ 'form2' => Prismic::Form.new(@api, 'form2', {}, nil, nil, nil, nil),
21
+ 'form3' => Prismic::Form.new(@api, 'form3', {}, nil, nil, nil, nil),
22
+ 'form4' => Prismic::Form.new(@api, 'form4', {}, nil, nil, nil, nil),
22
23
  }
23
24
  api.oauth = Prismic::API::OAuth.new(@oauth_initiate_url, "https://lesbonneschoses.prismic.io/auth/token")
24
25
  }
@@ -44,7 +45,14 @@ describe 'Api' do
44
45
 
45
46
  describe 'form' do
46
47
  it "return the right Form" do
47
- @api.form('form2').form.name.should == 'form2'
48
+ @api.form('form2').name.should == 'form2'
49
+ end
50
+ end
51
+
52
+ describe 'create_search_form' do
53
+ it "create a new search form for the right form" do
54
+ @form = @api.create_search_form('form2')
55
+ @form.form.name.should == 'form2'
48
56
  end
49
57
  end
50
58
 
@@ -157,6 +165,15 @@ describe 'Api' do
157
165
  @parsed.forms['pies'].fields['q'].default.should ==
158
166
  '[[at(document.tags, ["Pie"])][any(document.type, ["product"])]]'
159
167
  end
168
+
169
+ it "create fields (other than 'q') as non repeatable" do
170
+ @parsed.forms['pies'].fields['ref'].repeatable.should be_false
171
+ end
172
+
173
+ it "create 'q' fields as repeatable" do
174
+ @parsed.forms['pies'].fields['q'].repeatable.should be_true
175
+ end
176
+
160
177
  end
161
178
 
162
179
  describe 'as_json' do
@@ -236,34 +253,67 @@ end
236
253
 
237
254
  describe 'SearchForm' do
238
255
  before do
256
+ @field = Prismic::Field.new('String', ['foo'], true)
239
257
  @api = nil
240
- @field = Prismic::Field.new('String', '[foo]')
241
258
  end
242
259
 
243
- describe 'query' do
244
- it "adds the query to the form's data" do
245
- @form = Prismic::SearchForm.new(@api, Prismic::Form.new('form1', {}, nil, nil, nil, nil), {})
246
- @form.query('[bar]')
247
- @form.data.should == { 'q' => '[bar]' }
260
+ def create_form(fields)
261
+ form = Prismic::Form.new(nil, 'form1', fields, nil, nil, nil, nil)
262
+ form.create_search_form
263
+ end
264
+
265
+ describe 'set() for queries' do
266
+
267
+ it "append value for repeatable fields" do
268
+ @field = Prismic::Field.new('String', ['foo'], true)
269
+ @form = create_form('q' => @field)
270
+ @form.set('q', 'bar')
271
+ @form.data.should == { 'q' => ['foo', 'bar'] } # test the 1st call
272
+ @form.set('q', 'baz')
273
+ @form.data.should == { 'q' => ['foo', 'bar', 'baz'] } # test an other
248
274
  end
249
275
 
250
- it "adds existant non-nil queries (in fields) to the form's data" do
251
- @form = Prismic::SearchForm.new(@api, Prismic::Form.new('form1', {'q' => @field}, nil, nil, nil, nil), {})
252
- @form.query('[bar]')
253
- @form.data.should == { 'q' => '[foobar]' }
276
+ it "replace value for non repeatable fields" do
277
+ @field = Prismic::Field.new('String', 'foo', false)
278
+ @form = create_form('q' => @field)
279
+ @form.set('q', 'bar')
280
+ @form.data.should == { 'q' => 'bar' } # test the 1st call
281
+ @form.set('q', 'baz')
282
+ @form.data.should == { 'q' => 'baz' } # test an other
283
+ end
284
+
285
+ it "create value array for repeatable fields without value" do
286
+ @field = Prismic::Field.new('String', nil, true)
287
+ @form = create_form('q' => @field)
288
+ @form.set('q', 'bar')
289
+ @form.data.should == { 'q' => ['bar'] }
290
+ end
291
+
292
+ it "create value for non repeatable fields without value" do
293
+ @field = Prismic::Field.new('String', nil, false)
294
+ @form = create_form('q' => @field)
295
+ @form.set('q', 'bar')
296
+ @form.data.should == { 'q' => 'bar' }
254
297
  end
255
298
 
256
299
  it "returns the form itself" do
257
- @form = Prismic::SearchForm.new(@api, Prismic::Form.new('form1', {'q' => @field}, nil, nil, nil, nil), {})
258
- @form.query('[foo]').should == @form
300
+ @form = create_form('q' => @field)
301
+ @form.query('foo').should equal @form
259
302
  end
260
303
 
261
304
  it "merge user defined params into default ones" do
262
305
  field = ->(value){ Prismic::Field.new('String', value) }
263
306
  default_params = {'param1' => field.('a'), 'param2' => field.('b')}
264
- user_params = {'param1' => 'a2'}
265
- @form = Prismic::SearchForm.new(@api, Prismic::Form.new('form1', default_params, nil, nil, nil, nil), user_params)
307
+ @form = create_form(default_params)
308
+ @form.set('param1', 'a2')
266
309
  @form.data.should == {'param1' => 'a2', 'param2' => 'b'}
267
310
  end
268
311
  end
312
+
313
+ describe 'submit' do
314
+ it "raises an exception if no ref is set" do
315
+ @form = create_form('q' => @field)
316
+ expect { @form.submit }.to raise_error Prismic::SearchForm::NoRefSetException
317
+ end
318
+ end
269
319
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'nokogiri'
2
3
  begin
3
4
  require 'yajl'
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.3
4
+ version: 1.0.0.preview.5
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-10-18 00:00:00.000000000 Z
12
+ date: 2013-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler