imglab 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d52305397eb47d122dcfb0211c013ba7d7d11958bb45854949321a3972c87c7
4
- data.tar.gz: 7bf0e1b1325ba9ee9e54be5b3c56cd43de1cc386fa902aba7c9a7468a458fc3f
3
+ metadata.gz: 78faee0a4ff6558f7ece0d993cac443de2b480e0c230a5586dc6fdbc8c6867db
4
+ data.tar.gz: e6cbb31a3d1e86d8b54cc7c9985b10cd3555886d941ca66925b6752f8bacf51d
5
5
  SHA512:
6
- metadata.gz: eca396e106d905482ea8ed1a8c081b3bc9d9e5d5380b2af576ae4ab3a36f1318e7585f9a10c90fed9f7fa85be8c75d1b3c971a5bf205e7fd212e05dd5d8dc271
7
- data.tar.gz: b2255cf65a8887bbdd1dbcff7a52c9bf724b8d05ee4b2127931f0f8ed8b424a2c4df4808d41fc63374f959c3e000c08ee54e165d9e4e7c882d1884f8d332868e
6
+ metadata.gz: cb456e7d68b9e66ae072ec27f5232c455cd32f040b3b029dbd0d03362709f22bbd3c61a3b2d908589eff85ff28dde67ceba0c7dd2d12ccb458a121ac035c4974
7
+ data.tar.gz: 4053ed5c372ad62d78bd0af9c8d762b0cd9a6dfa9cba6215328d59bfd73e75951b96c97986ee5657a455954d3fd09cc4d8ce91292edda7061a67b006e8e3c329
data/Gemfile CHANGED
@@ -5,4 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "minitest", "~> 5.11.3"
8
- gem "yard", "~> 0.9.26"
8
+ gem "yard", "~> 0.8.7"
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem "imglab", "~> 0.1.0"
10
+ gem "imglab", "~> 0.2"
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -24,7 +24,7 @@ $ gem install imglab
24
24
 
25
25
  ## Ruby compatibility
26
26
 
27
- `imglab` has been successfully tested on the following Ruby versions: `3.0`, `2.7`, `2.6`, `2.5`, `2.4`, `2.3`, `2.2`, `2.1` and `2.0`.
27
+ `imglab` has been successfully tested on the following Ruby versions: `3.1`, `3.0`, `2.7`, `2.6`, `2.5`, `2.4`, `2.3`, `2.2`, `2.1` and `2.0`.
28
28
 
29
29
  ## Generating URLs
30
30
 
@@ -34,28 +34,28 @@ The easiest way to generate a URL is to specify the `source_name`, `path` and re
34
34
 
35
35
  ```ruby
36
36
  Imglab.url("assets", "image.jpeg", width: 500, height: 600)
37
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600"
37
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600"
38
38
 
39
39
  Imglab.url("avatars", "user-01.jpeg", width: 300, height: 300, mode: :crop, crop: :face, format: :webp)
40
- "https://cdn.imglab.io/avatars/user-01.jpeg?width=300&height=300&mode=crop&crop=face&format=webp"
40
+ "https://avatars.imglab-cdn.net/user-01.jpeg?width=300&height=300&mode=crop&crop=face&format=webp"
41
41
  ```
42
42
 
43
43
  If some specific settings are required for the source you can use an instance of `Imglab::Source` class instead of a `string` source name:
44
44
 
45
45
  ```ruby
46
46
  Imglab.url(Imglab::Source.new("assets"), "image.jpeg", width: 500, height: 600)
47
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600"
47
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600"
48
48
  ```
49
49
 
50
50
  ### Using secure image sources
51
51
 
52
- For sources that require signed URLs you can specify `secure_key` and `secure_salt` attributes for the source:
52
+ For sources that require signed URLs you can specify `secure_key` and `secure_salt` attributes:
53
53
 
54
54
  ```ruby
55
- source = Imglab::Source.new(secure_key: "assets-secure-key", secure_salt: "assets-secure-salt")
55
+ source = Imglab::Source.new("assets", secure_key: "assets-secure-key", secure_salt: "assets-secure-salt")
56
56
 
57
57
  Imglab.url(source, "image.jpeg", width: 500, height: 600)
58
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&signature=generated-signature"
58
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&signature=generated-signature"
59
59
  ```
60
60
 
61
61
  `signature` query parameter will be automatically generated and attached to the returned URL.
@@ -68,7 +68,7 @@ In the case that HTTP schema is required instead of HTTPS you can set `https` at
68
68
 
