delivery-sdk-ruby 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +21 -21
  3. data/README.md +480 -461
  4. data/bin/console +14 -14
  5. data/bin/setup +8 -8
  6. data/lib/delivery-sdk-ruby.rb +17 -17
  7. data/lib/delivery/builders/image_transformation_builder.rb +143 -141
  8. data/lib/delivery/builders/url_builder.rb +75 -73
  9. data/lib/delivery/client/delivery_client.rb +87 -85
  10. data/lib/delivery/client/delivery_query.rb +190 -188
  11. data/lib/delivery/models/content_item.rb +98 -96
  12. data/lib/delivery/models/content_type.rb +28 -26
  13. data/lib/delivery/models/pagination.rb +15 -13
  14. data/lib/delivery/models/taxonomy_group.rb +26 -24
  15. data/lib/delivery/query_parameters/filters.rb +89 -87
  16. data/lib/delivery/query_parameters/parameter_base.rb +32 -30
  17. data/lib/delivery/query_parameters/query_string.rb +51 -49
  18. data/lib/delivery/resolvers/content_link_resolver.rb +64 -62
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +52 -50
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +30 -28
  21. data/lib/delivery/responses/delivery_element_response.rb +25 -23
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +41 -39
  23. data/lib/delivery/responses/delivery_item_response.rb +31 -29
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +34 -32
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +24 -22
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +33 -31
  27. data/lib/delivery/responses/delivery_type_response.rb +23 -21
  28. data/lib/delivery/responses/response_base.rb +22 -20
  29. data/lib/delivery/version.rb +5 -3
  30. metadata +13 -14
