resizing 1.0.1 → 1.0.3
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 +4 -4
- data/.github/workflows/test.yml +8 -2
- data/.gitignore +6 -0
- data/Gemfile +6 -2
- data/README.md +59 -32
- data/lib/resizing/active_storage/service/resizing_service.rb +4 -2
- data/lib/resizing/carrier_wave/storage/file.rb +43 -10
- data/lib/resizing/carrier_wave.rb +15 -9
- data/lib/resizing/client.rb +24 -25
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +6 -16
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +6 -5
- data/lib/resizing/public_id.rb +5 -4
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +23 -12
- data/resizing.gemspec +5 -6
- data/test/resizing/carrier_wave_test.rb +336 -30
- data/test/resizing/client_test.rb +68 -12
- data/test/resizing/configuration_test.rb +2 -3
- data/test/resizing/error_test.rb +9 -9
- data/test/resizing/http_clientable_test.rb +1 -1
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing_module_test.rb +88 -6
- data/test/resizing_test.rb +16 -0
- data/test/test_helper.rb +146 -9
- data/test/vcr/carrier_wave_test/update_image.yml +63 -0
- metadata +37 -19
- /data/{exe → bin}/console +0 -0
- /data/{exe → bin}/generate-changelog +0 -0
- /data/{exe → bin}/setup +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f294d5e861e1eea2b9e47c22b5fbb72bbd2ef194b73543a24cf030473f5876ac
|
|
4
|
+
data.tar.gz: 51086d5f4785f7071128b0ccb62841a8527aaccebf456543b95ef090eb52f383
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6de5db8c05e6c2d7eff10d7315ad4300dd20e6a1a2d88b418f0486bd1310f23090b750ae42eb63d86ab2deda5bf3e49650b841ccacc63567f08da40a4c747728
|
|
7
|
+
data.tar.gz: cede047a60662714f51ba182251cb85280db895e83de00c498e6fa8c9c19d7302ac6132f7772ebc8b902ee711a173a833950267f6a2c9092c1f21a53f486d6db
|
data/.github/workflows/test.yml
CHANGED
|
@@ -7,22 +7,24 @@ on:
|
|
|
7
7
|
- master
|
|
8
8
|
|
|
9
9
|
permissions:
|
|
10
|
-
id-token: write # This is required for
|
|
10
|
+
id-token: write # This is required for codecov/codecov-action
|
|
11
11
|
contents: read # This is required for actions/checkout
|
|
12
12
|
|
|
13
13
|
jobs:
|
|
14
14
|
test:
|
|
15
|
-
name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
|
|
15
|
+
name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }} / Faraday ${{ matrix.faraday }}
|
|
16
16
|
strategy:
|
|
17
17
|
fail-fast: false
|
|
18
18
|
matrix:
|
|
19
19
|
ruby: ['3.1']
|
|
20
20
|
rails: ['6.1', '7.0']
|
|
21
|
+
faraday: ['1.0', '2.0']
|
|
21
22
|
|
|
22
23
|
runs-on: ubuntu-22.04
|
|
23
24
|
|
|
24
25
|
env:
|
|
25
26
|
RAILS_VERSION: ${{ matrix.rails }}
|
|
27
|
+
FARADAY_VERSION: ${{ matrix.faraday }}
|
|
26
28
|
|
|
27
29
|
steps:
|
|
28
30
|
- uses: actions/checkout@v4
|
|
@@ -51,6 +53,10 @@ jobs:
|
|
|
51
53
|
run: |
|
|
52
54
|
bundle install
|
|
53
55
|
|
|
56
|
+
- name: Show Faraday version
|
|
57
|
+
run: |
|
|
58
|
+
bundle list | grep faraday
|
|
59
|
+
|
|
54
60
|
- name: Run test
|
|
55
61
|
run: |
|
|
56
62
|
bundle exec rake test
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
|
@@ -9,8 +9,12 @@ gemspec
|
|
|
9
9
|
rails_version = ENV['RAILS_VERSION'] || '7.0'
|
|
10
10
|
gem 'rails', "~> #{rails_version}"
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
# Allow testing against different Faraday versions
|
|
13
|
+
faraday_version = ENV['FARADAY_VERSION'] || '2.0'
|
|
14
|
+
gem 'faraday', "~> #{faraday_version}"
|
|
15
|
+
|
|
16
|
+
gem 'byebug'
|
|
13
17
|
gem 'github_changelog_generator'
|
|
14
18
|
gem 'mysql2'
|
|
15
|
-
gem 'byebug'
|
|
16
19
|
gem 'pry-byebug'
|
|
20
|
+
gem 'rake', '~> 13.0'
|
data/README.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# Resizing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://rubygems.org/gems/resizing)
|
|
4
|
+
[](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
|
|
5
|
+
[](https://codecov.io/gh/jksy/resizing-gem)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
Client and utilities for [Resizing](https://www.resizing.net/) - an image hosting and transformation service.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Ruby 3.1.0 or later
|
|
6
12
|
|
|
7
13
|
## Installation
|
|
8
14
|
|
|
@@ -20,37 +26,59 @@ Or install it yourself as:
|
|
|
20
26
|
|
|
21
27
|
$ gem install resizing
|
|
22
28
|
|
|
29
|
+
## Configuration
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
Resizing.configure = {
|
|
33
|
+
image_host: 'https://img.resizing.net',
|
|
34
|
+
video_host: 'https://video.resizing.net',
|
|
35
|
+
project_id: 'your-project-id',
|
|
36
|
+
secret_token: 'your-secret-token'
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
23
40
|
## Usage
|
|
24
41
|
|
|
42
|
+
### Basic Client Usage
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
# Initialize client
|
|
46
|
+
client = Resizing::Client.new
|
|
47
|
+
|
|
48
|
+
# Upload image to resizing
|
|
49
|
+
file = File.open('sample.jpg', 'r')
|
|
50
|
+
response = client.post(file)
|
|
51
|
+
# => {
|
|
52
|
+
# "id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
|
|
53
|
+
# "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
|
|
54
|
+
# "content_type"=>"image/jpeg",
|
|
55
|
+
# "latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
|
|
56
|
+
# "latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
|
|
57
|
+
# "created_at"=>"2020-05-15T15:33:10.711Z",
|
|
58
|
+
# "updated_at"=>"2020-05-15T15:33:10.711Z",
|
|
59
|
+
# "url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
|
|
60
|
+
# }
|
|
61
|
+
|
|
62
|
+
# Generate transformation URL
|
|
63
|
+
image_id = response['id']
|
|
64
|
+
transformation_url = Resizing.url_from_image_id(image_id, nil, ['w_200', 'h_300'])
|
|
65
|
+
# => "https://img.resizing.net/projects/.../upload/images/.../w_200,h_300"
|
|
25
66
|
```
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"latest_etag"=>"\"190143614e6c342637584f46f18f8c58\"",
|
|
42
|
-
"created_at"=>"2020-05-15T15:33:10.711Z",
|
|
43
|
-
"updated_at"=>"2020-05-15T15:33:10.711Z",
|
|
44
|
-
"url"=>"/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469"
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
name = response['url']
|
|
48
|
-
# get transformation url
|
|
49
|
-
name = response['url']
|
|
50
|
-
transform = {width: 200, height: 300}
|
|
51
|
-
|
|
52
|
-
transformation_url = Resizing.url(name, transform)
|
|
53
|
-
=> "https://www.resizing.net/projects/098a2a0d-0000-0000-0000-000000000000/upload/images/a4ed2bf0-a4cf-44fa-9c82-b53e581cb469/width_200,height_300"
|
|
67
|
+
|
|
68
|
+
### CarrierWave Integration
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
class ImageUploader < CarrierWave::Uploader::Base
|
|
72
|
+
include Resizing::CarrierWave
|
|
73
|
+
|
|
74
|
+
version :list_smallest do
|
|
75
|
+
process resize_to_fill: [200, 200]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class User
|
|
80
|
+
mount_uploader :image, ImageUploader
|
|
81
|
+
end
|
|
54
82
|
```
|
|
55
83
|
|
|
56
84
|
## Development
|
|
@@ -61,8 +89,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
61
89
|
|
|
62
90
|
## Contributing
|
|
63
91
|
|
|
64
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing.
|
|
65
|
-
|
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing-gem.
|
|
66
93
|
|
|
67
94
|
## License
|
|
68
95
|
|
|
@@ -8,10 +8,12 @@ module Resizing
|
|
|
8
8
|
# ref.
|
|
9
9
|
# https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/s3_service.rb
|
|
10
10
|
#
|
|
11
|
-
# rubocop:disable
|
|
11
|
+
# rubocop:disable Metrics/ParameterLists
|
|
12
12
|
class ResizingService < ::ActiveStorage::Service
|
|
13
13
|
# def initialize(bucket:, upload: {}, public: false, **options)
|
|
14
|
+
# rubocop:disable Lint/MissingSuper
|
|
14
15
|
def initialize; end
|
|
16
|
+
# rubocop:enable Lint/MissingSuper
|
|
15
17
|
|
|
16
18
|
def upload(_key, _io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
|
|
17
19
|
raise NotImplementedError, 'upload is not implemented'
|
|
@@ -53,7 +55,7 @@ module Resizing
|
|
|
53
55
|
raise NotImplementedError, 'public_url is not implemented'
|
|
54
56
|
end
|
|
55
57
|
end
|
|
56
|
-
# rubocop:enable
|
|
58
|
+
# rubocop:enable Metrics/ParameterLists
|
|
57
59
|
end
|
|
58
60
|
end
|
|
59
61
|
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
module Resizing
|
|
4
4
|
module CarrierWave
|
|
5
5
|
module Storage
|
|
6
|
+
# rubocop:disable Metrics/ClassLength
|
|
6
7
|
class File
|
|
7
8
|
include ::CarrierWave::Utilities::Uri
|
|
8
9
|
|
|
@@ -28,6 +29,7 @@ module Resizing
|
|
|
28
29
|
@content_type || file.try(:content_type)
|
|
29
30
|
end
|
|
30
31
|
|
|
32
|
+
# rubocop:disable Metrics/AbcSize
|
|
31
33
|
def delete
|
|
32
34
|
# Use the identifier from constructor if available, otherwise try to get from model
|
|
33
35
|
if @public_id.present?
|
|
@@ -42,22 +44,27 @@ module Resizing
|
|
|
42
44
|
@public_id = Resizing::PublicId.new(column_value)
|
|
43
45
|
end
|
|
44
46
|
|
|
45
|
-
return if @public_id.empty?
|
|
47
|
+
return if @public_id.empty?
|
|
46
48
|
|
|
47
|
-
# 以下、既存のコード(変更なし)
|
|
48
49
|
resp = client.delete(@public_id.image_id)
|
|
50
|
+
|
|
51
|
+
# NOTE: 削除時のカラムクリアは以下の理由で必要:
|
|
52
|
+
# - 画像更新時: 古い画像IDと新しい画像IDが異なるため、古い画像削除時に新しいIDを消さないようにする
|
|
53
|
+
# - 明示的なremove!時: カラムをnilにする必要がある
|
|
54
|
+
# - clear_column_if_current_imageは削除される画像IDと現在のカラム値を比較して判断
|
|
49
55
|
if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found
|
|
50
|
-
|
|
56
|
+
clear_column_if_current_image
|
|
51
57
|
return
|
|
52
58
|
end
|
|
53
59
|
|
|
54
60
|
if @public_id.image_id == resp['id']
|
|
55
|
-
|
|
61
|
+
clear_column_if_current_image
|
|
56
62
|
return
|
|
57
63
|
end
|
|
58
64
|
|
|
59
65
|
raise APIError, "raise someone error:#{resp.inspect}"
|
|
60
66
|
end
|
|
67
|
+
# rubocop:enable Metrics/AbcSize
|
|
61
68
|
|
|
62
69
|
def extension
|
|
63
70
|
raise NotImplementedError, 'this method is do not used. maybe'
|
|
@@ -92,10 +99,16 @@ module Resizing
|
|
|
92
99
|
end
|
|
93
100
|
|
|
94
101
|
def current_path
|
|
102
|
+
# Return the path from @public_id if set (for retrieve scenarios),
|
|
103
|
+
# otherwise fall back to reading from model
|
|
104
|
+
return @public_id.to_s if @public_id.present?
|
|
105
|
+
|
|
95
106
|
@current_path = model.send :read_attribute, serialization_column
|
|
96
107
|
end
|
|
97
108
|
alias path current_path
|
|
98
109
|
|
|
110
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
111
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
99
112
|
def store(new_file)
|
|
100
113
|
if new_file.is_a?(self.class)
|
|
101
114
|
# new_file.copy_to(path)
|
|
@@ -126,18 +139,23 @@ module Resizing
|
|
|
126
139
|
@public_id = Resizing::PublicId.new(@response['public_id'])
|
|
127
140
|
@content_type = @response['content_type']
|
|
128
141
|
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
#
|
|
133
|
-
|
|
142
|
+
# NOTE: 理想的にはStorage::File内でモデルのカラムをいじらず、CarrierWaveに任せるべきだが、
|
|
143
|
+
# 現在の実装では以下の理由で必要:
|
|
144
|
+
# - CarrierWaveは write_uploader(column, mounter.identifiers.first) でカラムを更新
|
|
145
|
+
# - mounter.identifiers -> uploaders.map(&:identifier) -> storage.identifier -> uploader.filename
|
|
146
|
+
# - resizing-gemの filenameメソッドは read_column を返す(既存のカラム値)
|
|
147
|
+
# - そのため、CarrierWaveに任せると旧い値が書き戻されてしまう
|
|
148
|
+
# TODO: これを修正するには、Remote#identifierをオーバーライドして@public_id.to_sを返すか、
|
|
149
|
+
# uploader.filenameの実装を変更する必要がある
|
|
134
150
|
# save new value to model class
|
|
135
151
|
model.send :write_attribute, serialization_column, @public_id.to_s
|
|
136
152
|
|
|
137
153
|
true
|
|
138
154
|
end
|
|
155
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
156
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
139
157
|
|
|
140
|
-
def name(
|
|
158
|
+
def name(_options = {})
|
|
141
159
|
@public_id = PublicId.new(model.send(:read_attribute, serialization_column))
|
|
142
160
|
CGI.unescape(@public_id.filename)
|
|
143
161
|
end
|
|
@@ -170,6 +188,20 @@ module Resizing
|
|
|
170
188
|
@serialization_column ||= model.send(:_mounter, uploader.mounted_as).send(:serialization_column)
|
|
171
189
|
end
|
|
172
190
|
|
|
191
|
+
# Only clear the column if the deleted image is the current one
|
|
192
|
+
# (not when deleting an old image during update)
|
|
193
|
+
def clear_column_if_current_image
|
|
194
|
+
return if model.destroyed?
|
|
195
|
+
|
|
196
|
+
current_value = model.send(:read_attribute, serialization_column)
|
|
197
|
+
current_public_id = Resizing::PublicId.new(current_value)
|
|
198
|
+
|
|
199
|
+
# Only clear if the deleted image is the same as the current one
|
|
200
|
+
return unless current_public_id.image_id == @public_id.image_id
|
|
201
|
+
|
|
202
|
+
model.send :write_attribute, serialization_column, nil
|
|
203
|
+
end
|
|
204
|
+
|
|
173
205
|
##
|
|
174
206
|
# client of Resizing
|
|
175
207
|
def client
|
|
@@ -222,6 +254,7 @@ module Resizing
|
|
|
222
254
|
# "Need to implement #clean_cache! if you want to use #{self.class.name} as a cache storage."
|
|
223
255
|
# end
|
|
224
256
|
end
|
|
257
|
+
# rubocop:enable Metrics/ClassLength
|
|
225
258
|
end
|
|
226
259
|
end
|
|
227
260
|
end
|
|
@@ -4,6 +4,7 @@ require 'resizing/carrier_wave/storage/file'
|
|
|
4
4
|
require 'resizing/carrier_wave/storage/remote'
|
|
5
5
|
|
|
6
6
|
module Resizing
|
|
7
|
+
# rubocop:disable Metrics/ModuleLength
|
|
7
8
|
module CarrierWave
|
|
8
9
|
class Railtie < ::Rails::Railtie
|
|
9
10
|
# Railtie skelton codes
|
|
@@ -28,11 +29,20 @@ module Resizing
|
|
|
28
29
|
def initialize(*args)
|
|
29
30
|
@requested_format = nil
|
|
30
31
|
@default_format = nil
|
|
32
|
+
@retrieved_identifier = nil
|
|
31
33
|
super
|
|
32
34
|
end
|
|
33
35
|
|
|
36
|
+
# Override to store the identifier and set up @file for later use
|
|
37
|
+
def retrieve_from_store!(identifier)
|
|
38
|
+
@retrieved_identifier = identifier
|
|
39
|
+
super
|
|
40
|
+
# Ensure @file is set up so that remove! can call @file.delete
|
|
41
|
+
file
|
|
42
|
+
end
|
|
43
|
+
|
|
34
44
|
def file
|
|
35
|
-
file_identifier = identifier || read_column
|
|
45
|
+
file_identifier = @retrieved_identifier || identifier || read_column
|
|
36
46
|
|
|
37
47
|
return nil if file_identifier.blank?
|
|
38
48
|
|
|
@@ -46,7 +56,7 @@ module Resizing
|
|
|
46
56
|
|
|
47
57
|
transforms = args.map do |version|
|
|
48
58
|
version = version.intern
|
|
49
|
-
raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.
|
|
59
|
+
raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.key? version
|
|
50
60
|
|
|
51
61
|
versions[version].transform_string
|
|
52
62
|
end.compact
|
|
@@ -110,11 +120,6 @@ module Resizing
|
|
|
110
120
|
read_column
|
|
111
121
|
end
|
|
112
122
|
|
|
113
|
-
def store!
|
|
114
|
-
# DO NOTHING
|
|
115
|
-
super
|
|
116
|
-
end
|
|
117
|
-
|
|
118
123
|
def serialization_column
|
|
119
124
|
model.send(:_mounter, mounted_as).send(:serialization_column)
|
|
120
125
|
end
|
|
@@ -152,7 +157,7 @@ module Resizing
|
|
|
152
157
|
|
|
153
158
|
private
|
|
154
159
|
|
|
155
|
-
# rubocop:disable Metrics/AbcSize
|
|
160
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
156
161
|
def transform_string_from(processor)
|
|
157
162
|
action = processor.first
|
|
158
163
|
value = processor.second
|
|
@@ -175,6 +180,7 @@ module Resizing
|
|
|
175
180
|
"#{key}_#{value}"
|
|
176
181
|
end.compact.join(',')
|
|
177
182
|
end
|
|
178
|
-
# rubocop:enable Metrics/AbcSize
|
|
183
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
179
184
|
end
|
|
185
|
+
# rubocop:enable Metrics/ModuleLength
|
|
180
186
|
end
|
data/lib/resizing/client.rb
CHANGED
|
@@ -24,6 +24,7 @@ module Resizing
|
|
|
24
24
|
# }
|
|
25
25
|
#
|
|
26
26
|
#++
|
|
27
|
+
# rubocop:disable Metrics/ClassLength
|
|
27
28
|
class Client
|
|
28
29
|
include Resizing::Constants
|
|
29
30
|
include Resizing::Configurable
|
|
@@ -44,7 +45,7 @@ module Resizing
|
|
|
44
45
|
|
|
45
46
|
url = build_post_url
|
|
46
47
|
params = {
|
|
47
|
-
image:
|
|
48
|
+
image: Resizing.file_part_class.new(filename_or_io, options[:content_type], filename)
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
response = handle_faraday_error do
|
|
@@ -53,8 +54,7 @@ module Resizing
|
|
|
53
54
|
end
|
|
54
55
|
end
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
result
|
|
57
|
+
handle_create_response(response)
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def put(image_id, filename_or_io, options)
|
|
@@ -64,7 +64,7 @@ module Resizing
|
|
|
64
64
|
|
|
65
65
|
url = build_put_url(image_id)
|
|
66
66
|
params = {
|
|
67
|
-
image:
|
|
67
|
+
image: Resizing.file_part_class.new(filename_or_io, options[:content_type], filename)
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
response = handle_faraday_error do
|
|
@@ -73,8 +73,7 @@ module Resizing
|
|
|
73
73
|
end
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
result
|
|
76
|
+
handle_create_response(response)
|
|
78
77
|
end
|
|
79
78
|
|
|
80
79
|
def delete(image_id)
|
|
@@ -86,8 +85,7 @@ module Resizing
|
|
|
86
85
|
end
|
|
87
86
|
end
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
result
|
|
88
|
+
handle_delete_response(response)
|
|
91
89
|
end
|
|
92
90
|
|
|
93
91
|
def metadata(image_id, options = {})
|
|
@@ -99,8 +97,7 @@ module Resizing
|
|
|
99
97
|
end
|
|
100
98
|
end
|
|
101
99
|
|
|
102
|
-
|
|
103
|
-
result
|
|
100
|
+
handle_metadata_response(response, options)
|
|
104
101
|
end
|
|
105
102
|
|
|
106
103
|
private
|
|
@@ -114,8 +111,7 @@ module Resizing
|
|
|
114
111
|
end
|
|
115
112
|
|
|
116
113
|
def gather_filename(filename_or_io, options)
|
|
117
|
-
|
|
118
|
-
filename ||= filename_or_io.respond_to?(:path) ? File.basename(filename_or_io.path) : nil
|
|
114
|
+
options[:filename] || (filename_or_io.respond_to?(:path) ? File.basename(filename_or_io.path) : nil)
|
|
119
115
|
end
|
|
120
116
|
|
|
121
117
|
def build_put_url(image_id)
|
|
@@ -140,17 +136,14 @@ module Resizing
|
|
|
140
136
|
# Accept IO-like objects (StringIO, Tempfile, etc.)
|
|
141
137
|
return if filename_or_io.respond_to?(:read) && filename_or_io.respond_to?(:rewind)
|
|
142
138
|
|
|
143
|
-
if filename_or_io.is_a?(String)
|
|
144
|
-
if File.exist?(filename_or_io)
|
|
145
|
-
return
|
|
146
|
-
end
|
|
147
|
-
end
|
|
139
|
+
return if filename_or_io.is_a?(String) && File.exist?(filename_or_io)
|
|
148
140
|
|
|
149
|
-
raise ArgumentError,
|
|
141
|
+
raise ArgumentError,
|
|
142
|
+
"filename_or_io must be a File object, an IO-like object, or a path to a file (#{filename_or_io.class})"
|
|
150
143
|
end
|
|
151
144
|
|
|
152
145
|
def handle_create_response(response)
|
|
153
|
-
raise APIError,
|
|
146
|
+
raise APIError, 'No response is returned' if response.nil?
|
|
154
147
|
|
|
155
148
|
case response.status
|
|
156
149
|
when HTTP_STATUS_OK, HTTP_STATUS_CREATED
|
|
@@ -161,7 +154,7 @@ module Resizing
|
|
|
161
154
|
end
|
|
162
155
|
|
|
163
156
|
def handle_delete_response(response)
|
|
164
|
-
raise APIError,
|
|
157
|
+
raise APIError, 'No response is returned' if response.nil?
|
|
165
158
|
|
|
166
159
|
case response.status
|
|
167
160
|
when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
|
|
@@ -174,24 +167,30 @@ module Resizing
|
|
|
174
167
|
def handle_metadata_response(response, options = {})
|
|
175
168
|
when_not_found = options[:when_not_found] || nil
|
|
176
169
|
|
|
177
|
-
raise APIError,
|
|
170
|
+
raise APIError, 'No response is returned' if response.nil?
|
|
178
171
|
|
|
179
172
|
case response.status
|
|
180
|
-
when HTTP_STATUS_OK
|
|
173
|
+
when HTTP_STATUS_OK
|
|
181
174
|
JSON.parse(response.body)
|
|
182
175
|
when HTTP_STATUS_NOT_FOUND
|
|
183
176
|
raise decode_error_from(response) if when_not_found == :raise
|
|
184
|
-
|
|
177
|
+
|
|
178
|
+
JSON.parse(response.body)
|
|
185
179
|
else
|
|
186
180
|
raise decode_error_from(response)
|
|
187
181
|
end
|
|
188
182
|
end
|
|
189
183
|
|
|
190
|
-
def decode_error_from
|
|
191
|
-
result =
|
|
184
|
+
def decode_error_from(response)
|
|
185
|
+
result = begin
|
|
186
|
+
JSON.parse(response.body)
|
|
187
|
+
rescue StandardError
|
|
188
|
+
{}
|
|
189
|
+
end
|
|
192
190
|
err = APIError.new(result['message'] || "invalid http status code #{response.status}")
|
|
193
191
|
err.decoded_body = result
|
|
194
192
|
err
|
|
195
193
|
end
|
|
196
194
|
end
|
|
195
|
+
# rubocop:enable Metrics/ClassLength
|
|
197
196
|
end
|
|
@@ -27,12 +27,8 @@ module Resizing
|
|
|
27
27
|
def initialize(*attrs)
|
|
28
28
|
case attr = attrs.first
|
|
29
29
|
when Hash
|
|
30
|
-
if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
if attr[:host].present?
|
|
34
|
-
raise_configiration_error
|
|
35
|
-
end
|
|
30
|
+
raise_configiration_error if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
+
raise_configiration_error if attr[:host].present?
|
|
36
32
|
|
|
37
33
|
initialize_by_hash attr
|
|
38
34
|
return
|
|
@@ -41,11 +37,6 @@ module Resizing
|
|
|
41
37
|
raise_configiration_error
|
|
42
38
|
end
|
|
43
39
|
|
|
44
|
-
def host
|
|
45
|
-
Kernel.warn "[DEPRECATED] The Configuration#host is deprecated. Use Configuration#image_host."
|
|
46
|
-
self.image_host
|
|
47
|
-
end
|
|
48
|
-
|
|
49
40
|
def generate_auth_header
|
|
50
41
|
current_timestamp = Time.now.to_i
|
|
51
42
|
data = [current_timestamp, secret_token].join('|')
|
|
@@ -102,13 +93,11 @@ module Resizing
|
|
|
102
93
|
raise ConfigurationError, 'need hash and some keys like :image_host, video_host, :project_id, :secret_token'
|
|
103
94
|
end
|
|
104
95
|
|
|
96
|
+
# rubocop:disable Metrics/AbcSize
|
|
105
97
|
def initialize_by_hash(attr)
|
|
106
|
-
|
|
107
|
-
if attr[:host].present?
|
|
108
|
-
Kernel.warn "[DEPRECATED] The host on configration is deprecated. Use image_host, video_host" if attr[:host].present?
|
|
109
|
-
@image_host ||= attr[:host].dup.freeze || DEFAULT_HOST # for backward compatible
|
|
110
|
-
end
|
|
98
|
+
raise 'The host on configuration is deprecated. Use image_host, video_host' if attr[:host].present?
|
|
111
99
|
|
|
100
|
+
@image_host = attr[:image_host].dup.freeze || DEFAULT_IMAGE_HOST
|
|
112
101
|
@video_host = attr[:video_host].dup.freeze || DEFAULT_VIDEO_HOST
|
|
113
102
|
@project_id = attr[:project_id].dup.freeze
|
|
114
103
|
@secret_token = attr[:secret_token].dup.freeze
|
|
@@ -116,5 +105,6 @@ module Resizing
|
|
|
116
105
|
@response_timeout = attr[:response_timeout] || DEFAULT_RESPONSE_TIMEOUT
|
|
117
106
|
@enable_mock = attr[:enable_mock] || false
|
|
118
107
|
end
|
|
108
|
+
# rubocop:enable Metrics/AbcSize
|
|
119
109
|
end
|
|
120
110
|
end
|
|
@@ -12,14 +12,14 @@ module Resizing
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def handle_faraday_error
|
|
15
|
+
def handle_faraday_error
|
|
16
16
|
yield
|
|
17
17
|
rescue Faraday::TimeoutError => e
|
|
18
18
|
handle_timeout_error e
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def handle_timeout_error
|
|
22
|
-
raise APIError
|
|
21
|
+
def handle_timeout_error(error)
|
|
22
|
+
raise APIError, "TimeoutError: #{error.inspect}"
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|
data/lib/resizing/mock_client.rb
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Resizing
|
|
4
4
|
class MockClient
|
|
5
|
-
def post(
|
|
5
|
+
def post(_file_or_binary, _options = {})
|
|
6
6
|
r = load_yaml('test/vcr/client/post.yml')
|
|
7
7
|
JSON.parse(r['string'])
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
def put(name,
|
|
10
|
+
def put(name, _file_or_binary, _options)
|
|
11
11
|
r = load_yaml('test/vcr/client/put.yml')
|
|
12
12
|
result = JSON.parse(r['string'])
|
|
13
13
|
# replace name, public_id and version by name argument
|
|
@@ -26,7 +26,7 @@ module Resizing
|
|
|
26
26
|
result
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def metadata(name)
|
|
29
|
+
def metadata(name, _options = {})
|
|
30
30
|
r = load_yaml('test/vcr/client/metadata.yml')
|
|
31
31
|
result = JSON.parse(r['string'])
|
|
32
32
|
# replace name and public_id by name argument
|
|
@@ -34,15 +34,16 @@ module Resizing
|
|
|
34
34
|
result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
|
|
35
35
|
result
|
|
36
36
|
end
|
|
37
|
+
|
|
37
38
|
private
|
|
38
39
|
|
|
39
|
-
def load_yaml
|
|
40
|
+
def load_yaml(filename)
|
|
40
41
|
path = "#{library_root}/#{filename}"
|
|
41
42
|
YAML.load_file(path)['http_interactions'].first['response']['body']
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
def library_root
|
|
45
|
-
@library_root ||= File.expand_path('
|
|
46
|
+
@library_root ||= File.expand_path('../..', __dir__)
|
|
46
47
|
end
|
|
47
48
|
end
|
|
48
49
|
end
|