69
69
  ```ruby
70
70
  Imglab.url(Imglab::Source.new("assets", https: false), "image.jpeg", width: 500, height: 600)
71
- "http://cdn.imglab.io/assets/image.jpeg?width=500&height=600"
71
+ "http://assets.imglab-cdn.net/image.jpeg?width=500&height=600"
72
72
  ```
73
73
 
74
74
  > Note: HTTPS is the default and recommended way to generate URLs with imglab.
@@ -79,21 +79,21 @@ Any parameter from the imglab API can be used to generate URLs with `Imglab.url`
79
79
 
80
80
  ```ruby
81
81
  Imglab.url("assets", "image.jpeg", trim: "color", trim_color: "black")
82
- "https://cdn.imglab.io/assets/image.jpeg?trim=color&trim-color=black"
82
+ "https://assets.imglab-cdn.net/image.jpeg?trim=color&trim-color=black"
83
83
  ```
84
84
 
85
85
  It is possible to use strings too:
86
86
 
87
87
  ```ruby
88
88
  Imglab.url("assets", "image.jpeg", "trim" => "color", "trim-color" => "black")
89
- "https://cdn.imglab.io/assets/image.jpeg?trim=color&trim-color=black"
89
+ "https://assets.imglab-cdn.net/image.jpeg?trim=color&trim-color=black"
90
90
  ```
91
91
 
92
92
  And quoted symbols for Ruby version >= 2.2:
93
93
 
94
94
  ```ruby
95
95
  Imglab.url("assets", "image.jpeg", trim: "color", "trim-color": "black")
96
- "https://cdn.imglab.io/assets/image.jpeg?trim=color&trim-color=black"
96
+ "https://assets.imglab-cdn.net/image.jpeg?trim=color&trim-color=black"
97
97
  ```
98
98
 
99
99
  ### Specifying color parameters
@@ -103,19 +103,19 @@ Some imglab parameters can receive a color as value. It is possible to specify t
103
103
  ```ruby
104
104
  # Specifying a RGB color as string
105
105
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: :contain, background_color: "255,0,0")
106
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0"
106
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0"
107
107
 
108
108
  # Specifying a RGBA color as string
109
109
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: :contain, background_color: "255,0,0,128")
110
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0%2C128"
110
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0%2C128"
111
111
 
112
112
  # Specifying a named color as string
113
113
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: :contain, background_color: "red")
114
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=red"
114
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=red"
115
115
 
116
116
  # Specifying a hexadecimal color as string
117
117
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: :contain, background_color: "F00")
118
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=F00"
118
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=F00"
119
119
  ```
120
120
 
121
121
  You can additionally use `Imglab::Color` helpers to specify these color values:
@@ -126,15 +126,15 @@ include Imglab::Color
126
126
 
127
127
  # Using color helper for a RGB color
128
128
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: "contain", background_color: color(255, 0, 0))
129
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0"
129
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0"
130
130
 
131
131
  # Using color helper for a RGBA color
132
132
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: "contain", background_color: color(255, 0, 0, 128))
133
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0%2C128"
133
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=255%2C0%2C0%2C128"
134
134
 
135
135
  # Using color helper for a named color
136
136
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, mode: "contain", background_color: color("red"))
137
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&mode=contain&background-color=red"
137
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&mode=contain&background-color=red"
138
138
  ```
139
139
 
140
140
  > Note: specify hexadecimal color values using `Imglab::Color` helpers is not allowed. You can use strings instead.
@@ -146,15 +146,15 @@ Some imglab parameters can receive a position as value. It is possible to specif
146
146
  ```ruby
147
147
  # Specifying a horizontal and vertical position as string
148
148
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: "left,top")
149
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=left%2Ctop"
149
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=left%2Ctop"
150
150
 
151
151
  # Specifying a vertical and horizontal position as string
152
152
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: "top,left")
153
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=top%2Cleft"
153
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=top%2Cleft"
154
154
 
155
155
  # Specifying a position as string
156
156
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: "left")
157
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=left"
157
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=left"
158
158
  ```
159
159
 
160
160
  You can additionally use `Imglab::Position` helpers to specify these position values:
@@ -165,15 +165,15 @@ include Imglab::Position
165
165
 
166
166
  # Using position helper for a horizontal and vertical position
167
167
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: position("left", "top"))
168
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=left%2Ctop"
168
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=left%2Ctop"
169
169
 
170
- # Using position macro for a vertical and horizontal position
170
+ # Using position helper for a vertical and horizontal position
171
171
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: position("top", "left"))
172
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=top%2Cleft"
172
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=top%2Cleft"
173
173
 
174
- # Using position macro for a position
174
+ # Using position helper for a position
175
175
  Imglab.url("assets", "image.jpeg", width: 500, height: 500, mode: "crop", crop: position("left"))
176
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=500&mode=crop&crop=left"
176
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=500&mode=crop&crop=left"
177
177
  ```
