Skip to content

Commit

Permalink
Use standardrb code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
julik committed Mar 20, 2024
1 parent a283c11 commit 83b73e7
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # 'bundle install' and cache
- name: Run tests
- name: Run tests and standard
run: bundle exec rake
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in zipline.gemspec
gemspec
12 changes: 4 additions & 8 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "standard/rake"
require "rspec/core/rake_task"

begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

RSpec::Core::RakeTask.new(:spec)

task default: :spec
rescue LoadError
# no rspec available
end
task default: [:spec, :standard]
10 changes: 5 additions & 5 deletions lib/zipline.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'content_disposition'
require 'zip_kit'
require 'zipline/version'
require 'zipline/zip_handler'
require "content_disposition"
require "zip_kit"
require "zipline/version"
require "zipline/zip_handler"

# class MyController < ApplicationController
# include Zipline
Expand All @@ -17,7 +17,7 @@ def self.included(into_controller)
super
end

def zipline(files, zipname = 'zipline.zip', **kwargs_for_zip_kit_stream)
def zipline(files, zipname = "zipline.zip", **kwargs_for_zip_kit_stream)
zip_kit_stream(filename: zipname, **kwargs_for_zip_kit_stream) do |zip_kit_streamer|
handler = Zipline::ZipHandler.new(zip_kit_streamer, logger)
files.each do |file, name, options = {}|
Expand Down
14 changes: 9 additions & 5 deletions lib/zipline/zip_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def handle_file(file, name, options)
# Rack bodies, it can be helpful to print the error to the Rails log at least
error_message = "zipline: an exception (#{e.inspect}) was raised when serving the ZIP body."
error_message += " The error occurred when handling file #{name.inspect}"
@logger.error(error_message) if @logger
@logger&.error(error_message)
raise
end

Expand Down Expand Up @@ -51,7 +51,7 @@ def normalize(file)
elsif is_url?(file)
{url: file}
else
raise(ArgumentError, 'Bad File/Stream')
raise(ArgumentError, "Bad File/Stream")
end
end

Expand All @@ -71,7 +71,7 @@ def write_file(file, name, options)
elsif file[:blob]
file[:blob].download { |chunk| writer_for_file << chunk }
else
raise(ArgumentError, 'Bad File/Stream')
raise(ArgumentError, "Bad File/Stream")
end
end
end
Expand All @@ -91,8 +91,12 @@ def is_active_storage_one?(file)
end

def is_url?(url)
url = URI.parse(url) rescue false
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
url = begin
URI.parse(url)
rescue
false
end
url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS)
end
end
end
96 changes: 53 additions & 43 deletions spec/lib/zipline/zip_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
require 'spec_helper'
require 'tempfile'
require "spec_helper"
require "tempfile"

module ActiveStorage
class Attached
class One < Attached
end
end

class Attachment; end

class Blob; end

class Filename
def initialize(name)
@name = name
end

def to_s
@name
end
Expand All @@ -20,38 +24,44 @@ def to_s

describe Zipline::ZipHandler do
before { Fog.mock! }
let(:file_attributes){ {
key: 'fog_file_tests',
body: 'some body',
public: true
}}
let(:directory_attributes){{
key: 'fog_directory'
}}
let(:storage_attributes){{
aws_access_key_id: 'fake_access_key_id',
aws_secret_access_key: 'fake_secret_access_key',
provider: 'AWS'
}}
let(:storage){ Fog::Storage.new(storage_attributes)}
let(:directory){ storage.directories.create(directory_attributes) }
let(:file){ directory.files.create(file_attributes) }

describe '.normalize' do
let(:handler){ Zipline::ZipHandler.new(_streamer = double(), _logger = nil)}
let(:file_attributes) {
{
key: "fog_file_tests",
body: "some body",
public: true
}
}
let(:directory_attributes) {
{
key: "fog_directory"
}
}
let(:storage_attributes) {
{
aws_access_key_id: "fake_access_key_id",
aws_secret_access_key: "fake_secret_access_key",
provider: "AWS"
}
}
let(:storage) { Fog::Storage.new(storage_attributes) }
let(:directory) { storage.directories.create(directory_attributes) }
let(:file) { directory.files.create(file_attributes) }

