resizing 1.0.3 → 1.1.0.pre

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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +2 -13
  3. data/.gitignore +0 -6
  4. data/Gemfile +2 -10
  5. data/README.md +32 -59
  6. data/lib/resizing/active_storage/service/resizing_service.rb +2 -6
  7. data/lib/resizing/carrier_wave/storage/file.rb +24 -68
  8. data/lib/resizing/carrier_wave/storage/remote.rb +3 -7
  9. data/lib/resizing/carrier_wave.rb +11 -19
  10. data/lib/resizing/client.rb +24 -40
  11. data/lib/resizing/configurable.rb +1 -1
  12. data/lib/resizing/configuration.rb +16 -6
  13. data/lib/resizing/http_clientable.rb +3 -3
  14. data/lib/resizing/mock_client.rb +5 -6
  15. data/lib/resizing/public_id.rb +4 -5
  16. data/lib/resizing/version.rb +1 -1
  17. data/lib/resizing/video/client.rb +116 -0
  18. data/lib/resizing/video.rb +8 -0
  19. data/lib/resizing.rb +12 -24
  20. data/resizing.gemspec +11 -9
  21. data/test/resizing/carrier_wave/storage/file_test.rb +8 -149
  22. data/test/resizing/carrier_wave/storage/remote_test.rb +0 -75
  23. data/test/resizing/carrier_wave_test.rb +32 -373
  24. data/test/resizing/client_test.rb +11 -96
  25. data/test/resizing/configuration_test.rb +2 -118
  26. data/test/resizing/public_id_test.rb +1 -1
  27. data/test/resizing/video/client_test.rb +158 -0
  28. data/test/resizing_test.rb +0 -16
  29. data/test/test_helper.rb +9 -148
  30. data/test/vcr/video/metadata/success.yml +47 -0
  31. data/test/vcr/video/prepare/success.yml +47 -0
  32. data/test/vcr/video/upload_completed/success.yml +47 -0
  33. metadata +43 -56
  34. data/lib/resizing/active_storage/service.rb +0 -9
  35. data/lib/resizing/active_storage.rb +0 -7
  36. data/test/resizing/active_storage_service_test.rb +0 -98
  37. data/test/resizing/configurable_test.rb +0 -82
  38. data/test/resizing/constants_test.rb +0 -25
  39. data/test/resizing/error_test.rb +0 -73
  40. data/test/resizing/http_clientable_test.rb +0 -84
  41. data/test/resizing/mock_client_test.rb +0 -75
  42. data/test/resizing_module_test.rb +0 -206
  43. data/test/vcr/carrier_wave_test/update_image.yml +0 -63
  44. /data/{bin → exe}/console +0 -0
  45. /data/{bin → exe}/generate-changelog +0 -0
  46. /data/{bin → exe}/setup +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f294d5e861e1eea2b9e47c22b5fbb72bbd2ef194b73543a24cf030473f5876ac
4
- data.tar.gz: 51086d5f4785f7071128b0ccb62841a8527aaccebf456543b95ef090eb52f383
3
+ metadata.gz: 2d601d6b8f87f6440de0c1c4d4dcbb74e7bc386dd3e217b66b8ac259d35939f0
4
+ data.tar.gz: 3f2ce39dcb8879dee7a9717142bfa794e6cad5671e19cdd01afbb5f7dacc6787
5
5
  SHA512:
6
- metadata.gz: 6de5db8c05e6c2d7eff10d7315ad4300dd20e6a1a2d88b418f0486bd1310f23090b750ae42eb63d86ab2deda5bf3e49650b841ccacc63567f08da40a4c747728
7
- data.tar.gz: cede047a60662714f51ba182251cb85280db895e83de00c498e6fa8c9c19d7302ac6132f7772ebc8b902ee711a173a833950267f6a2c9092c1f21a53f486d6db
6
+ metadata.gz: c221c9290ccf562d804bb9f5dca6657568c891ce4c14d825fb8a19627feec1982c09773bfb55304cd5a13b8c4bb786a57d1abe3589684da312fd0167ecfc9a28
7
+ data.tar.gz: ee2dde3b29accd86dae059daf0e164fef634476886fb254b2145e3be33764d3a81308d9eea382a557817951714cbc8d4e98b02318417042a38590a4109334630
@@ -7,25 +7,18 @@ on:
7
7
  - master
