prismic_rails 0.3.0 → 1.0.0

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: b661aa54deff7c406f834ddd6da31318cbb75d4f
4
- data.tar.gz: ba0aecffc2202919bea3da0a318a98b476401b3b
3
+ metadata.gz: 020d3bd11b1873d59943f9bd97178578c350b53a
4
+ data.tar.gz: 00b2e319e63e1f4550d443ee960dfee32375d85c
5
5
  SHA512:
6
- metadata.gz: bb3cb78090badadb3370356ec4b12030e17f4ad7b991d3efadcac1d6cf928a541c9426e39add6aa9be53e80d7a83c2aaa576a8f9650ba67df7cdc7b8c3061d20
7
- data.tar.gz: 7b08ab8b08a078073a668e63d8db3e408580cb81933924fe12603bb38f41ec14be78a4bafdeda3cf196067549ce81810c10c942d4711515469f1c72abb9e991b
6
+ metadata.gz: 16594c30edb833d0b6a835680da5cc35ec818a41ff9ec0ae69fec7504ddbf1d139d7dbf5d3e521a821b98e74ba9a45ce687c8c86429ce1594e7d607f289c938f
7
+ data.tar.gz: 1279b6f79edd3aef343c6e3e740127d15c63db142a0a017b97d677ebae2cb901706c22f257e6266c6c280063434058a03ae5153ccd9b93e74c3001933e5df08b
data/.env CHANGED
@@ -1 +1,2 @@
1
1
  PRISMIC_API_URL='https://prismic-rails.prismic.io/api'
2
+ PRISMIC_ACCESS_TOKEN='MC5Xb1FQa3lzQUFLMVN3RVh4.ZXo4Uu-_ve-_vXXvv73vv73vv71echEp77-9EzFx77-9MO-_vSXvv71MEO-_vScZ77-977-9XO-_vQ'
data/bin/console CHANGED
@@ -1,7 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "prismicRails"
3
+ require 'dotenv'
4
+ require 'bundler/setup'
5
+ require 'pry'
6
+ require 'rails'
7
+ require 'prismic_rails'
5
8
 
6
9
  # You can add fixtures and/or initialization code here to make experimenting
7
10
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +13,13 @@ require "prismicRails"
10
13
  # require "pry"
11
14
  # Pry.start
12
15
 
13
- require "irb"
16
+ Dotenv.load
17
+
18
+ PrismicRails.configure do |config|
19
+ config.url = ENV.fetch("PRISMIC_API_URL", "")
20
+ config.token = ENV.fetch("PRISMIC_ACCESS_TOKEN", nil)
21
+ end
22
+
23
+ require 'irb'
14
24
  IRB.start(__FILE__)
25
+
@@ -6,15 +6,15 @@ PrismicRails.configure do |config|
6
6
  # Set the access token of prismic if you have one
7
7
  config.token = ENV["PRISMIC_ACCESS_TOKEN"]
8
8
 
9
- # Language machting of your app locale to prismic locale:
9
+ ## Language machting of your app locale to prismic locale:
10
10
  # Example
11
11
  # config.languages = {
12
- # 'en' => 'en-gb',
12
+ # 'en' => 'en-us',
13
13
  # 'de' => 'de-ch',
14
14
  # 'fr' => 'fr-ch',
15
15
  # 'it' => 'it-ch'
16
16
  # }
17
- #
18
- # Set if PrismicRails should use rails caching
17
+
18
+ ## Set if PrismicRails should use rails caching
19
19
  # config.caching = true
20
20
  end
@@ -12,12 +12,12 @@ module PrismicRails
12
12
  end
13
13
 
14
14
  # Returns the document as safe html
15
- def to_html
16
- @document.as_html(nil).html_safe
15
+ def as_html(serializer = nil)
16
+ @document.as_html(serializer)
17
17
  end
18
18
 
19
19
  # Returns only the text of a document
20
- def to_text
20
+ def as_text
21
21
  @document.as_text
22
22
  end
23
23
 
@@ -26,7 +26,11 @@ module PrismicRails
26
26
  # +type+ 'text', 'image' etc
27
27
  def find_fragment(type)
28
28
  fragment = @document.fragments[type]
29
- return PrismicRails::Fragment.new(fragment) || NilDocument.new
29
+ if fragment
30
+ PrismicRails::Fragment.new(fragment)
31
+ else
32
+ NilDocument.new
33
+ end
30
34
  end
31
35
 
32
36
  # Tests if the document has the type type.
@@ -10,12 +10,12 @@ module PrismicRails
10
10
  end
11
11
 
12
12
  # Returns the document as safe html
13
- def to_html
14
- @fragment.as_html(nil).html_safe
13
+ def as_html(serializer = nil)
14
+ @fragment.as_html(serializer)
15
15
  end
16
16
 
17
17
  # Returns only the text of a document
18
- def to_text
18
+ def as_text
19
19
  @fragment.as_text
20
20
  end
21
21
 
@@ -10,15 +10,19 @@ module PrismicRails
10
10
  end
11
11
 
12
12
  # Returns the document as safe html, in this case simply an empty string
13
- def to_html
13
+ def as_html(serializer = nil)
14
14
  @document
15
15
  end
16
16
 
17
17
  # Returns the document as text, in this case simply an empty string
18
- def to_text
18
+ def as_text
19
19
  @document
20
20
  end
21
21
 
22
+ def find_fragment(type)
23
+ PrismicRails::Fragment.new(PrismicRails::NilDocument.new)
24
+ end
25
+
22
26
  # Returns the type nil of the NilDocument
23
27
  def type
24
28
  nil