describe ".normalize" do
let(:handler) { Zipline::ZipHandler.new(_streamer = double, _logger = nil) }
context "CarrierWave" do
context "Remote" do
let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) }
let(:file) { CarrierWave::Storage::Fog::File.new(nil, nil, nil) }
it "extracts the url" do
allow(file).to receive(:url).and_return('fakeurl')
allow(file).to receive(:url).and_return("fakeurl")
expect(File).not_to receive(:open)
expect(handler.normalize(file)).to eq({url: 'fakeurl'})
expect(handler.normalize(file)).to eq({url: "fakeurl"})
end
end
context "Local" do
let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) }
let(:file) { CarrierWave::SanitizedFile.new(Tempfile.new("t")) }
it "creates a File" do
allow(file).to receive(:path).and_return('spec/fakefile.txt')
allow(file).to receive(:path).and_return("spec/fakefile.txt")
normalized = handler.normalize(file)
expect(normalized.keys).to include(:file)
expect(normalized[:file]).to be_a File
Expand All @@ -61,20 +71,20 @@ def to_s
let(:uploader) { Class.new(CarrierWave::Uploader::Base).new }

context "Remote" do
let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) }
let(:file) { CarrierWave::Storage::Fog::File.new(nil, nil, nil) }
it "extracts the url" do
allow(uploader).to receive(:file).and_return(file)
allow(file).to receive(:url).and_return('fakeurl')
allow(file).to receive(:url).and_return("fakeurl")
expect(File).not_to receive(:open)
expect(handler.normalize(uploader)).to eq({url: 'fakeurl'})
expect(handler.normalize(uploader)).to eq({url: "fakeurl"})
end
end

context "Local" do
let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) }
let(:file) { CarrierWave::SanitizedFile.new(Tempfile.new("t")) }
it "creates a File" do
allow(uploader).to receive(:file).and_return(file)
allow(file).to receive(:path).and_return('spec/fakefile.txt')
allow(file).to receive(:path).and_return("spec/fakefile.txt")
normalized = handler.normalize(uploader)
expect(normalized.keys).to include(:file)
expect(normalized[:file]).to be_a File
Expand All @@ -84,20 +94,20 @@ def to_s
end
context "Paperclip" do
context "Local" do
let(:file){ Paperclip::Attachment.new(:name, :instance) }
let(:file) { Paperclip::Attachment.new(:name, :instance) }
it "creates a File" do
allow(file).to receive(:path).and_return('spec/fakefile.txt')
allow(file).to receive(:path).and_return("spec/fakefile.txt")
normalized = handler.normalize(file)
expect(normalized.keys).to include(:file)
expect(normalized[:file]).to be_a File
end
end
context "Remote" do
let(:file){ Paperclip::Attachment.new(:name, :instance, storage: :s3) }
let(:file) { Paperclip::Attachment.new(:name, :instance, storage: :s3) }
it "creates a URL" do
allow(file).to receive(:expiring_url).and_return('fakeurl')
allow(file).to receive(:expiring_url).and_return("fakeurl")
expect(File).to_not receive(:open)
expect(handler.normalize(file)).to include(url: 'fakeurl')
expect(handler.normalize(file)).to include(url: "fakeurl")
end
end
end
Expand Down Expand Up @@ -154,34 +164,34 @@ def create_attachment

def create_blob
blob = ActiveStorage::Blob.new
allow(blob).to receive(:service_url).and_return('fakeurl')
allow(blob).to receive(:service_url).and_return("fakeurl")
filename = create_filename
allow(blob).to receive(:filename).and_return(filename)
blob
end

def create_filename
# Rails wraps Blob#filname in this class since Rails 5.2
ActiveStorage::Filename.new('test')
ActiveStorage::Filename.new("test")
end
end
context "Fog" do
it "extracts url" do
allow(file).to receive(:url).and_return('fakeurl')
allow(file).to receive(:url).and_return("fakeurl")
expect(File).not_to receive(:open)
expect(handler.normalize(file)).to eq(url: 'fakeurl')
expect(handler.normalize(file)).to eq(url: "fakeurl")
end
end
context "IOStream" do
let(:file){ StringIO.new('passthrough')}
let(:file) { StringIO.new("passthrough") }
it "passes through" do
expect(handler.normalize(file)).to eq(file: file)
end
end
context "invalid" do
let(:file){ Thread.new{} }
let(:file) { Thread.new {} }
it "raises error" do
expect{handler.normalize(file)}.to raise_error(ArgumentError)
expect { handler.normalize(file) }.to raise_error(ArgumentError)
end
end
end
Expand Down
28 changes: 16 additions & 12 deletions spec/lib/zipline/zipline_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'
require 'action_controller'
require "spec_helper"
require "action_controller"