@@ -1,30 +1,32 @@
1
- module Delivery
2
- # Contains static methods for adding parameters to a DeliveryQuery
3
- # as well as the Filter class.
4
- module QueryParameters
5
- # Base class for all parameters added to a DeliveryQuery using the
6
- # .parameters method. All parameters appear in the query string.
7
- class ParameterBase
8
- attr_accessor :key
9
- SEPARATOR = CGI.escape(',')
10
-
11
- def initialize(key, operator, values)
12
- self.key = key
13
- values = [values] unless values.respond_to? :each
14
- @values = values
15
- @operator = operator
16
- end
17
-
18
- def provide_query_string_parameter
19
- escaped_values = []
20
- @values.each { |n| escaped_values << CGI.escape(n.to_s) }
21
- format(
22
- '%<k>s%<o>s=%<v>s',
23
- k: CGI.escape(key),
24
- o: CGI.escape(@operator),
25
- v: escaped_values.join(SEPARATOR)
26
- )
27
- end
28
- end
29
- end
30
- end
1
+ module KenticoCloud
2
+ module Delivery
3
+ # Contains static methods for adding parameters to a DeliveryQuery
4
+ # as well as the Filter class.
5
+ module QueryParameters
6
+ # Base class for all parameters added to a DeliveryQuery using the
7
+ # .parameters method. All parameters appear in the query string.
8
+ class ParameterBase
9
+ attr_accessor :key
10
+ SEPARATOR = CGI.escape(',')
11
+
12
+ def initialize(key, operator, values)
13
+ self.key = key
14
+ values = [values] unless values.respond_to? :each
15
+ @values = values
16
+ @operator = operator
17
+ end
18
+
19
+ def provide_query_string_parameter
20
+ escaped_values = []
21
+ @values.each { |n| escaped_values << CGI.escape(n.to_s) }
22
+ format(
23
+ '%<k>s%<o>s=%<v>s',
24
+ k: CGI.escape(key),
25
+ o: CGI.escape(@operator),
26
+ v: escaped_values.join(SEPARATOR)
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,49 +1,51 @@
1
- require 'delivery/query_parameters/parameter_base'
2
-
3
- module Delivery
4
- module QueryParameters
5
- class QueryString
6
- def initialize
7
- @params = []
8
- end
9
-
10
- # Adds a parameter to the query string
11
- # @param [String] param Either a string representing the key for the parameter, or a complete ParameterBase object
12
- # @param [String] values A string or array of strings representing the values for the parameter
13
- # @param [String] operator Kentico Cloud filtering parameter, placed after the key, before the equal sign
14
- def set_param(param, values = '', operator = '')
15
- parameter_base =
16
- if param.is_a? String
17
- Delivery::QueryParameters::ParameterBase.new(
18
- param,
19
- operator,
20
- values
21
- )
22
- else
23
- param
24
- end
25
- # Ensure we have a ParameterBase object
26
- return unless parameter_base.respond_to? 'provide_query_string_parameter'
27
-
28
- remove_param parameter_base.key
29
- @params << parameter_base
30
- end
31
-
32
- def remove_param(key)
33
- @params.delete_if { |i| i.key.eql? key }
34
- end
35
-
36
- def param(key)
37
- @params.select { |p| p.key.eql? key }
38
- end
39
-
40
- def empty?
41
- @params.empty?
42
- end
43
-
44
- def to_s
45
- '?' + @params.map(&:provide_query_string_parameter).join('&')
46
- end
47
- end
48
- end
49
- end
1
+ require 'delivery/query_parameters/parameter_base'
2
+
3
+ module KenticoCloud
4
+ module Delivery
5
+ module QueryParameters
6
+ class QueryString
7
+ def initialize
8
+ @params = []
9
+ end
10
+
11
+ # Adds a parameter to the query string
12
+ # @param [String] param Either a string representing the key for the parameter, or a complete ParameterBase object
13
+ # @param [String] values A string or array of strings representing the values for the parameter
14
+ # @param [String] operator Kentico Cloud filtering parameter, placed after the key, before the equal sign
15
+ def set_param(param, values = '', operator = '')
16
+ parameter_base =
17
+ if param.is_a? String
18
+ KenticoCloud::Delivery::QueryParameters::ParameterBase.new(
19
+ param,
20
+ operator,
21
+ values
22
+ )
23
+ else
24
+ param
25
+ end
26
+ # Ensure we have a ParameterBase object
27
+ return unless parameter_base.respond_to? 'provide_query_string_parameter'
28
+
29
+ remove_param parameter_base.key
30
+ @params << parameter_base
31
+ end
32
+
33
+ def remove_param(key)
34
+ @params.delete_if { |i| i.key.eql? key }
35
+ end
36
+
37
+ def param(key)
38
+ @params.select { |p| p.key.eql? key }
39
+ end
40
+
41
+ def empty?
42
+ @params.empty?
43
+ end
44
+
45
+ def to_s
46
+ '?' + @params.map(&:provide_query_string_parameter).join('&')
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,62 +1,64 @@
1
- require 'nokogiri'
2
-
3
- module Delivery
4
- module Resolvers
5
- # Locates <a data-item-id=""> tags in content and calls a user-defined method
6
- # to supply the href for content item links
7
- class ContentLinkResolver
8
- def initialize(callback = nil)
9
- @callback = callback
10
- end
11
-
12
- # Resolves all links in the content
13
- # @param [String] content The string value stored in the element
14
- # @param [Array] links The collection of source links from the JSON response
15
- def resolve(content, links)
16
- doc = Nokogiri::HTML.parse(content).xpath('//body')
17
- links = links.map { |link| ContentLink.new link }
18
- tags = doc.xpath('//a[@data-item-id]')
19
- # This line performs the link resolving and replaces the tags in doc
20
- tags.map { |tag| resolve_tag tag, links }
21
- doc.inner_html
22
- end
23
-
24
- private
25
-
26
- # Accepts a tag found in the content and tries to locate matching
27
- # source link from JSON response. If found, resolves URL and returns
28
- # the tag with generated HREF
29
- def resolve_tag(tag, links)
30
- matches = links.select { |link| link.id == tag['data-item-id'].to_s }
31
- url = provide_url matches
32
- tag['href'] = url
33
- tag
34
- end
35
-
36
- # Returns a url if a link was found in source links, otherwise returns 404
37
- def provide_url(matches)
38
- if !matches.empty?
39
- if @callback.nil?
40
- resolve_link matches[0]
41
- else
42
- @callback.call matches[0]
43
- end
44
- else
45
- '/404'
46
- end
47
- end
48
- end
49
-
50
- # Model for links from the JSON response
51
- class ContentLink
52
- attr_accessor :code_name, :type, :url_slug, :id
53
-
54
- def initialize(link)
55
- self.id = link[0]
56
- self.code_name = link[1]['codename']
57
- self.type = link[1]['type']
58
- self.url_slug = link[1]['url_slug']
59
- end
60
- end
61
- end
62
- end
1
+ require 'nokogiri'
2
+
3
+ module KenticoCloud
4
+ module Delivery
5
+ module Resolvers
6
+ # Locates <a data-item-id=""> tags in content and calls a user-defined method
7
+ # to supply the href for content item links
8
+ class ContentLinkResolver
9
+ def initialize(callback = nil)
10
+ @callback = callback
11
+ end
12
+
13
+ # Resolves all links in the content
14
+ # @param [String] content The string value stored in the element
15
+ # @param [Array] links The collection of source links from the JSON response
16
+ def resolve(content, links)
17
+ doc = Nokogiri::HTML.parse(content).xpath('//body')
18
+ links = links.map { |link| ContentLink.new link }
19
+ tags = doc.xpath('//a[@data-item-id]')
20
+ # This line performs the link resolving and replaces the tags in doc
21
+ tags.map { |tag| resolve_tag tag, links }
22
+ doc.inner_html
23
+ end
24
+
25
+ private
26
+
27
+ # Accepts a tag found in the content and tries to locate matching
28
+ # source link from JSON response. If found, resolves URL and returns
29
+ # the tag with generated HREF
30
+ def resolve_tag(tag, links)
31
+ matches = links.select { |link| link.id == tag['data-item-id'].to_s }
32
+ url = provide_url matches
33
+ tag['href'] = url
34
+ tag
35
+ end
36
+
37
+ # Returns a url if a link was found in source links, otherwise returns 404
38
+ def provide_url(matches)
39
+ if !matches.empty?
40
+ if @callback.nil?
41
+ resolve_link matches[0]
42
+ else
43
+ @callback.call matches[0]
44
+ end
45
+ else
46
+ '/404'
47
+ end
48
+ end
49
+ end
50
+
51
+ # Model for links from the JSON response
52
+ class ContentLink
53
+ attr_accessor :code_name, :type, :url_slug, :id
54
+
55
+ def initialize(link)
56
+ self.id = link[0]
57
+ self.code_name = link[1]['codename']
58
+ self.type = link[1]['type']
59
+ self.url_slug = link[1]['url_slug']
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,50 +1,52 @@
1
- require 'nokogiri'
2
-
3
- module Delivery
4
- module Resolvers
5
- # Locates <object data-type="item"> tags in content and calls a user-defined method
6
- # to supply the HTML output for the content item
7
- class InlineContentItemResolver
8
- def initialize(callback = nil)
9
- @callback = callback
10
- end
11
-
12
- # Resolves all inline content items in the content
13
- # @param [String] content The string value stored in the element
14
- # @param [Array] inline_items ContentItems referenced by the content
15
- def resolve(content, inline_items)
16
- doc = Nokogiri::HTML.parse(content).xpath('//body')
17
- tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
18
- tags.each do |tag|
19
- output = resolve_tag tag, inline_items
20
- el = doc.at_xpath(
21
- '//object[@type="application/kenticocloud"][@data-type="item"][@data-codename=$value]',
22
- nil,
23
- value: tag['data-codename']
24
- )
25
- el.swap(output) unless output.nil?
26
- end
27
- doc.inner_html
28
- end
29
-
30
- private
31
-
32
- # Accepts a tag found in the content and tries to locate matching
33
- # ContentItem from JSON response.
34
- def resolve_tag(tag, inline_items)
35
- matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
36
- provide_output matches
37
- end
38
-
39
- def provide_output(matches)
40
- if !matches.empty?
41
- if @callback.nil?
42
- resolve_item matches[0]
43
- else
44
- @callback.call matches[0]
45
- end
46
- end
47
- end
48
- end
49
- end
50
- end
1
+ require 'nokogiri'
2
+
3
+ module KenticoCloud
4
+ module Delivery
5
+ module Resolvers
6
+ # Locates <object data-type="item"> tags in content and calls a user-defined method
7
+ # to supply the HTML output for the content item
8
+ class InlineContentItemResolver
9
+ def initialize(callback = nil)
10
+ @callback = callback
11
+ end
12
+
13
+ # Resolves all inline content items in the content
14
+ # @param [String] content The string value stored in the element
15
+ # @param [Array] inline_items ContentItems referenced by the content
16
+ def resolve(content, inline_items)
17
+ doc = Nokogiri::HTML.parse(content).xpath('//body')
18
+ tags = doc.xpath('//object[@type="application/kenticocloud"][@data-type="item"]')
19
+ tags.each do |tag|
20
+ output = resolve_tag tag, inline_items
21
+ el = doc.at_xpath(
22
+ '//object[@type="application/kenticocloud"][@data-type="item"][@data-codename=$value]',
23
+ nil,
24
+ value: tag['data-codename']
25
+ )
26
+ el.swap(output) unless output.nil?
27
+ end
28
+ doc.inner_html
29
+ end
30
+
31
+ private
32
+
33
+ # Accepts a tag found in the content and tries to locate matching
34
+ # ContentItem from JSON response.
35
+ def resolve_tag(tag, inline_items)
36
+ matches = inline_items.select { |item| item.system.codename == tag['data-codename'].to_s }
37
+ provide_output matches
38
+ end
39
+
40
+ def provide_output(matches)
41
+ if !matches.empty?
42
+ if @callback.nil?
43
+ resolve_item matches[0]
44
+ else
45
+ @callback.call matches[0]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,28 +1,30 @@
1
- module Delivery
2
- # Resolves a content item by its codename
3
- # It contains the modular content of item/items response
4
- class LinkedItemResolver
5
- def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
6
- @modular_content = modular_content
7
- @content_link_url_resolver = content_link_url_resolver
8
- @inline_content_item_resolver = inline_content_item_resolver
9
- @resolved_items = {}
10
- end
11
-
12
- # Resolves a content item
13
- # If the link for a codename was resolved before,
14
- # it returns the same instance of Content Item
15
- # @param [String] codename Codename of the content item
16
- # @return [ContentItem]
17
- def resolve(codename)
18
- @resolved_items[codename] ||= resolve_item(codename)
19
- end
20
-
21
- private
22
-
23
- def resolve_item(codename)
24
- item = @modular_content.values.find { |i| i['system']['codename'] == codename }
25
- ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
26
- end
27
- end
28
- end
1
+ module KenticoCloud
2
+ module Delivery
3
+ # Resolves a content item by its codename
4
+ # It contains the modular content of item/items response
5
+ class LinkedItemResolver
6
+ def initialize(modular_content, content_link_url_resolver, inline_content_item_resolver)
7
+ @modular_content = modular_content
8
+ @content_link_url_resolver = content_link_url_resolver
9
+ @inline_content_item_resolver = inline_content_item_resolver
10
+ @resolved_items = {}
11
+ end
12
+
13
+ # Resolves a content item
14
+ # If the link for a codename was resolved before,
15
+ # it returns the same instance of Content Item
16
+ # @param [String] codename Codename of the content item
17
+ # @return [ContentItem]
18
+ def resolve(codename)
19
+ @resolved_items[codename] ||= resolve_item(codename)
20
+ end
21
+
22
+ private
23
+
24
+ def resolve_item(codename)
25
+ item = @modular_content.values.find { |i| i['system']['codename'] == codename }
26
+ ContentItem.new JSON.parse(JSON.generate(item)), @content_link_url_resolver, @inline_content_item_resolver, self
27
+ end
28
+ end
29
+ end
30
+ end