Skip to content

Commit 524e0cb

Browse files
committed
Refactor importer to make it easier to cache asset
My next commit will add in another task to the import, having one instance of ScratchAssetImporter per asset, will make easier to cache the asset and not download it twice
1 parent cfd59c6 commit 524e0cb

4 files changed

Lines changed: 29 additions & 30 deletions

File tree

lib/scratch_asset_importer.rb

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22

33
require 'ruby-progressbar'
44
require 'stringio'
5+
require 'aws-sdk-s3'
56

67
class ScratchAssetImporter
7-
def self.import(...)
8-
new(...).import
8+
class << self
9+
def import_all(asset_names, asset_base_url)
10+
bar = ProgressBar.create(format: '%t: |%B| %c of %C %E', total: asset_names.count) if show_progress?
11+
12+
asset_names.each do |asset_name|
13+
bar.increment if show_progress?
14+
new(asset_name, asset_base_url).import
15+
end
16+
end
17+
18+
private
19+
20+
def show_progress?
21+
!Rails.env.test?
22+
end
923
end
1024

11-
attr_reader :asset_base_url, :asset_names
25+
attr_reader :asset_base_url, :asset_name
1226

1327
ASSET_FETCHING_DELAY = 0.2
1428

15-
def initialize(asset_names, asset_base_url)
16-
@asset_names = asset_names
29+
def initialize(asset_name, asset_base_url)
30+
@asset_name = asset_name
1731
@asset_base_url = asset_base_url
1832
end
1933

2034
def import
21-
bar = ProgressBar.create(format: '%t: |%B| %c of %C %E', total: asset_names.count) if show_progress?
22-
23-
asset_names.each do |asset_name|
24-
bar.increment if show_progress?
25-
import_asset(asset_name)
26-
end
27-
end
28-
29-
private
30-
31-
def import_asset(asset_name)
3235
return if ScratchAsset.global_assets.exists?(filename: asset_name)
3336

3437
sleep(ASSET_FETCHING_DELAY)
@@ -45,8 +48,4 @@ def connection
4548
faraday.response :raise_error
4649
end
4750
end
48-
49-
def show_progress?
50-
!Rails.env.test?
51-
end
5251
end

lib/scratch_config_importer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def import
1616
config = connection.get.body
1717
asset_config = JSON.parse(config, symbolize_names: true)
1818
asset_names = extract_asset_names(asset_config)
19-
ScratchAssetImporter.import(asset_names, asset_base_url)
19+
ScratchAssetImporter.import_all(asset_names, asset_base_url)
2020
end
2121

2222
def connection

spec/lib/scratch_asset_importer_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
require 'scratch_asset_importer'
55

66
RSpec.describe ScratchAssetImporter do
7-
describe '.import' do
7+
describe '.import_all' do
88
it 'imports assets from the config' do
99
image = Rails.root.join('spec/fixtures/files/test_image_1.png').read
1010
stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image)
1111

12-
described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/')
12+
described_class.import_all(['123abc.png'], 'https://example.net/internalapi/asset/')
1313

1414
scratch_asset = ScratchAsset.find_by(filename: '123abc.png')
1515
expect(scratch_asset).to be_present
@@ -21,7 +21,7 @@
2121
create(:scratch_asset, :with_file, filename: '123abc.png')
2222

2323
expect do
24-
described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/')
24+
described_class.import_all(['123abc.png'], 'https://example.net/internalapi/asset/')
2525
end.not_to change(ScratchAsset, :count)
2626
end
2727

@@ -31,7 +31,7 @@
3131
stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image)
3232
stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image)
3333

34-
described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
34+
described_class.import_all(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
3535
expect(ScratchAsset.find_by(filename: '123abc.png')).to be_present
3636
expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present
3737
end
@@ -45,7 +45,7 @@
4545
stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 200, body: image)
4646

4747
expect do
48-
described_class.import(['123abc.png'], 'https://example.net/internalapi/asset/')
48+
described_class.import_all(['123abc.png'], 'https://example.net/internalapi/asset/')
4949
end.to change { ScratchAsset.global_assets.where(filename: '123abc.png').count }.by(1)
5050
end
5151

@@ -55,7 +55,7 @@
5555
stub_request(:get, 'https://example.net/internalapi/asset/123abc.png/get/').to_return(status: 500, body: 'error')
5656
stub_request(:get, 'https://example.net/internalapi/asset/456xyz.png/get/').to_return(status: 200, body: image)
5757

58-
described_class.import(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
58+
described_class.import_all(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
5959
expect(ScratchAsset.find_by(filename: '123abc.png')).not_to be_present
6060
expect(ScratchAsset.find_by(filename: '456xyz.png')).to be_present
6161
end

spec/lib/scratch_config_importer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
RSpec.describe ScratchConfigImporter do
77
before do
8-
allow(ScratchAssetImporter).to receive(:import)
8+
allow(ScratchAssetImporter).to receive(:import_all)
99
end
1010

1111
describe '.import' do
@@ -15,7 +15,7 @@
1515

1616
described_class.import('https://example.com/config/backdrops.json', 'https://example.net/internalapi/asset/')
1717

18-
expect(ScratchAssetImporter).to have_received(:import).with(['123abc.png'], 'https://example.net/internalapi/asset/')
18+
expect(ScratchAssetImporter).to have_received(:import_all).with(['123abc.png'], 'https://example.net/internalapi/asset/')
1919
end
2020

2121
it 'handles assets nested under sounds and costumes' do
@@ -33,7 +33,7 @@
3333

3434
described_class.import('https://example.com/config/sprites.json', 'https://example.net/internalapi/asset/')
3535

36-
expect(ScratchAssetImporter).to have_received(:import).with(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
36+
expect(ScratchAssetImporter).to have_received(:import_all).with(['123abc.png', '456xyz.png'], 'https://example.net/internalapi/asset/')
3737
end
3838
end
3939
end

0 commit comments

Comments
 (0)