cloudimage 0.4.0 → 0.5.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: 72e83343c301b5b791cd7fa5f88593fd3715cfa01d918e6d473aae4376022594
4
- data.tar.gz: '059edd8cd34c45bf7226cd876fb10d64d4cefc93059996d6e12f66a0ccc2d96d'
3
+ metadata.gz: 67e8f90abd62c87ace671bfb901dc04fe4acd45eb4bdef4d85a5b3208d0b3b3e
4
+ data.tar.gz: 683086c1cd095ee0121a17d30d498f11a29d558b6e855b55b5d3a4232e312b08
5
5
  SHA512:
6
- metadata.gz: 24d09cd09df390e46f6e9e0035ae3df80eb458a2b80c7f6f39d922c21c333063e4abd95302f63a007dc6da289301875b3bd7ec87c2c21daa2d08dd96799d8b29
7
- data.tar.gz: 805d1c5b001cebd0deaf230111785284c69e6759e3d64f18d635df832195495a19afa209a1c0fc45484b44dd7ead82edff532747cfec12a8d838851cfc7607fa
6
+ metadata.gz: b409ab0b86ba4b26c4b0e2d8d36b201f1383bdeaa67e729982eecf7ff3ad0ab142e876cb5c3294cb81ea28b985da3903f4a947f0b300e08938dd806cab1fa842
7
+ data.tar.gz: 4bd6d593d935c1a74cf1a6a5d440337937c88dc530d57ef71f3bc3b38b8a902bd3088b513df13700cef2c25d6d0901ac3381c5aba2dddcb967ec43f04fa7f639
@@ -1,18 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.4.0](https://github.com/scaleflex/cloudimage-rb/tree/v0.4.0) (2020-07-19)
4
+
5
+ [Full Changelog](https://github.com/scaleflex/cloudimage-rb/compare/v0.3.0...v0.4.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Add support for aliases [\#20](https://github.com/scaleflex/cloudimage-rb/pull/20) ([janklimo](https://github.com/janklimo))
10
+
3
11
  ## [v0.3.0](https://github.com/scaleflex/cloudimage-rb/tree/v0.3.0) (2020-07-09)
4
12
 
5
13
  [Full Changelog](https://github.com/scaleflex/cloudimage-rb/compare/v0.2.1...v0.3.0)
6
14
 
7
- **Closed issues:**
15
+ **Implemented enhancements:**
8
16
 
9
- - Add URL sealing [\#12](https://github.com/scaleflex/cloudimage-rb/issues/12)
10
- - Add automatic changelog generation [\#11](https://github.com/scaleflex/cloudimage-rb/issues/11)
17
+ - Introduce URL sealing [\#10](https://github.com/scaleflex/cloudimage-rb/pull/10) ([janklimo](https://github.com/janklimo))
18
+ - Add support for image resizing [\#2](https://github.com/scaleflex/cloudimage-rb/pull/2) ([janklimo](https://github.com/janklimo))
11
19
 
12
20
  **Merged pull requests:**
13
21
 
14
22
  - Use changelog generation [\#16](https://github.com/scaleflex/cloudimage-rb/pull/16) ([janklimo](https://github.com/janklimo))
15
- - Introduce URL sealing [\#10](https://github.com/scaleflex/cloudimage-rb/pull/10) ([janklimo](https://github.com/janklimo))
16
23
  - Add test coverage with SimpleCov [\#9](https://github.com/scaleflex/cloudimage-rb/pull/9) ([janklimo](https://github.com/janklimo))
17
24
 
18
25
  ## 0.2.1 (2020-06-29)
data/README.md CHANGED
@@ -15,9 +15,11 @@ Supports Ruby `2.4` and above, `JRuby`, and `TruffleRuby`.
15
15
  - [Method aliases](#method-aliases)
16
16
  - [Custom helpers](#custom-helpers)
17
17
  - [URL aliases](#url-aliases)
18
+ - [CNAME](#cname)
18
19
  - [Security](#security)
19
20
  - [URL signature](#url-signature)
20
21
  - [URL sealing](#url-sealing)
22
+ - [Invalidation API](#invalidation-api)
21
23
  - [Development](#development)
22
24
  - [Contributing](#contributing)
23
25
  - [License](#license)
@@ -41,7 +43,7 @@ Or install it yourself as:
41
43
 
42
44
  ## Usage
43
45
 
44
- The only requirement to get started is your customer token. You can
46
+ The most common way to use Cloudimage is by means of your customer token. You can
45
47
  find it within your Admin interface:
46
48
 
47
49
  ![token](docs/token.png)
@@ -55,13 +57,15 @@ client = Cloudimage::Client.new(token: 'mysecrettoken')
55
57
 
56
58
  Cloudimage client accepts the following options:
57
59
 
58
- | Option | Required? | Type | Additional info |
59
- | ------------------ | --------- | ------- | --------------------------------------------------- |
60
- | `token` | Yes | string | |
61
- | `salt` | No | string | See [Security](#security). |
62
- | `signature_length` | No | integer | Integer value in the range `6..40`. Defaults to 18. |
63
- | `sign_urls` | No | boolean | Defaults to `true`. See [Security](#security). |
64
- | `aliases` | No | hash | See [URL aliases](#url-aliases). |
60
+ | Option | Type | Additional info |
61
+ | ------------------ | ------- | ------------------------------------------------------------- |
62
+ | `token` | string | Required if `cname` is missing. |
63
+ | `cname` | string | Required if `token` is missing. See [CNAME](#cname). |
64
+ | `salt` | string | Optional. See [Security](#security). |
65
+ | `signature_length` | integer | Optional. Integer value in the range `6..40`. Defaults to 18. |
66
+ | `sign_urls` | boolean | Optional. Defaults to `true`. See [Security](#security). |
67
+ | `aliases` | hash | Optional. See [URL aliases](#url-aliases). |
68
+ | `api_key` | string | Optional. See [Invalidation API](#invalidation-api). |
65
69
 
66
70
  Calling `path` on the client object returns an instance of `Cloudimage::URI`.
67
71
  It accepts path to the image as a string and we we will use it to build
@@ -145,6 +149,17 @@ client.path('https://store.s3-us-west-2.amazonaws.com/uploads/image.jpg').to_url
145
149
  # => "https://token.cloudimg.io/v7/image.jpg"
146
150
  ```
147
151
 
152
+ ### CNAME
153
+
154
+ If you have a custom CNAME configured for your account, you can
155
+ use it to initialize the client:
156
+
157
+ ```ruby
158
+ client = Cloudimage::Client.new(cname: 'img.klimo.io')
159
+ client.path('/assets/image.jpg').to_url
160
+ # => 'https://img.klimo.io/v7/assets/image.jpg'
161
+ ```
162
+
148
163
  ### Security
149
164
 
150
165
  #### URL signature
@@ -201,6 +216,29 @@ client
201
216
  This approach protects `w` and `f` values from being edited but
202
217
  makes it possible to freely modify the value of `h`.
203
218
 
219
+ ### Invalidation API
220
+
221
+ To access invalidation API you'll need to initialize client with
222
+ an API key.
223
+
224
+ The provided helper methods accept any number of strings:
225
+
226
+ ```ruby
227
+ client = Cloudimage::Client.new(token: 'token', api_key: 'key')
228
+
229
+ # Invalidate original
230
+ client.invalidate_original('/v7/image.jpg')
231
+
232
+ # Invalidate URLs
233
+ client.invalidate_urls('/v7/image.jpg?w=200', '/v7/image.jpg?h=300')
234
+
235
+ # Invalidate all
236
+ client.invalidate_all
237
+ ```
238
+
239
+ Consult the [API docs](https://docs.cloudimage.io/go/cloudimage-documentation-v7/en/caching-acceleration/invalidation-api)
240
+ for further details.
241
+
204
242
  ## Development
205
243
 
206
244
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -1,27 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'uri'
4
+ require_relative 'invalidation'
4
5
 
5
6
  module Cloudimage
6
7
  class InvalidConfig < StandardError; end
7
8
 
8
9
  class Client
10
+ include Invalidation
11
+
9
12
  attr_reader :config
10
13
 
11
14
  API_VERSION = 'v7'
12
15
  DEFAULT_SIGNATURE_LENGTH = 18
13
16
 
14
17
  def initialize(**options)
15
- @config = {}
16
- @config[:token] = options[:token]
17
- @config[:salt] = options[:salt]
18
- @config[:signature_length] =
19
- options[:signature_length] || DEFAULT_SIGNATURE_LENGTH
20
- @config[:api_version] = API_VERSION
21
- @config[:sign_urls] = options[:sign_urls].nil? ? true : false
22
- @config[:aliases] = options[:aliases] || {}
23
-
24
- ensure_valid_config
18
+ @config = set_config_defaults(options)
19
+ validate_config
25
20
  end
26
21
 
27
22
  def path(path)
@@ -30,18 +25,29 @@ module Cloudimage
30
25
 
31
26
  private
32
27
 
33
- def ensure_valid_config
34
- ensure_valid_token
35
- ensure_valid_signature_length
28
+ def set_config_defaults(options)
29
+ options.tap do |config|
30
+ config[:signature_length] =
31
+ options[:signature_length] || DEFAULT_SIGNATURE_LENGTH
32
+ config[:api_version] = API_VERSION
33
+ config[:sign_urls] = options[:sign_urls].nil? ? true : false
34
+ config[:aliases] = options[:aliases] || {}
35
+ end
36
+ end
37
+
38
+ def validate_config
39
+ validate_site_config
40
+ validate_signature_length
36
41
  end
37
42
 
38
- def ensure_valid_token
39
- return unless config[:token].nil?
43
+ def validate_site_config
44
+ return unless config[:token].nil? && config[:cname].nil?
40
45
 
41
- raise InvalidConfig, 'Please specify your Cloudimage customer token.'
46
+ raise InvalidConfig,
47
+ 'Please specify your customer token or a custom CNAME.'
42
48
  end
43
49
 
44
- def ensure_valid_signature_length
50
+ def validate_signature_length
45
51
  return if config[:salt].nil?
46
52
  return if (6..40).cover? config[:signature_length]
47
53
 
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module Cloudimage
7
+ module Invalidation
8
+ ENDPOINT = ::URI.parse('https://api.cloudimage.com/invalidate')
9
+
10
+ %i[original urls all].each do |type|
11
+ define_method "invalidate_#{type}" do |*paths|
12
+ validate_api_key
13
+
14
+ body = {
15
+ scope: type,
16
+ }
17
+
18
+ body[:urls] = paths if paths.any?
19
+
20
+ send_request(body)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def validate_api_key
27
+ return if config[:api_key]
28
+
29
+ raise InvalidConfig, 'API key is required to perform cache invalidation.'
30
+ end
31
+
32
+ def headers
33
+ {
34
+ 'X-Client-Key': config[:api_key],
35
+ 'Content-Type': 'application/json',
36
+ }
37
+ end
38
+
39
+ def send_request(body)
40
+ http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
41
+ http.use_ssl = true
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+
44
+ request = Net::HTTP::Post.new(ENDPOINT.path, headers)
45
+ request.body = body.to_json
46
+
47
+ http.request(request)
48
+ end
49
+ end
50
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  require_relative 'params'
4
6
  require_relative 'custom_helpers'
5
7
  require_relative 'security'
@@ -43,6 +45,8 @@ module Cloudimage
43
45
  private
44
46
 
45
47
  def site
48
+ return "https://#{config[:cname]}" if config[:cname]
49
+
46
50
  "https://#{config[:token]}.cloudimg.io"
47
51
  end
48
52
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Klimo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-19 00:00:00.000000000 Z
11
+ date: 2020-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -68,6 +68,7 @@ files:
68
68
  - lib/cloudimage.rb
69
69
  - lib/cloudimage/client.rb
70
70
  - lib/cloudimage/custom_helpers.rb
71
+ - lib/cloudimage/invalidation.rb
71
72
  - lib/cloudimage/params.rb
72
73
  - lib/cloudimage/refinements.rb
73
74
  - lib/cloudimage/security.rb