Commit 49284ad
Changed files (5)
app
models
spec
models
app/jobs/download_from_drive_job.rb
@@ -2,7 +2,8 @@ class DownloadFromDriveJob < ActiveJob::Base
queue_as :default
def perform(user, params)
- backup_file = user.google_drive.download(params)
- backup_file.process_later(Program.stronglifts)
+ user.google_drive.download(params) do |backup_file|
+ backup_file.process_later(Program.stronglifts)
+ end
end
end
app/models/google_drive.rb
@@ -9,31 +9,17 @@ class GoogleDrive
def download(params)
Dir.mktmpdir do |dir|
download_path = File.join(dir, params[:data][:title])
- execute(create_command(
- params[:data][:downloadUrl].strip,
- download_path,
- params[:accessToken]
- ))
- return BackupFile.new(user, File.new(download_path))
+ download_url = params[:data][:downloadUrl].strip
+ access_token = params[:accessToken]
+ curl = Shell.new('curl')
+ curl << "'#{download_url}'"
+ curl << "-o '#{download_path}'"
+ curl << "-H 'Authorization: Bearer #{access_token}'"
+ curl << "-H 'Referer: #{@referrer_domain}/dashboard'"
+ curl << "-H 'Origin: #{@referrer_domain}'"
+ curl << "--compressed"
+ curl.run
+ yield BackupFile.new(user, File.new(download_path))
end
end
-
- private
-
- def create_command(download_url, download_path, access_token)
- command = <<-COMMAND
-curl '#{download_url}' \
--o '#{download_path}' \
--H 'Authorization: Bearer #{access_token}' \
--H 'Referer: #{@referrer_domain}/dashboard' \
--H 'Origin: #{@referrer_domain}' \
--H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36' \
---compressed
- COMMAND
- command
- end
-
- def execute(command)
- `#{command}`
- end
end
app/models/shell.rb
@@ -0,0 +1,22 @@
+class Shell
+ def initialize(program)
+ @program = program
+ @options = []
+ end
+
+ def <<(option)
+ @options.push(option)
+ end
+
+ def run
+ `#{build_command}`
+ end
+
+ private
+
+ def build_command
+ result = "#{@program} #{@options.join(" ")}"
+ puts result.inspect
+ result
+ end
+end
spec/jobs/download_from_drive_job_spec.rb
@@ -9,7 +9,7 @@ RSpec.describe DownloadFromDriveJob, type: :job do
it "downloads the file for further processing" do
allow(user).to receive(:google_drive).and_return(drive)
- allow(drive).to receive(:download).with(params).and_return(backup_file)
+ allow(drive).to receive(:download).with(params).and_yield(backup_file)
subject.perform(user, params)
expect(backup_file).to have_received(:process_later).with(Program.stronglifts)
end
spec/models/google_drive_spec.rb
@@ -20,7 +20,11 @@ describe GoogleDrive do
let(:access_token) { FFaker::Internet.user_name }
it "downloads the specified google drive file" do
- result = subject.download(params)
+ result = nil
+ subject.download(params) do |backup_file|
+ result = backup_file
+ end
+ expect(result).to_not be_nil
expect(result.user).to eql(user)
expect(result.backup_file.path).to end_with(filename)
end