@@ -13,12 +13,13 @@ module PrismicRails
13
13
  # ==== Attributes
14
14
  # +response+ The response of the Prismic API query
15
15
  def initialize(response)
16
- if response.results
16
+ if response && response.results && !response.results.empty?
17
17
  @documents = response.results.map do |document|
18
18
  PrismicRails::Document.new(document)
19
19
  end
20
- else #Handles the case if the response is nil
21
- @documents = []
20
+ else #Handles the case if the response is nil or empty
21
+ nil_document = PrismicRails::NilDocument.new
22
+ @documents = [nil_document]
22
23
  end
23
24
  end
24
25
 
@@ -35,5 +36,13 @@ module PrismicRails
35
36
  end
36
37
  end
37
38
 
39
+ def first
40
+ @documents.first
41
+ end
42
+
43
+ def last
44
+ @documents.last
45
+ end
46
+
38
47
  end
39
48
  end
@@ -33,7 +33,7 @@ module PrismicRails
33
33
  def prismic_type(type, options = {}, &block)
34
34
 
35
35
  query = Proc.new do
36
- result = PrismicRails::QueryService.type(type, options)
36
+ result = PrismicRails::QueryService.by_type(type, options)
37
37
  capture(result, &block)
38
38
  end
39
39
 
@@ -14,8 +14,6 @@ module PrismicRails
14
14
  PrismicRails.configure do |config|
15
15
  config.url = app.config.prismic_rails[:url]
16
16
  config.token = app.config.prismic_rails[:token]
17
- config.client_id = app.config.prismic_rails[:client_id]
18
- config.client_secret = app.config.prismic_rails[:client_secret]
19
17
  config.languages = app.config.prismic_rails[:languages]
20
18
  config.caching = app.config.prismic_rails[:caching]
21
19
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
1
3
  # :nodoc:
2
4
  module PrismicRails
3
5
  # The PrismicRails::LanguageService tries to match your i18n locale to the
@@ -7,10 +7,7 @@ module PrismicRails
7
7
  # :nodoc:
8
8
  class << self
9
9
 
10
- # Constant that holds the query string to query the document type
11
- DOCUMENT_TYPE = 'document.type'.freeze
12
-
13
- # Query the Prismic API with a type
10
+ # Query the Prismic API by type
14
11
  # ==== Examples
15
12
  # PrismicRails::QueryService.type('blog-post')
16
13
  #
@@ -21,25 +18,30 @@ module PrismicRails
21
18
  #
22
19
  # This gets all the (published) documents of
23
20
  # the type 'blog-post' in english as a Prismic::Response and wraps it around with a PrismicRails::Result
24
- #
25
- def type(type, options = {})
26
- match_language options if options[:lang]
27
- response = query(predicates(type), options)
28
- PrismicRails::Result.new(response)
21
+ def by_type(type, options = {})
22
+ type_query = {'document.type': type}
23
+ if options[:q]
24
+ options[:q].merge(type_query)
25
+ else
26
+ options[:q] = type_query
27
+ end
28
+ query(options)
29
29
  end
30
30
 
31
- private
32
-
33
- # Do the actual query against the Prismic API
34
- def query(predicates, options)
35
- api.query(predicates,
36
- options)
31
+ # Query the Prismic API
32
+ def query(options = {})
33
+ match_language(options) if options[:lang]
34
+ predicates = []
35
+ if q = options.delete(:q)
36
+ q.each do |key, value|
37
+ predicates << Prismic::Predicates.at(key, value)
38
+ end
39
+ end
40
+ response = api.query(predicates, options)
41
+ PrismicRails::Result.new(response)
37
42
  end
38
43
 
39
- # Creates the Prismic::Predicates for a given type
40
- def predicates type
41
- Prismic::Predicates.at(DOCUMENT_TYPE, type)
42
- end
44
+ private
43
45
 
44
46
  # Returns the Prismic::Api object
45
47
  def api
@@ -1,5 +1,5 @@
1
1
  # :nodoc:
2
2
  module PrismicRails
3
3
  # :nodoc:
4
- VERSION = "0.3.0"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/prismic_rails.rb CHANGED
@@ -13,11 +13,12 @@ require "prismic_rails/content/nil_document.rb"
13
13
  # :nodoc:
14
14
  module PrismicRails
15
15
 
16
+ class NoPrismicAPIConnection < StandardError; end
17
+
16
18
  # A simple Config class that holds the config objects
17
19
  class Config
18
20
  attr_accessor :url, # Prismic API URL
19
- :token, # Prismic API Token
20
- :client_id, # Client ID
21
+ :token, # Prismic API Token :client_id, # Client ID
21
22
  :client_secret, # Client Secret
22
23
  :languages, # Language maching hash
23
24
  :caching # Changing enabled?
@@ -42,7 +43,7 @@ module PrismicRails
42
43
  Prismic::API::PrismicWSAuthError,
43
44
  SocketError,
44
45
  Net::OpenTimeout
45
- @api = nil
46
+ raise NoPrismicAPIConnection
46
47
  end
47
48
  end
48
49
 
@@ -77,12 +78,4 @@ module PrismicRails
77
78
  api.master_ref.ref
78
79
  end
79
80
 
80
- def self.find(document_type, fragment_id, options = {})
81
- if options[:lang]
82
- PrismicRails::QueryService.type(document_type, lang: options[:lang]).find_fragment(fragment_id)
83
- else
84
- PrismicRails::QueryService.type(document_type).find_fragment(fragment_id)
85
- end
86
- end
87
-
88
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prismic_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Langenegger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-20 00:00:00.000000000 Z
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prismic.io