8
8
 
9
9
  permissions:
10
- id-token: write # This is required for codecov/codecov-action
10
+ id-token: write # This is required for requesting the JWT
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 }} / Faraday ${{ matrix.faraday }}
16
15
  strategy:
17
16
  fail-fast: false
18
17
  matrix:
19
- ruby: ['3.1']
20
- rails: ['6.1', '7.0']
21
- faraday: ['1.0', '2.0']
18
+ ruby: ['2.7.6', '3.1', '3.2','3.3']
22
19
 
23
20
  runs-on: ubuntu-22.04
24
21
 
25
- env:
26
- RAILS_VERSION: ${{ matrix.rails }}
27
- FARADAY_VERSION: ${{ matrix.faraday }}
28
-
29
22
  steps:
30
23
  - uses: actions/checkout@v4
31
24
 
@@ -53,10 +46,6 @@ jobs:
53
46
  run: |
54
47
  bundle install
55
48
 
56
- - name: Show Faraday version
57
- run: |
58
- bundle list | grep faraday
59
-
60
49
  - name: Run test
61
50
  run: |
62
51
  bundle exec rake test
data/.gitignore CHANGED
@@ -6,9 +6,3 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- vendor/
10
- Gemfile.lock
11
- tags
12
- sample1.jpg
13
- .ruby-version
14
- uploads/
data/Gemfile CHANGED
@@ -5,16 +5,8 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in resizing.gemspec
6
6
  gemspec
7
7
 
8
- # Allow testing against different Rails versions
9
- rails_version = ENV['RAILS_VERSION'] || '7.0'
10
- gem 'rails', "~> #{rails_version}"
11
-
12
- # Allow testing against different Faraday versions
13
- faraday_version = ENV['FARADAY_VERSION'] || '2.0'
14
- gem 'faraday', "~> #{faraday_version}"
15
-
16
- gem 'byebug'
8
+ gem 'rake', '~> 13.0'
17
9
  gem 'github_changelog_generator'
18
10
  gem 'mysql2'
11
+ gem 'byebug'
19
12
  gem 'pry-byebug'
20
- gem 'rake', '~> 13.0'
data/README.md CHANGED
@@ -1,14 +1,8 @@
1
1
  # Resizing
2
2
 
