delivery-sdk-ruby 0.11.4 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93bedb0d59952488e4abf5736db0442a9fae71a990b454e843ce3781f23e357d
4
- data.tar.gz: d6057dfc9b64c77131675037a7dcf9d29cd9c123908c2e318c65446b970d9e47
3
+ metadata.gz: 3af4e9e0b215b8ddfad6bef517543de085c0cfc86bd3da5e8139f63fa3c71d4c
4
+ data.tar.gz: 52f4765ebccf5b02a7c9c1410500a5e03022b789957c31c5d195150e83134ab3
5
5
  SHA512:
6
- metadata.gz: 8cef13a61db0fe6fbbed38a9a2b31a9fcf318e82038a9a2111aee12aaacdee085f733cbf855725f543c06726528a3bbdde99ff2914a114cbbae76dda5f0c533c
7
- data.tar.gz: 406c66405ea177a8b79dafa22f2d7b2fe8c74c31515884c88597146498251a789ad6d8f3f0f1fea9171d435f4b24867a6ac0a3cca9d1babfbd6a761f2f8d743c
6
+ metadata.gz: 3d09788c7f7cae943c864232babe39eee5d47c3c8f9857e3b4c2b6ce7fa19dca55074b0d170fb105574ea20a5a375bbc31bc35503c1dbcd76906e2ba78fc976b
7
+ data.tar.gz: 36a186c3972f83f5836ca29dedd28c6deeed6e7ee4b0fe97bb0478c9dcec5dba55d51906708dacf5014cbd187bed2989ed6bd5a26a6f3ca5788cc1b4597bc187
data/README.md CHANGED
@@ -5,9 +5,27 @@
5
5
 
6
6
  The Delivery Ruby SDK can be used in Ruby/Rails projects to retrieve content from Kentico Cloud. This is a community project and not an official Kentico SDK. If you find a bug in the SDK or have a feature request, please submit a GitHub issue.
7
7
 
8
+ ## Demo Rails application
9
+
10
+ This repository contains a very basic Rails application that you can run locally to see how the SDK can be used. To run the Dancing Goat demo application, clone this repository and open `/dancing_goat/app/controllers/application_controller.rb`. Add your project ID to the file here:
11
+
12
+ ```ruby
13
+ class ApplicationController < ActionController::Base
14
+ PROJECT_ID = '<your-project-id>'.freeze
15
+ ```
16
+
17
+ If you don't have the sample project installed in Kentico Cloud, you can generate a new project [here](https://app.kenticocloud.com/sample-project-generator). Save the file, then open a terminal in the `/dancing_goat` directory and run the following commands:
18
+
19
+ ```
20
+ bundle install
21
+ rails server
22
+ ```
23
+
24
+ The site should be accessible at localhost:3000.
25
+
8
26
  ## Installation
9
27
 
10
- Add the gem to your Gemfile:
28
+ To use the SDK in your own project, add the gem to your Gemfile:
11
29
 
12
30
  ```ruby
13
31
  gem 'delivery-sdk-ruby'
@@ -339,6 +357,33 @@ delivery_client.items
339
357
  .with_inline_content_item_resolver MyItemResolver.new
340
358
  ```
341
359
 
360
+ ## Image transformation
361
+
362
+ When you've obtained the URL for an asset, you can use our [Image Transformation API](https://developer.kenticocloud.com/v1/reference#image-transformation) to make on-the-fly modifications to the image. To do this, use the static `.transform` method of `Delivery::Builders::ImageTransformationBuilder`, then call the transformation methods. When you're done, call the `.url` method to get the new URL:
363
+
364
+ ```ruby
365
+ url = response.item.get_assets('teaser_image').first.url
366
+ url = Delivery::Builders::ImageTransformationBuilder.transform(url)
367
+ # methods...
368
+ .url
369
+ ```
370
+
371
+ The available methods are:
372
+
373
+ |Method|Possible values|REST example
374
+ |--|--|--|
375
+ |`.with_width`| positive integer, or float between 0 and 1| ?w=200
376
+ |`.with_height`| positive integer, or float between 0 and 1| ?h=200
377
+ |`.with_pixel_ratio`| float greater than 0 but less than 5| ?dpr=1.5
378
+ |`.with_fit_mode`| contstants available at Delivery::Builders::ImageTransformationBuilder <ul><li>FITMODE_CROP</li><li>FITMODE_CLIP</li><li>FITMODE_SCALE</li></ul>| ?fit=crop
379
+ |`.with_rect`| 4 integer values representing pixels or floats representing percentages|rect=100,100,0.7,0.7
380
+ |`.with_focal_point`| 2 floats between 0 and 1 and one integer between 1 and 100| ?fp-x=0.2&fp-y=0.7&fp-z=5
381
+ |`.with_background_color`| string containing 3, 4, 6, or 8 characters | ?bg=7A0099EE
382
+ |`.with_output_format`| contstants available at Delivery::Builders::ImageTransformationBuilder <ul><li>FORMAT_GIF</li><li>FORMAT_PNG</li><li>FORMAT_PNG8</li><li>FORMAT_JPG</li><li>FORMAT_PJPG</li><li>FORMAT_WEBP</li></ul> | ?fm=webp
383
+ |`.with_quality`| integer between 1 to 100 | ?quality=50
384
+ |`.with_lossless`| 'true', 'false', 0, or 1| ?lossless=1
385
+ |`.with_auto_format_selection`| 'true', 'false', 0, or 1 | ?auto=format
386
+
342
387
  ## Feedback & Contributing
