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