describe Zipline do
before do
Expand All @@ -14,7 +14,7 @@ def download_zip
[StringIO.new("File content goes here"), "one.txt"],
[StringIO.new("Some other content goes here"), "two.txt"]
]
zipline(files, 'myfiles.zip', auto_rename_duplicate_filenames: false)
zipline(files, "myfiles.zip", auto_rename_duplicate_filenames: false)
end

class FailingIO < StringIO
Expand All @@ -28,46 +28,50 @@ def download_zip_with_error_during_streaming
[StringIO.new("File content goes here"), "one.txt"],
[FailingIO.new("This will fail half-way"), "two.txt"]
]
zipline(files, 'myfiles.zip', auto_rename_duplicate_filenames: false)
zipline(files, "myfiles.zip", auto_rename_duplicate_filenames: false)
end
end

it 'passes keyword parameters to ZipKit::OutputEnumerator' do
it "passes keyword parameters to ZipKit::OutputEnumerator" do
fake_rack_env = {
"HTTP_VERSION" => "HTTP/1.0",
"REQUEST_METHOD" => "GET",
"SCRIPT_NAME" => "",
"PATH_INFO" => "/download",
"QUERY_STRING" => "",
"SERVER_NAME" => "host.example",
"rack.input" => StringIO.new,
"rack.input" => StringIO.new
}
expect(ZipKit::OutputEnumerator).to receive(:new).with(auto_rename_duplicate_filenames: false).and_call_original

status, headers, body = FakeController.action(:download_zip).call(fake_rack_env)

expect(headers['Content-Disposition']).to eq("attachment; filename=\"myfiles.zip\"; filename*=UTF-8''myfiles.zip")
expect(status).to eq(200)
expect(headers["Content-Disposition"]).to eq("attachment; filename=\"myfiles.zip\"; filename*=UTF-8''myfiles.zip")
expect {
body.each {}
}.not_to raise_error
end

it 'sends the exception raised in the streaming body to the Rails logger' do
it "sends the exception raised in the streaming body to the Rails logger" do
fake_rack_env = {
"HTTP_VERSION" => "HTTP/1.0",
"REQUEST_METHOD" => "GET",
"SCRIPT_NAME" => "",
"PATH_INFO" => "/download",
"QUERY_STRING" => "",
"SERVER_NAME" => "host.example",
"rack.input" => StringIO.new,
"rack.input" => StringIO.new
}
fake_logger = double()
fake_logger = double
allow(fake_logger).to receive(:warn)
expect(fake_logger).to receive(:error).with(a_string_matching(/when serving the ZIP/))

FakeController.logger = fake_logger

expect {
status, headers, body = FakeController.action(:download_zip_with_error_during_streaming).call(fake_rack_env)
body.each { }
_status, _headers, body = FakeController.action(:download_zip_with_error_during_streaming).call(fake_rack_env)
body.each {}
}.to raise_error(/Something wonky/)
end
end
28 changes: 14 additions & 14 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
require 'rspec'
require 'active_support'
require 'active_support/core_ext'
require 'action_dispatch'
require "rspec"
require "active_support"
require "active_support/core_ext"
require "action_dispatch"

require 'zipline'
require 'paperclip'
require 'fog-aws'
require 'carrierwave'
require "zipline"
require "paperclip"
require "fog-aws"
require "carrierwave"

Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].sort.each { |f| require f }
Dir["#{File.expand_path("..", __FILE__)}/support/**/*.rb"].sort.each { |f| require f }

CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_provider = "fog/aws"
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: 'dummy',
aws_secret_access_key: 'data',
region: 'us-west-2',
provider: "AWS",
aws_access_key_id: "dummy",
aws_secret_access_key: "data",
region: "us-west-2"
}
end

Expand Down
Loading

0 comments on commit 83b73e7

Please sign in to comment.