178
178
 
179
179
  ### Specifying URL parameters
@@ -182,14 +182,14 @@ Some imglab parameters can receive URLs as values. It is possible to specify the
182
182
 
183
183
  ```ruby
184
184
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, watermark: "logo.svg")
185
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&watermark=logo.svg"
185
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&watermark=logo.svg"
186
186
  ```
187
187
 
188
188
  And even use parameters if required:
189
189
 
190
190
  ```ruby
191
191
  Imglab.url("assets", "image.jpeg", width: 500, height: 600, watermark: "logo.svg?width=100&format=png")
192
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&watermark=logo.svg%3Fwidth%3D100%26format%3Dpng"
192
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&watermark=logo.svg%3Fwidth%3D100%26format%3Dpng"
193
193
  ```
194
194
 
195
195
  Additionally you can use nested `Imglab.url` calls to specify these URL values:
@@ -202,7 +202,7 @@ Imglab.url(
202
202
  height: 600,
203
203
  watermark: Imglab.url("assets", "logo.svg", width: 100, format: "png")
204
204
  )
205
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&watermark=https%3A%2F%2Fcdn.imglab.io%2Fassets%2Flogo.svg%3Fwidth%3D100%26format%3Dpng"
205
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&watermark=https%3A%2F%2Fassets.imglab-cdn.net%2Flogo.svg%3Fwidth%3D100%26format%3Dpng"
206
206
  ```
207
207
 
208
208
  If the resource is located in a different source we can specify it using `Imglab.url`:
@@ -215,7 +215,7 @@ Imglab.url(
215
215
  height: 600,
216
216
  watermark: Imglab.url("marketing", "logo.svg", width: 100, format: "png")
217
217
  )
218
- "https://cdn.imglab.io/assets/image.jpeg?width=500&height=600&watermark=https%3A%2F%2Fcdn.imglab.io%2Fmarketing%2Flogo.svg%3Fwidth%3D100%26format%3Dpng"
218
+ "https://assets.imglab-cdn.net/image.jpeg?width=500&height=600&watermark=https%3A%2F%2Fmarketing.imglab-cdn.net%2Flogo.svg%3Fwidth%3D100%26format%3Dpng"
219
219
  ```
220
220
 
221
221
  Using secure sources for URLs parameter values is possible too:
