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
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "delivery"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "delivery"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,17 +1,17 @@
1
- require File.dirname(__FILE__) + '/delivery/client/delivery_client'
2
- require File.dirname(__FILE__) + '/delivery/client/delivery_query'
3
- require File.dirname(__FILE__) + '/delivery/models/content_item'
4
- require File.dirname(__FILE__) + '/delivery/models/content_type'
5
- require File.dirname(__FILE__) + '/delivery/models/taxonomy_group'
6
- require File.dirname(__FILE__) + '/delivery/query_parameters/filters'
7
- require File.dirname(__FILE__) + '/delivery/responses/delivery_item_listing_response'
8
- require File.dirname(__FILE__) + '/delivery/responses/delivery_item_response'
9
- require File.dirname(__FILE__) + '/delivery/responses/delivery_type_listing_response'
10
- require File.dirname(__FILE__) + '/delivery/responses/delivery_type_response'
11
- require File.dirname(__FILE__) + '/delivery/responses/delivery_taxonomy_listing_response'
12
- require File.dirname(__FILE__) + '/delivery/responses/delivery_taxonomy_response'
13
- require File.dirname(__FILE__) + '/delivery/responses/delivery_element_response'
14
- require File.dirname(__FILE__) + '/delivery/resolvers/content_link_resolver'
15
- require File.dirname(__FILE__) + '/delivery/resolvers/inline_content_item_resolver'
16
- require File.dirname(__FILE__) + '/delivery/resolvers/linked_item_resolver'
17
- require File.dirname(__FILE__) + '/delivery/builders/image_transformation_builder'
1
+ require File.dirname(__FILE__) + '/delivery/client/delivery_client'
2
+ require File.dirname(__FILE__) + '/delivery/client/delivery_query'
3
+ require File.dirname(__FILE__) + '/delivery/models/content_item'
4
+ require File.dirname(__FILE__) + '/delivery/models/content_type'
5
+ require File.dirname(__FILE__) + '/delivery/models/taxonomy_group'
6
+ require File.dirname(__FILE__) + '/delivery/query_parameters/filters'
7
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_item_listing_response'
8
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_item_response'
9
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_type_listing_response'
10
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_type_response'
11
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_taxonomy_listing_response'
12
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_taxonomy_response'
13
+ require File.dirname(__FILE__) + '/delivery/responses/delivery_element_response'
14
+ require File.dirname(__FILE__) + '/delivery/resolvers/content_link_resolver'
15
+ require File.dirname(__FILE__) + '/delivery/resolvers/inline_content_item_resolver'
16
+ require File.dirname(__FILE__) + '/delivery/resolvers/linked_item_resolver'
17
+ require File.dirname(__FILE__) + '/delivery/builders/image_transformation_builder'
@@ -1,141 +1,141 @@
1
- require 'delivery/query_parameters/query_string'
2
-
3
- module Delivery
4
- module Builders
5
- class ImageTransformationBuilder
6
- FIT_MODE_CLIP = 'clip'.freeze
7
- FIT_MODE_SCALE = 'scale'.freeze
8
- FIT_MODE_CROP = 'crop'.freeze
9
- FORMAT_GIF = 'gif'.freeze
10
- FORMAT_PNG = 'png'.freeze
11
- FORMAT_PNG8 = 'png8'.freeze
12
- FORMAT_JPG = 'jpg'.freeze
13
- FORMAT_PJPG = 'pjpg'.freeze
14
- FORMAT_WEBP = 'webp'.freeze
15
-
16
- class << self
17
- def transform(url)
18
- AssetURL.new url
19
- end
20
- end
21
- end
22
-
23
- class AssetURL
24
- INVALID_PARAMS = 'One or more of the parameters is invalid. '\
25
- 'See https://developer.kenticocloud.com/v1/reference#focal-point-crop'\
26
- 'for more information.'.freeze
27
- ONE_TO_100 = 'Quality parameter must be between 1 and 100.'.freeze
28
- BOOLEAN_PARAM = 'The lossless parameter must be "true," "false," '\
29
- '1, or 0.'.freeze
30
-
31
- def initialize(url)
32
- @url = url
33
- @query_string = Delivery::QueryParameters::QueryString.new
34
- end
35
-
36
- def url
37
- @url + @query_string.to_s
38
- end
39
-
40
- def with_width(width)
41
- @query_string.set_param 'w', width
42
- self
43
- end
44
-
45
- def with_height(height)
46
- @query_string.set_param 'h', height
47
- self
48
- end
49
-
50
- def with_pixel_ratio(dpr)
51
- @query_string.set_param 'dpr', dpr
52
- self
53
- end
54
-
55
- def with_fit_mode(fit)
56
- @query_string.set_param 'fit', fit
57
- self
58
- end
59
-
60
- # Setting this will remove focal point cropping from the image,
61
- # as the two options are incompatible.
62
- # @param x
63
- # @param y
64
- # @param width
65
- # @param height
66
- def with_rect(x, y, width, height)
67
- @query_string.remove_param 'fp-x'
68
- @query_string.remove_param 'fp-y'
69
- @query_string.remove_param 'fp-z'
70
- @query_string.remove_param 'crop', 'focalpoint'
71
- @query_string.set_param 'rect', "#{x},#{y},#{width},#{height}"
72
- self
73
- end
74
-
75
- # Setting this will remove the source rectangle region,
76
- # as the two options are incompatible.
77
- def with_focal_point(x, y, z)
78
- raise ArgumentError, INVALID_PARAMS unless valid_dims?(x, y, z)
79
-
80
- @query_string.remove_param 'rect'
81
- @query_string.set_param 'fp-x', x
82
- @query_string.set_param 'fp-y', y
83
- @query_string.set_param 'fp-z', z
84
- @query_string.set_param 'crop', 'focalpoint'
85
- self
86
- end
87
-
88
- def valid_dims?(x, y, z)
89
- (x.to_f >= 0.0 && x.to_f <= 1.0) &&
90
- (y.to_f >= 0.0 && y.to_f <= 1.0) &&
91
- (z.to_i >= 1)
92
- end
93
-
94
- # Sets the background color.
95
- # @param [String] color a valid 3, 4, 6, or 8 digit hexadecimal color, without the # symbol
96
- def with_background_color(color)
97
- @query_string.set_param 'bg', color
98
- self
99
- end
100
-
101
- def with_output_format(format)
102
- @query_string.set_param 'fm', format
103
- self
104
- end
105
-
106
- def with_quality(quality)
107
- raise ArgumentError, ONE_TO_100 unless quality.to_i >= 1 && quality.to_i <= 100
108
-
109
- @query_string.set_param 'q', quality
110
- self
111
- end
112
-
113
- # Sets lossless to true or false. If true, automatically sets the format to WebP
114
- def with_lossless(lossless)
115
- lossless = lossless.to_s.downcase
116
- raise ArgumentError, BOOLEAN_PARAM unless bool? lossless
117
-
118
- @query_string.set_param 'lossless', lossless
119
- @query_string.set_param 'fm', Delivery::Builders::ImageTransformationBuilder::FORMAT_WEBP if %w[true 1].include? lossless
120
- self
121
- end
122
-
123
- def bool?(value)
124
- (value == 'true') ||
125
- (value == 'false') ||
126
- (value == '0') ||
127
- (value == '1')
128
- end
129
-
130
- def with_auto_format_selection(auto)
131
- auto = auto.to_s.downcase
132
- if %w[true 1].include? auto
133
- @query_string.set_param 'auto', 'format'
134
- else
135
- @query_string.remove_param 'auto'
136
- end
137
- self
138
- end
139
- end
140
- end
141
- end
1
+ require 'delivery/query_parameters/query_string'
2
+
3
+ module Delivery
4
+ module Builders
5
+ class ImageTransformationBuilder
6
+ FIT_MODE_CLIP = 'clip'.freeze
7
+ FIT_MODE_SCALE = 'scale'.freeze
8
+ FIT_MODE_CROP = 'crop'.freeze
9
+ FORMAT_GIF = 'gif'.freeze
10
+ FORMAT_PNG = 'png'.freeze
11
+ FORMAT_PNG8 = 'png8'.freeze
12
+ FORMAT_JPG = 'jpg'.freeze
13
+ FORMAT_PJPG = 'pjpg'.freeze
14
+ FORMAT_WEBP = 'webp'.freeze
15
+
16
+ class << self
17
+ def transform(url)
18
+ AssetURL.new url
19
+ end
20
+ end
21
+ end
22
+
23
+ class AssetURL
24
+ INVALID_PARAMS = 'One or more of the parameters is invalid. '\
25
+ 'See https://developer.kenticocloud.com/v1/reference#focal-point-crop'\
26
+ 'for more information.'.freeze
27
+ ONE_TO_100 = 'Quality parameter must be between 1 and 100.'.freeze
28
+ BOOLEAN_PARAM = 'The lossless parameter must be "true," "false," '\
29
+ '1, or 0.'.freeze
30
+
31
+ def initialize(url)
32
+ @url = url
33
+ @query_string = Delivery::QueryParameters::QueryString.new
34
+ end
35
+
36
+ def url
37
+ @url + @query_string.to_s
38
+ end
39
+
40
+ def with_width(width)
41
+ @query_string.set_param 'w', width
42
+ self
43
+ end
44
+
45
+ def with_height(height)
46
+ @query_string.set_param 'h', height
47
+ self
48
+ end
49
+
50
+ def with_pixel_ratio(dpr)
51
+ @query_string.set_param 'dpr', dpr
52
+ self
53
+ end
54
+
55
+ def with_fit_mode(fit)
56
+ @query_string.set_param 'fit', fit
57
+ self
58
+ end
59
+
60
+ # Setting this will remove focal point cropping from the image,
61
+ # as the two options are incompatible.
62
+ # @param x
63
+ # @param y
64
+ # @param width
65
+ # @param height
66
+ def with_rect(x, y, width, height)
67
+ @query_string.remove_param 'fp-x'
68
+ @query_string.remove_param 'fp-y'
69
+ @query_string.remove_param 'fp-z'
70
+ @query_string.remove_param 'crop', 'focalpoint'
71
+ @query_string.set_param 'rect', "#{x},#{y},#{width},#{height}"
72
+ self
73
+ end
74
+
75
+ # Setting this will remove the source rectangle region,
76
+ # as the two options are incompatible.
77
+ def with_focal_point(x, y, z)
78
+ raise ArgumentError, INVALID_PARAMS unless valid_dims?(x, y, z)
79
+
80
+ @query_string.remove_param 'rect'
81
+ @query_string.set_param 'fp-x', x
82
+ @query_string.set_param 'fp-y', y
83
+ @query_string.set_param 'fp-z', z
84
+ @query_string.set_param 'crop', 'focalpoint'
85
+ self
86
+ end
87
+
88
+ def valid_dims?(x, y, z)
89
+ (x.to_f >= 0.0 && x.to_f <= 1.0) &&
90
+ (y.to_f >= 0.0 && y.to_f <= 1.0) &&
91
+ (z.to_i >= 1)
92
+ end
93
+
94
+ # Sets the background color.
95
+ # @param [String] color a valid 3, 4, 6, or 8 digit hexadecimal color, without the # symbol
96
+ def with_background_color(color)
97
+ @query_string.set_param 'bg', color
98
+ self
99
+ end
100
+
101
+ def with_output_format(format)
102
+ @query_string.set_param 'fm', format
103
+ self
104
+ end
105
+
106
+ def with_quality(quality)
107
+ raise ArgumentError, ONE_TO_100 unless quality.to_i >= 1 && quality.to_i <= 100
108
+
109
+ @query_string.set_param 'q', quality
110
+ self
111
+ end
112
+
113
+ # Sets lossless to true or false. If true, automatically sets the format to WebP
114
+ def with_lossless(lossless)
115
+ lossless = lossless.to_s.downcase
116
+ raise ArgumentError, BOOLEAN_PARAM unless bool? lossless
117
+
118
+ @query_string.set_param 'lossless', lossless
119
+ @query_string.set_param 'fm', Delivery::Builders::ImageTransformationBuilder::FORMAT_WEBP if %w[true 1].include? lossless
120
+ self
121
+ end
122
+
123
+ def bool?(value)
124
+ (value == 'true') ||
125
+ (value == 'false') ||
126
+ (value == '0') ||
127
+ (value == '1')
128
+ end
129
+
130
+ def with_auto_format_selection(auto)
131
+ auto = auto.to_s.downcase
132
+ if %w[true 1].include? auto
133
+ @query_string.set_param 'auto', 'format'
134
+ else
135
+ @query_string.remove_param 'auto'
136
+ end
137
+ self
138
+ end
139
+ end
140
+ end
141
+ end
@@ -1,73 +1,73 @@
1
- module Delivery
2
- module Builders
3
- # Generates the URL required for Delivery REST API
4
- class UrlBuilder
5
- URL_TEMPLATE_BASE = 'https://deliver.kenticocloud.com/%s'.freeze
6
- URL_TEMPLATE_PREVIEW = 'https://preview-deliver.kenticocloud.com/%s'.freeze
7
- URL_TEMPLATE_ITEM = '/items/%s'.freeze
8
- URL_TEMPLATE_ITEMS = '/items'.freeze
9
- URL_TEMPLATE_TYPE = '/types/%s'.freeze
10
- URL_TEMPLATE_TYPES = '/types'.freeze
11
- URL_TEMPLATE_ELEMENTS = '/types/%s/elements/%s'.freeze
12
- URL_TEMPLATE_TAXONOMY = '/taxonomies/%s'.freeze
13
- URL_TEMPLATE_TAXONOMIES = '/taxonomies'.freeze
14
-
15
- URL_MAX_LENGTH = 65_519
16
- MSG_LONG_QUERY = 'The request url is too long. Split your query into multiple calls.'.freeze
17
-
18
- class << self
19
- def provide_url(query)
20
- url = provide_base_url(query)
21
- url += provide_path_part(query)
22
-
23
- if query.query_string.empty?
24
- url
25
- else
26
- url + query.query_string.to_s
27
- end
28
- end
29
-
30
- def validate_url(url)
31
- raise UriFormatException, MSG_LONG_QUERY if url.length > URL_MAX_LENGTH
32
- end
33
-
34
- private
35
-
36
- # Returns relative path part of URL depending on query type
37
- def provide_path_part(query)
38
- case query.query_type
39
- when Delivery::QUERY_TYPE_ITEMS
40
- if query.code_name.nil?
41
- URL_TEMPLATE_ITEMS
42
- else
43
- format(URL_TEMPLATE_ITEM, query.code_name)
44
- end
45
- when Delivery::QUERY_TYPE_TYPES
46
- if query.code_name.nil?
47
- URL_TEMPLATE_TYPES
48
- else
49
- format(URL_TEMPLATE_TYPE, query.code_name)
50
- end
51
- when Delivery::QUERY_TYPE_TAXONOMIES
52
- if query.code_name.nil?
53
- URL_TEMPLATE_TAXONOMIES
54
- else
55
- format(URL_TEMPLATE_TAXONOMY, query.code_name)
56
- end
57
- when Delivery::QUERY_TYPE_ELEMENT
58
- format(URL_TEMPLATE_ELEMENTS, query.content_type, query.code_name)
59
- end
60
- end
61
-
62
- # Returns the protocol and domain with project ID
63
- def provide_base_url(query)
64
- if query.use_preview
65
- format(URL_TEMPLATE_PREVIEW, query.project_id)
66
- else
67
- format(URL_TEMPLATE_BASE, query.project_id)
68
- end
69
- end
70
- end
71
- end
72
- end
73
- end
1
+ module Delivery
2
+ module Builders
3
+ # Generates the URL required for Delivery REST API
4
+ class UrlBuilder
5
+ URL_TEMPLATE_BASE = 'https://deliver.kenticocloud.com/%s'.freeze
6
+ URL_TEMPLATE_PREVIEW = 'https://preview-deliver.kenticocloud.com/%s'.freeze
7
+ URL_TEMPLATE_ITEM = '/items/%s'.freeze
8
+ URL_TEMPLATE_ITEMS = '/items'.freeze
9
+ URL_TEMPLATE_TYPE = '/types/%s'.freeze
10
+ URL_TEMPLATE_TYPES = '/types'.freeze
11
+ URL_TEMPLATE_ELEMENTS = '/types/%s/elements/%s'.freeze
12
+ URL_TEMPLATE_TAXONOMY = '/taxonomies/%s'.freeze
13
+ URL_TEMPLATE_TAXONOMIES = '/taxonomies'.freeze
14
+
15
+ URL_MAX_LENGTH = 65_519
16
+ MSG_LONG_QUERY = 'The request url is too long. Split your query into multiple calls.'.freeze
17
+
18
+ class << self
19
+ def provide_url(query)
20
+ url = provide_base_url(query)
21
+ url += provide_path_part(query)
22
+
23
+ if query.query_string.empty?
24
+ url
25
+ else
26
+ url + query.query_string.to_s
27
+ end
28
+ end
29
+
30
+ def validate_url(url)
31
+ raise UriFormatException, MSG_LONG_QUERY if url.length > URL_MAX_LENGTH
32
+ end
33
+
34
+ private
35
+
36
+ # Returns relative path part of URL depending on query type
37
+ def provide_path_part(query)
38
+ case query.query_type
39
+ when Delivery::QUERY_TYPE_ITEMS
40
+ if query.code_name.nil?
41
+ URL_TEMPLATE_ITEMS
42
+ else
43
+ format(URL_TEMPLATE_ITEM, query.code_name)
44
+ end
45
+ when Delivery::QUERY_TYPE_TYPES
46
+ if query.code_name.nil?
47
+ URL_TEMPLATE_TYPES
48
+ else
49
+ format(URL_TEMPLATE_TYPE, query.code_name)
50
+ end
51
+ when Delivery::QUERY_TYPE_TAXONOMIES
52
+ if query.code_name.nil?
53
+ URL_TEMPLATE_TAXONOMIES
54
+ else
55
+ format(URL_TEMPLATE_TAXONOMY, query.code_name)
56
+ end
57
+ when Delivery::QUERY_TYPE_ELEMENT
58
+ format(URL_TEMPLATE_ELEMENTS, query.content_type, query.code_name)
59
+ end
60
+ end
61
+
62
+ # Returns the protocol and domain with project ID
63
+ def provide_base_url(query)
64
+ if query.use_preview
65
+ format(URL_TEMPLATE_PREVIEW, query.project_id)
66
+ else
67
+ format(URL_TEMPLATE_BASE, query.project_id)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end