delivery-sdk-ruby 0.14.13 → 0.15.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 +461 -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 +141 -141
  8. data/lib/delivery/builders/url_builder.rb +73 -73
  9. data/lib/delivery/client/delivery_client.rb +85 -85
  10. data/lib/delivery/client/delivery_query.rb +188 -183
  11. data/lib/delivery/models/content_item.rb +96 -96
  12. data/lib/delivery/models/content_type.rb +26 -26
  13. data/lib/delivery/models/pagination.rb +13 -13
  14. data/lib/delivery/models/taxonomy_group.rb +24 -24
  15. data/lib/delivery/query_parameters/filters.rb +87 -87
  16. data/lib/delivery/query_parameters/parameter_base.rb +30 -30
  17. data/lib/delivery/query_parameters/query_string.rb +49 -49
  18. data/lib/delivery/resolvers/content_link_resolver.rb +62 -62
  19. data/lib/delivery/resolvers/inline_content_item_resolver.rb +50 -50
  20. data/lib/delivery/resolvers/linked_item_resolver.rb +27 -27
  21. data/lib/delivery/responses/delivery_element_response.rb +23 -23
  22. data/lib/delivery/responses/delivery_item_listing_response.rb +39 -39
  23. data/lib/delivery/responses/delivery_item_response.rb +29 -29
  24. data/lib/delivery/responses/delivery_taxonomy_listing_response.rb +32 -32
  25. data/lib/delivery/responses/delivery_taxonomy_response.rb +22 -22
  26. data/lib/delivery/responses/delivery_type_listing_response.rb +31 -31
  27. data/lib/delivery/responses/delivery_type_response.rb +21 -21
  28. data/lib/delivery/responses/response_base.rb +20 -20
  29. data/lib/delivery/version.rb +3 -3
  30. metadata +13 -12
@@ -1,30 +1,30 @@
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 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,49 +1,49 @@
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 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,62 +1,62 @@
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 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,50 +1,50 @@
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 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,28 +1,28 @@
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
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
28
  end
@@ -1,23 +1,23 @@
1
- require 'delivery/responses/response_base'
2
-
3
- module Delivery
4
- module Responses
5
- # Returned by DeliveryClient.element containing a single element
6
- class DeliveryElementResponse < ResponseBase
7
- def element
8
- @element unless @element.nil?
9
- @element = JSON.parse(
10
- JSON.generate(@response),
11
- object_class: OpenStruct
12
- )
13
- end
14
-
15
- def initialize(response)
16
- @response = response
17
- super 200,
18
- "Success, '#{element.codename}' returned",
19
- JSON.generate(@response)
20
- end
21
- end
22
- end
23
- end
1
+ require 'delivery/responses/response_base'
2
+
3
+ module Delivery
4
+ module Responses
5
+ # Returned by DeliveryClient.element containing a single element
6
+ class DeliveryElementResponse < ResponseBase
7
+ def element
8
+ @element unless @element.nil?
9
+ @element = JSON.parse(
10
+ JSON.generate(@response),
11
+ object_class: OpenStruct
12
+ )
13
+ end
14
+
15
+ def initialize(response)
16
+ @response = response
17
+ super 200,
18
+ "Success, '#{element.codename}' returned",
19
+ JSON.generate(@response)
20
+ end
21
+ end
22
+ end
23
+ end