3
- [![Gem Version](https://img.shields.io/gem/v/resizing.svg)](https://rubygems.org/gems/resizing)
4
- [![test](https://github.com/jksy/resizing-gem/actions/workflows/test.yml/badge.svg)](https://github.com/jksy/resizing-gem/actions/workflows/test.yml)
5
- [![codecov](https://codecov.io/gh/jksy/resizing-gem/graph/badge.svg)](https://codecov.io/gh/jksy/resizing-gem)
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/resizing`. To experiment with that code, run `bin/console` for an interactive prompt.
6
4
 
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
5
+ TODO: Delete this and the text above, and describe your gem
12
6
 
13
7
  ## Installation
14
8
 
@@ -26,59 +20,37 @@ Or install it yourself as:
26
20
 
27
21
  $ gem install resizing
28
22
 
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
-
40
23
  ## Usage
41
24
 
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"
66
25
  ```
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
26
+ # initialize client
27
+ options = {
28
+ project_id: '098a2a0d-0000-0000-0000-000000000000',
29
+ secret_token: '4g1cshg......rbs6'
30
+ }
31
+ client = Resizing::Client.new(options)
32
+
33
+ # upload image to resizing
34
+ file = File.open('sample.jpg', 'r')
35
+ response = client.post(file)
36
+ => {
37
+ "id"=>"a4ed2bf0-a4cf-44fa-9c82-b53e581cb469",
38
+ "project_id"=>"098a2a0d-0000-0000-0000-000000000000",
39
+ "content_type"=>"image/jpeg",
40
+ "latest_version_id"=>"LJY5bxBF7Ryxfr5kC1F.63W8bzp3pcUm",
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"
82
54
  ```
83
55
 
84
56
  ## Development
@@ -89,7 +61,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
89
61
 
90
62
  ## Contributing
91
63
 
92
- Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing-gem.
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jksy/resizing.
65
+
93
66
 
94
67
  ## License
95
68
 
@@ -1,19 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_storage/service'
4
-
5
3
  module Resizing
6
4
  module ActiveStorage
7
5
  module Service
8
6
  # ref.
9
7
  # https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/s3_service.rb
10
8
  #
11
- # rubocop:disable Metrics/ParameterLists
9
+ # rubocop:disable Lint/UnusedMethodArgument,Metrics/ParameterLists
12
10
  class ResizingService < ::ActiveStorage::Service
13
11
  # def initialize(bucket:, upload: {}, public: false, **options)
14
- # rubocop:disable Lint/MissingSuper
15
12
  def initialize; end
16
- # rubocop:enable Lint/MissingSuper
17
13
 
18
14
  def upload(_key, _io, checksum: nil, filename: nil, content_type: nil, disposition: nil, **)
19
15
  raise NotImplementedError, 'upload is not implemented'
@@ -55,7 +51,7 @@ module Resizing
55
51
  raise NotImplementedError, 'public_url is not implemented'
56
52
  end
57
53
  end
58
- # rubocop:enable Metrics/ParameterLists
54
+ # rubocop:enable Lint/UnusedMethodArgument,Metrics/ParameterLists
59
55
  end
60
56
  end
61
57
  end
@@ -3,7 +3,6 @@
3
3
  module Resizing
4
4
  module CarrierWave
5
5
  module Storage
6
- # rubocop:disable Metrics/ClassLength
7
6
  class File
8
7
  include ::CarrierWave::Utilities::Uri
9
8
 
@@ -29,42 +28,23 @@ module Resizing
29
28
  @content_type || file.try(:content_type)
30
29
  end
31
30
 
32
- # rubocop:disable Metrics/AbcSize
33
31
  def delete
34
- # Use the identifier from constructor if available, otherwise try to get from model
35
- if @public_id.present?
36
- # Already set from constructor or retrieve
37
- elsif model.respond_to?(:attribute_was)
38
- # Try to get the value before changes (for remove! scenario)
39
- column_value = model.attribute_was(serialization_column) || model.send(:read_attribute,
40
- serialization_column)
41
- @public_id = Resizing::PublicId.new(column_value)
42
- else
43
- column_value = model.send(:read_attribute, serialization_column)
44
- @public_id = Resizing::PublicId.new(column_value)
45
- end
46
-
47
- return if @public_id.empty?
32
+ @public_id = Resizing::PublicId.new(model.send :read_attribute, serialization_column)
33
+ return if @public_id.empty? # do nothing
48
34
 
49
35
  resp = client.delete(@public_id.image_id)
50
-
51
- # NOTE: 削除時のカラムクリアは以下の理由で必要:
52
- # - 画像更新時: 古い画像IDと新しい画像IDが異なるため、古い画像削除時に新しいIDを消さないようにする
53
- # - 明示的なremove!時: カラムをnilにする必要がある
54
- # - clear_column_if_current_imageは削除される画像IDと現在のカラム値を比較して判断
55
36
  if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found
56
- clear_column_if_current_image
37
+ model.send :write_attribute, serialization_column, nil unless model.destroyed?
57
38
  return
58
39
  end
59
40
 
60
41
  if @public_id.image_id == resp['id']
61
- clear_column_if_current_image
42
+ model.send :write_attribute, serialization_column, nil unless model.destroyed?
62
43
  return
63
44
  end
64
45
 
65
46
  raise APIError, "raise someone error:#{resp.inspect}"
66
47
  end
67
- # rubocop:enable Metrics/AbcSize
68
48
 
69
49
  def extension
70
50
  raise NotImplementedError, 'this method is do not used. maybe'
@@ -99,38 +79,34 @@ module Resizing
99
79
  end
100
80
 
101
81
  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
-
106
82
  @current_path = model.send :read_attribute, serialization_column
107
83
  end
108
84
  alias path current_path
109
85
 
110
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
111
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
112
86
  def store(new_file)
113
87
  if new_file.is_a?(self.class)
114
88
  # new_file.copy_to(path)
115
89
  raise NotImplementedError, 'new file is required duplicating'
116
90
  end
117
91
 
118
- @content_type ||= if new_file.respond_to? :content_type
119
- new_file.content_type
120
- else
121
- # guess content-type from extension
122
- MIME::Types.type_for(new_file.path).first.content_type
123
- end
92
+ if new_file.respond_to? :content_type
93
+ @content_type ||= new_file.content_type
94
+ else
95
+ # guess content-type from extension
96
+ @content_type ||= MIME::Types.type_for(new_file.path).first.content_type
97
+ end
124
98
 
125
99
  original_filename = new_file.try(:original_filename) || new_file.try(:filename) || new_file.try(:path)
126
- original_filename = ::File.basename(original_filename) if original_filename.present?
100
+ if original_filename.present?
101
+ original_filename = ::File.basename(original_filename)
102
+ end
127
103
 
128
104
  content = if new_file.respond_to?(:to_io)
129
105
  new_file.to_io.tap(&:rewind)
130
106
  elsif new_file.respond_to?(:read) && new_file.respond_to?(:rewind)
131
- # Pass the IO object itself, not the read result
132
- new_file.rewind
133
- new_file
107
+ new_file.read.tap do
108
+ new_file.rewind
109
+ end
134
110
  else
135
111
  new_file
136
112
  end
@@ -139,24 +115,19 @@ module Resizing
139
115
  @public_id = Resizing::PublicId.new(@response['public_id'])
140
116
  @content_type = @response['content_type']
141
117
 
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の実装を変更する必要がある
118
+ # force update column
119
+ # model_class
120
+ # .where(primary_key_name => model.send(primary_key_name))
121
+ # .update_all(serialization_column=>@public_id)
122
+
150
123
  # save new value to model class
151
124
  model.send :write_attribute, serialization_column, @public_id.to_s
152
125
 
153
126
  true
154
127
  end
155
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
156
- # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
157
128
 
158
- def name(_options = {})
159
- @public_id = PublicId.new(model.send(:read_attribute, serialization_column))
129
+ def name(options = {})
130
+ @public_id = PublicId.new(model.send :read_attribute, serialization_column)
160
131
  CGI.unescape(@public_id.filename)
161
132
  end
162
133
 
@@ -167,7 +138,6 @@ module Resizing
167
138
  def retrieve(identifier)
168
139
  @public_id = Resizing::PublicId.new(identifier)
169
140
  end
170
-
171
141
  private
172
142
 
173
143
  attr_reader :uploader
@@ -188,20 +158,6 @@ module Resizing
188
158
  @serialization_column ||= model.send(:_mounter, uploader.mounted_as).send(:serialization_column)
189
159
  end
190
160
 
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
-
205
161
  ##
206
162
  # client of Resizing
207
163
  def client
@@ -239,6 +195,7 @@ module Resizing
239
195
  parameters.count == 2 && parameters[1].include?(:options)
240
196
  end
241
197
 
198
+
242
199
  # def retrieve_from_cache!(identifier)
243
200
  # raise NotImplementedError,
244
201
  # "Need to implement #retrieve_from_cache! if you want to use #{self.class.name} as a cache storage."
@@ -254,7 +211,6 @@ module Resizing
254
211
  # "Need to implement #clean_cache! if you want to use #{self.class.name} as a cache storage."
255
212
  # end
256
213
  end
257
- # rubocop:enable Metrics/ClassLength
258
214
  end
259
215
  end
260
216
  end
@@ -18,13 +18,9 @@ module Resizing
18
18
  f
19
19
  end
20
20
 
21
- def retrieve!(identifier)
22
- return nil if identifier.blank?
23
-
24
- f = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
25
- f.retrieve(identifier)
26
- f
27
- end
21
+ # def retrieve!(identifier)
22
+ # super
23
+ # end
28
24
 
29
25
  def cache!(new_file)
30
26
  f = Resizing::CarrierWave::Storage::File.new(uploader)
@@ -4,7 +4,6 @@ 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
8
7
  module CarrierWave
9
8
  class Railtie < ::Rails::Railtie
10
9
  # Railtie skelton codes
@@ -29,25 +28,15 @@ module Resizing
29
28
  def initialize(*args)
30
29
  @requested_format = nil
31
30
  @default_format = nil
32
- @retrieved_identifier = nil
33
31
  super
34
32
  end
35
33
 
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
-
44
34
  def file
45
- file_identifier = @retrieved_identifier || identifier || read_column
46
-
47
- return nil if file_identifier.blank?
35
+ return if identifier.nil?
36
+ return @file unless defined? @file
48
37
 
49
38
  @file ||= Resizing::CarrierWave::Storage::File.new(self)
50
- @file.retrieve(file_identifier)
39
+ @file.retrieve(identifier)
51
40
  @file
52
41
  end
53
42
 
@@ -56,8 +45,7 @@ module Resizing
56
45
 
57
46
  transforms = args.map do |version|
58
47
  version = version.intern
59
- raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.key? version
60
-
48
+ raise "No version is found: #{version}, #{versions.keys} are exists." unless versions.has_key? version
61
49
  versions[version].transform_string
62
50
  end.compact
63
51
 
@@ -120,6 +108,11 @@ module Resizing
120
108
  read_column
121
109
  end
122
110
 
111
+ def store!
112
+ # DO NOTHING
113
+ super
114
+ end
115
+
123
116
  def serialization_column
124
117
  model.send(:_mounter, mounted_as).send(:serialization_column)
125
118
  end
@@ -157,7 +150,7 @@ module Resizing
157
150
 
158
151
  private
159
152
 
160
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
153
+ # rubocop:disable Metrics/AbcSize
161
154
  def transform_string_from(processor)
162
155
  action = processor.first
163
156
  value = processor.second
@@ -180,7 +173,6 @@ module Resizing
180
173
  "#{key}_#{value}"
181
174
  end.compact.join(',')
182
175
  end
183
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
176
+ # rubocop:enable Metrics/AbcSize
184
177
  end
185
- # rubocop:enable Metrics/ModuleLength
186
178
  end
@@ -24,7 +24,6 @@ module Resizing
24
24
  # }
25
25
  #
26
26
  #++
27
- # rubocop:disable Metrics/ClassLength
28
27
  class Client
29
28
  include Resizing::Constants
30
29
  include Resizing::Configurable
@@ -38,14 +37,13 @@ module Resizing
38
37
  raise NotImplementedError
39
38
  end
40
39
 
41
- def post(filename_or_io, options = {})
40
+ def post(file_or_binary, options = {})
42
41
  ensure_content_type(options)
43
- ensure_filename_or_io(filename_or_io)
44
- filename = gather_filename filename_or_io, options
42
+ filename = gather_filename file_or_binary, options
45
43
 
46
44
  url = build_post_url
47
45
  params = {
48
- image: Resizing.file_part_class.new(filename_or_io, options[:content_type], filename)
46
+ image: Faraday::Multipart::FilePart.new(file_or_binary, options[:content_type], filename)
49
47
  }
50
48
 
51
49
  response = handle_faraday_error do
@@ -54,17 +52,17 @@ module Resizing
54
52
  end
55
53
  end
56
54
 
57
- handle_create_response(response)
55
+ result = handle_create_response(response)
56
+ result
58
57
  end
59
58
 
60
- def put(image_id, filename_or_io, options)
59
+ def put(image_id, file_or_binary, options)
61
60
  ensure_content_type(options)
62
- ensure_filename_or_io(filename_or_io)
63
- filename = gather_filename filename_or_io, options
61
+ filename = gather_filename file_or_binary, options
64
62
 
65
63
  url = build_put_url(image_id)
66
64
  params = {
67
- image: Resizing.file_part_class.new(filename_or_io, options[:content_type], filename)
65
+ image: Faraday::Multipart::FilePart.new(file_or_binary, options[:content_type], filename)
68
66
  }
69
67
 
70
68
  response = handle_faraday_error do
@@ -73,7 +71,8 @@ module Resizing
73
71
  end
74
72
  end
75
73
 
76
- handle_create_response(response)
74
+ result = handle_create_response(response)
75
+ result
77
76
  end
78
77
 
79
78
  def delete(image_id)
@@ -85,7 +84,8 @@ module Resizing
85
84
  end
86
85
  end
87
86
 
88
- handle_delete_response(response)
87
+ result = handle_delete_response(response)
88
+ result
89
89
  end
90
90
 
91
91
  def metadata(image_id, options = {})
@@ -97,7 +97,8 @@ module Resizing
97
97
  end
98
98
  end
99
99
 
100
- handle_metadata_response(response, options)
100
+ result = handle_metadata_response(response, options)
101
+ result
101
102
  end
102
103
 
103
104
  private
@@ -110,8 +111,9 @@ module Resizing
110
111
  "#{config.image_host}/projects/#{config.project_id}/upload/images/"
111
112
  end
112
113
 
113
- def gather_filename(filename_or_io, options)
114
- options[:filename] || (filename_or_io.respond_to?(:path) ? File.basename(filename_or_io.path) : nil)
114
+ def gather_filename file_or_binary, options
115
+ filename = options[:filename]
116
+ filename ||= file_or_binary.respond_to?(:path) ? File.basename(file_or_binary.path) : nil
115
117
  end
116
118
 
117
119
  def build_put_url(image_id)
@@ -130,20 +132,8 @@ module Resizing
130
132
  raise ArgumentError, "need options[:content_type] for #{options.inspect}" unless options[:content_type]
131
133
  end
132
134
 
133
- def ensure_filename_or_io(filename_or_io)
134
- return if filename_or_io.is_a?(File)
135
-
136
- # Accept IO-like objects (StringIO, Tempfile, etc.)
137
- return if filename_or_io.respond_to?(:read) && filename_or_io.respond_to?(:rewind)
138
-
139
- return if filename_or_io.is_a?(String) && File.exist?(filename_or_io)
140
-
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})"
143
- end
144
-
145
135
  def handle_create_response(response)
146
- raise APIError, 'No response is returned' if response.nil?
136
+ raise APIError, "No response is returned" if response.nil?
147
137
 
148
138
  case response.status
149
139
  when HTTP_STATUS_OK, HTTP_STATUS_CREATED
@@ -154,7 +144,7 @@ module Resizing
154
144
  end
155
145
 
156
146
  def handle_delete_response(response)
157
- raise APIError, 'No response is returned' if response.nil?
147
+ raise APIError, "No response is returned" if response.nil?
158
148
 
159
149
  case response.status
160
150
  when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
@@ -167,30 +157,24 @@ module Resizing
167
157
  def handle_metadata_response(response, options = {})
168
158
  when_not_found = options[:when_not_found] || nil
169
159
 
170
- raise APIError, 'No response is returned' if response.nil?
160
+ raise APIError, "No response is returned" if response.nil?
171
161
 
172
162
  case response.status
173
- when HTTP_STATUS_OK
163
+ when HTTP_STATUS_OK, HTTP_STATUS_NOT_FOUND
174
164
  JSON.parse(response.body)
175
165
  when HTTP_STATUS_NOT_FOUND
176
166
  raise decode_error_from(response) if when_not_found == :raise
177
-
178
- JSON.parse(response.body)
167
+ nil
179
168
  else
180
169
  raise decode_error_from(response)
181
170
  end
182
171
  end
183
172
 
184
- def decode_error_from(response)
185
- result = begin
186
- JSON.parse(response.body)
187
- rescue StandardError
188
- {}
189
- end
173
+ def decode_error_from response
174
+ result = JSON.parse(response.body) rescue {}
190
175
  err = APIError.new(result['message'] || "invalid http status code #{response.status}")
191
176
  err.decoded_body = result
192
177
  err
193
178
  end
194
179
  end
195
- # rubocop:enable Metrics/ClassLength
196
180
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Resizing
4
4
  module Configurable
5
- def self.included(mod)
5
+ def self.included mod
6
6
  mod.send(:attr_reader, :config)
7
7
  end
8
8