@@ -234,55 +234,73 @@ Imglab.url(
234
234
 
235
235
  `signature` query parameter will be automatically generated and attached to the nested URL value.
236
236
 
237
+ ### Specifying URLs with expiration timestamp
238
+
239
+ The `expires` parameter allows you to specify a UNIX timestamp in seconds after which the request is expired.
240
+
241
+ If a Ruby `Time` instance is used as value to `expires` parameter it will be automatically converted to UNIX timestamp. In the following example, we specify an expiration time of one hour, adding 3600 seconds to the current time:
242
+
243
+ ```ruby
244
+ Imglab.url("assets", "image.jpeg", width: 500, expires: Time.now.utc + 3600)
245
+ ```
246
+
247
+ If you are using Rails or Active Support you can use it's time helpers:
248
+
249
+ ```ruby
250
+ Imglab.url("assets", "image.jpeg", width: 500, expires: 1.hour.from_now)
251
+ ```
252
+
253
+ > Note: The `expires` parameter should be used in conjunction with secure sources. Otherwise, `expires` value could be tampered with.
254
+
237
255
  ## Generating URLs for on-premises imglab server
238
256
 
239
257
  For on-premises imglab server is possible to define custom sources pointing to your server location.
240
258
 
241
259
  * `:https` - a `boolean` value specifying if the source should use https or not (default: `true`)
242
- * `:host` - a `string` specifying the host where the imglab server is located. (default: `cdn.imglab.io`)
260
+ * `:host` - a `string` specifying the host where the imglab server is located. (default: `imglab-cdn.net`)
243
261
  * `:port` - a `integer` specifying a port where the imglab server is located.
244
- * `:subdomains` - a `boolean` value specifying if the source should be specified using subdomains instead of using the path. (default: `false`)
262
+ * `:subdomains` - a `boolean` value specifying if the source should be specified using subdomains instead of using the path. (default: `true`)
245
263
 
246
- If we have our on-premises imglab server at `http://imglab.mycompany.com:8080` with a source named `web-images` we can use the following source settings to access a `logo.png` image:
264
+ If we have our on-premises imglab server at `http://my-company.com:8080` with a source named `images` we can use the following source settings to access a `logo.png` image:
247
265
 
248
266
  ```ruby
249
- source = Imglab::Source.new("web-images", https: false, host: "imglab.mycompany.com", port: 8080)
267
+ source = Imglab::Source.new("images", https: false, host: "my-company.com", port: 8080)
250
268
 
251
269
  Imglab.url(source, "logo.png", width: 300, height: 300, format: "png")
252
- "http://imglab.mycompany.com:8080/web-images/logo.png?width=300&height=300&format=png"
270
+ "http://images.my-company.com:8080/logo.png?width=300&height=300&format=png"
253
271
  ```
254
272
 
255
273
  It is possible to use secure sources too:
256
274
 
257
275
  ```ruby
258
276
  source = Imglab::Source.new(
259
- "web-images",
277
+ "images",
260
278
  https: false,
261
- host: "imglab.mycompany.com",
279
+ host: "my-company.com",
262
280
  port: 8080,
263
- secure_key: "web-images-secure-key",
264
- secure_salt: "web-images-secure-salt"
281
+ secure_key: "images-secure-key",
282
+ secure_salt: "images-secure-salt"
265
283
  )
266
284
 
267
285
  Imglab.url(source, "logo.png", width: 300, height: 300, format: "png")
268
- "http://imglab.mycompany.com:8080/web-images/logo.png?width=300&height=300&format=png&signature=generated-signature"
286
+ "http://images.my-company.com:8080/logo.png?width=300&height=300&format=png&signature=generated-signature"
269
287
  ```
270
288
 
271
- ### Using sudomains sources
289
+ ### Using sources with disabled subdomains
272
290
 
273
- In the case that your on-premises imglab server is configured to use source names as subdomains you can set `subdomains` attribute to `true` to generate URLs using subdomains:
291
+ In the case that your on-premises imglab server is configured to use source names as paths instead of subdomains you can set `subdomains` attribute to `false`:
274
292
 
275
293
  ```ruby
276
294
  source = Imglab::Source.new(
277
- "web-images",
295
+ "images",
278
296
  https: false,
279
- host: "imglab.mycompany.com",
297
+ host: "my-company.com",
280
298
  port: 8080,
281
- subdomains: true
299
+ subdomains: false
282
300
  )
283
301
 
284
- Imglab.url(source, "marketing/logo.png", width: 300, height: 300, format: "png")
285
- "http://web-images.imglab.mycompany.com:8080/marketing/logo.png?width=300&height=300&format=png"
302
+ Imglab.url(source, "logo.png", width: 300, height: 300, format: "png")
303
+ "http://my-company.com:8080/images/logo.png?width=300&height=300&format=png"
286
304
  ```
287
305
 
288
306
  ## License
data/lib/imglab/source.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Imglab::Source
2
2
  DEFAULT_HTTPS = true
3
- DEFAULT_HOST = "cdn.imglab.io"
4
- DEFAULT_SUBDOMAINS = false
3
+ DEFAULT_HOST = "imglab-cdn.net"
4
+ DEFAULT_SUBDOMAINS = true
5
5
 
6
6
  attr_reader :name, :https, :port, :secure_key, :secure_salt, :subdomains
7
7
 
@@ -13,7 +13,7 @@ class Imglab::Source
13
13
  # @param port [Integer] the port where the imglab server is located, only for imglab on-premises.
14
14
  # @param secure_key [String] the source secure key.
15
15
  # @param secure_salt [String] the source secure salt.
16
- # @param subdomains [Boolean] specify if the source should use subdomains to build the host name, only for imglab on-premises.
16
+ # @param subdomains [Boolean] specify if the source should use subdomains instead of paths to build the host name, only for imglab on-premises.
17
17
  # @return [Imglab::Source] with the specified options.
18
18
  def initialize(name, host: DEFAULT_HOST, https: DEFAULT_HTTPS, port: nil, secure_key: nil, secure_salt: nil, subdomains: DEFAULT_SUBDOMAINS)
19
19
  @name = name
@@ -50,7 +50,7 @@ class Imglab::Source
50
50
  # Returns if the source is secure or not.
51
51
  #
52
52
  # @return [Boolean]
53
- def secure?
53
+ def is_secure?
54
54
  @secure_key && @secure_salt
55
55
  end
56
56
 
data/lib/imglab/utils.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  class Imglab::Utils
2
- NORMALIZE_PATH_PREFIX_REGEXP = Regexp.compile(/\/*$/)
3
- NORMALIZE_PATH_SUFFIX_REGEXP = Regexp.compile(/\A\/*/)
2
+ NORMALIZE_PATH_PREFIX_REGEXP = Regexp.compile(/\A\/*/)
3
+ NORMALIZE_PATH_SUFFIX_REGEXP = Regexp.compile(/\/*$/)
4
+
5
+ WEB_URI_SCHEMES = %w[https http]
4
6
 
5
7
  # Returns a normalized path where suffix and prefix slashes are removed.
6
8
  #
@@ -16,14 +18,32 @@ class Imglab::Utils
16
18
  # @return [Hash]
17
19
  def self.normalize_params(params)
18
20
  params.inject({}) do |normalized_params, value|
19
- normalized_params[dasherize(value[0])] = value[1]
20
- normalized_params
21
+ normalized_params.merge(normalize_param(dasherize(value[0]), value[1]))
21
22
  end
22
23
  end
23
24
 
25
+ # Returns a boolean value indicating whether a string is a valid HTTP/HTTPS URI or not.
26
+ #
27
+ # @param uri [String]
28
+ # @return [Boolean]
29
+ def self.web_uri?(uri)
30
+ WEB_URI_SCHEMES.include?(URI.parse(uri).scheme)
31
+ rescue URI::Error
32
+ false
33
+ end
34
+
24
35
  private
25
36
 
26
37
  def self.dasherize(value)
27
38
  value.to_s.gsub("_", "-")
28
39
  end
40
+
41
+ def self.normalize_param(key, value)
42
+ case
43
+ when key == "expires" && value.instance_of?(Time)
44
+ {key => value.to_i}
45
+ else
46
+ {key => value}
47
+ end
48
+ end
29
49
  end
@@ -1,3 +1,3 @@
1
1
  module Imglab
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/imglab.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "erb"
2
+
1
3
  require "imglab/version"
2
4
  require "imglab/source"
3
5
  require "imglab/signature"
@@ -15,9 +17,9 @@ module Imglab
15
17
  # @raise [ArgumentError] when the source name or source parameter has a not expected type.
16
18
  #
17
19
  # @example Creating a URL specifying source name as string
18
- # Imglab.url("assets", "example.jpeg", width: 500, height: 600) #=> "https://cdn.imglab.io/assets/example.jpeg?width=500&height=600"
20
+ # Imglab.url("assets", "example.jpeg", width: 500, height: 600) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&height=600"
19
21
  # @example Creating a URL specifying a Imglab::Source
20
- # Imglab.url(Imglab::Source.new("assets"), "example.jpeg", width: 500, height: 600) #=> "https://cdn.imglab.io/assets/example.jpeg?width=500&height=600"
22
+ # Imglab.url(Imglab::Source.new("assets"), "example.jpeg", width: 500, height: 600) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&height=600"
21
23
  def self.url(source_name_or_source, path, params = {})
22
24
  case source_name_or_source
23
25
  when String
@@ -41,17 +43,31 @@ module Imglab
41
43
  source.host,
42
44
  source.port,
43
45
  nil,
44
- File.join("/", source.path(normalized_path)),
46
+ File.join("/", source.path(encode_path(normalized_path))),
45
47
  nil,
46
48
  encode_params(source, normalized_path, normalized_params),
47
49
  nil
48
50
  ).to_s
49
51
  end
50
52
 
53
+ def self.encode_path(path)
54
+ if Utils.web_uri?(path)
55
+ encode_path_component(path)
56
+ else
57
+ path.split("/").map do |path_component|
58
+ encode_path_component(path_component)
59
+ end.join("/")
60
+ end
61
+ end
62
+
63
+ def self.encode_path_component(path_component)
64
+ ERB::Util.url_encode(path_component)
65
+ end
66
+
51
67
  def self.encode_params(source, path, params)
52
68
  return encode_empty_params(source, path) if params.empty?
53
69
 
54
- if source.secure?
70
+ if source.is_secure?
55
71
  signature = Signature.generate(source, path, URI.encode_www_form(params))
56
72
 
57
73
  URI.encode_www_form(params.merge(signature: signature))
@@ -61,7 +77,7 @@ module Imglab
61
77
  end
62
78
 
63
79
  def self.encode_empty_params(source, path)
64
- if source.secure?
80
+ if source.is_secure?
65
81
  signature = Signature.generate(source, path)
66
82
 
67
83
  URI.encode_www_form(signature: signature)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imglab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - imglab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-24 00:00:00.000000000 Z
11
+ date: 2022-06-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Official Ruby library to integrate with imglab services.
14
14
  email:
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubygems_version: 3.0.3
57
+ rubygems_version: 3.1.6
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Official imglab Ruby library.