343
388
 
344
389
  Check out the [contributing](https://github.com/Kentico/delivery-sdk-ruby/blob/master/CONTRIBUTING.md) page to see the best places to file issues, start discussions, and begin contributing.
@@ -9,3 +9,4 @@ require File.dirname(__FILE__) + '/delivery/responses/delivery_type_listing_resp
9
9
  require File.dirname(__FILE__) + '/delivery/responses/delivery_type_response'
10
10
  require File.dirname(__FILE__) + '/delivery/resolvers/content_link_resolver'
11
11
  require File.dirname(__FILE__) + '/delivery/resolvers/inline_content_item_resolver'
12
+ require File.dirname(__FILE__) + '/delivery/builders/image_transformation_builder'
@@ -0,0 +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
@@ -0,0 +1,65 @@
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
+ end
52
+ end
53
+
54
+ # Returns the protocol and domain with project ID
55
+ def provide_base_url(query)
56
+ if query.use_preview
57
+ format(URL_TEMPLATE_PREVIEW, query.project_id)
58
+ else
59
+ format(URL_TEMPLATE_BASE, query.project_id)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,5 +1,6 @@
1
1
  require 'rest-client'
2
- require 'delivery/client/url_provider'
2
+ require 'delivery/builders/url_builder'
3
+ require 'delivery/query_parameters/query_string'
3
4
 
4
5
  module Delivery
5
6
  # Responsible for translating query parameters into the
@@ -15,11 +16,11 @@ module Delivery
15
16
  :preview_key,
16
17
  :project_id,
17
18
  :code_name,
18
- :params,
19
19
  :secure_key,
20
20
  :content_link_url_resolver,
21
21
  :inline_content_item_resolver,
22
- :query_type
22
+ :query_type,
23
+ :query_string
23
24
 
24
25
  # Setter for url, returns self for chaining
25
26
  # .url represents *manually* configured urls, otherwise final url is
@@ -35,6 +36,7 @@ module Delivery
35
36
  config.each do |k, v|
36
37
  instance_variable_set("@#{k}", v) unless v.nil?
37
38
  end
39
+ self.query_string = Delivery::QueryParameters::QueryString.new
38
40
  return if config.fetch(:qp, nil).nil?
39
41
 
40
42
  # Query parameters were passed, parse and validate
@@ -70,60 +72,55 @@ module Delivery
70
72
  end
71
73
 
72
74
  def order_by(value, sort = '[asc]')
73
- set_param('order', value + sort)
75
+ query_string.set_param('order', value + sort)
74
76
  self
75
77
  end
76
78
 
77
79
  def skip(value)
78
- set_param('skip', value)
80
+ query_string.set_param('skip', value)
79
81
  self
80
82
  end
81
83
 
82
84
  def language(value)
83
- set_param('language', value)
85
+ query_string.set_param('language', value)
84
86
  end
85
87
 
86
88
  def limit(value)
87
- set_param('limit', value)
89
+ query_string.set_param('limit', value)
88
90
  self
89
91
  end
90
92
 
91
93
  def elements(value)
92
- set_param('elements', value)
94
+ query_string.set_param('elements', value)
93
95
  self
94
96
  end
95
97
 
96
98
  def depth(value)
97
- set_param('depth', value)
99
+ query_string.set_param('depth', value)
98
100
  self
99
101
  end
100
102
 
101
103
  private
102
104
 
103
105
  def provide_url
104
- @url = Delivery::UrlProvider.provide_url self if @url.nil?
105
- Delivery::UrlProvider.validate_url @url
106
+ @url = Delivery::Builders::UrlBuilder.provide_url self if @url.nil?
107
+ Delivery::Builders::UrlBuilder.validate_url @url
106
108
  end
107
109
 
108
110
  def validate_params(query_parameters)
109
- self.params = if query_parameters.is_a? Array
110
- query_parameters
111
- else
112
- [query_parameters]
113
- end
111
+ params = if query_parameters.is_a? Array
112
+ query_parameters
113
+ else
114
+ [query_parameters]
115
+ end
114
116
  params.each do |p|
117
+ query_string.set_param p
115
118
  unless p.is_a? Delivery::QueryParameters::Filter
116
119
  raise ArgumentError, ERROR_PARAMS
117
120
  end
118
121
  end
119
122
  end
120
123
 
121
- def set_param(key, value)
122
- self.params = [] if params.nil?
123
- remove_existing_param key
124
- params << Delivery::QueryParameters::ParameterBase.new(key, '', value)
125
- end
126
-
127
124
  # Returns true if this query should use preview mode. Raises an error if
128
125
  # preview is enabled, but the key is nil
129
126
  def should_preview
@@ -168,10 +165,5 @@ module Delivery
168
165
  end
169
166
  end
170
167
  end
171
-
172
- # Remove existing parameter from @params if key exists
173
- def remove_existing_param(key)
174
- params.delete_if { |i| i.key.eql? key } unless params.nil?
175
- end
176
168
  end
177
169
  end
@@ -0,0 +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,3 +1,3 @@
1
1
  module Delivery
2
- VERSION = '0.11.4'.freeze
2
+ VERSION = '0.12.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delivery-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.4
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Dugre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2019-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -104,14 +104,16 @@ files:
104
104
  - bin/console
105
105
  - bin/setup
106
106
  - lib/delivery-sdk-ruby.rb
107
+ - lib/delivery/builders/image_transformation_builder.rb
108
+ - lib/delivery/builders/url_builder.rb
107
109
  - lib/delivery/client/delivery_client.rb
108
110
  - lib/delivery/client/delivery_query.rb
109
- - lib/delivery/client/url_provider.rb
110
111
  - lib/delivery/models/content_item.rb
111
112
  - lib/delivery/models/content_type.rb
112
113
  - lib/delivery/models/pagination.rb
113
114
  - lib/delivery/query_parameters/filters.rb
114
115
  - lib/delivery/query_parameters/parameter_base.rb
116
+ - lib/delivery/query_parameters/query_string.rb
115
117
  - lib/delivery/resolvers/content_link_resolver.rb
116
118
  - lib/delivery/resolvers/inline_content_item_resolver.rb
117
119
  - lib/delivery/responses/delivery_item_listing_response.rb
@@ -1,64 +0,0 @@
1
- module Delivery
2
- # Generates the URL required for Delivery REST API
3
- class UrlProvider
4
- URL_TEMPLATE_BASE = 'https://deliver.kenticocloud.com/%s'.freeze
5
- URL_TEMPLATE_PREVIEW = 'https://preview-deliver.kenticocloud.com/%s'.freeze
6
- URL_TEMPLATE_ITEM = '/items/%s'.freeze
7
- URL_TEMPLATE_ITEMS = '/items'.freeze
8
- URL_TEMPLATE_TYPE = '/types/%s'.freeze
9
- URL_TEMPLATE_TYPES = '/types'.freeze
10
- URL_TEMPLATE_ELEMENTS = '/types/%s/elements/%s'.freeze
11
- URL_TEMPLATE_TAXONOMY = '/taxonomies/%s'.freeze
12
- URL_TEMPLATE_TAXONOMIES = '/taxonomies'.freeze
13
-
14
- URL_MAX_LENGTH = 65_519
15
- MSG_LONG_QUERY = 'The request url is too long. Split your query into multiple calls.'.freeze
16
-
17
- class << self
18
- def provide_url(query)
19
- url = provide_base_url(query)
20
- url += provide_path_part(query)
21
-
22
- if query.params.nil?
23
- url
24
- else
25
- # Map each parameter to the result of a method and separate with &
26
- url + '?' + query.params.map(&:provide_query_string_parameter).join('&')
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
- end
52
- end
53
-
54
- # Returns the protocol and domain with project ID
55
- def provide_base_url(query)
56
- if query.use_preview
57
- format(URL_TEMPLATE_PREVIEW, query.project_id)
58
- else
59
- format(URL_TEMPLATE_BASE, query.project_id)
60
- end
61
- end
62
- end
63
- end
64
- end