Skip to content

Commit

Permalink
Add S3 uploading feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchlloyd committed Feb 10, 2013
1 parent 1ab6344 commit a335d1d
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 6 deletions.
13 changes: 10 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ PATH
remote: .
specs:
podcast_press (0.0.1)
aws-s3 (~> 0.6.3)
commander (~> 4.1.2)
taglib-ruby (~> 0.5.0)

GEM
remote: https://rubygems.org/
specs:
ansi (1.4.3)
aws-s3 (0.6.3)
builder
mime-types
xml-simple
builder (3.1.4)
columnize (0.3.1)
commander (4.1.2)
commander (4.1.3)
highline (~> 1.6.11)
debugger (1.1.3)
columnize (>= 0.3.1)
Expand All @@ -21,7 +26,8 @@ GEM
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.1.2)
hashie (1.2.0)
highline (1.6.12)
highline (1.6.15)
mime-types (1.21)
minitest (4.6.0)
minitest-reporters (0.14.7)
ansi
Expand All @@ -31,7 +37,8 @@ GEM
powerbar (1.0.11)
ansi (~> 1.4.0)
hashie (>= 1.1.0)
taglib-ruby (0.5.0)
taglib-ruby (0.5.2)
xml-simple (1.1.2)

PLATFORMS
ruby
Expand Down
4 changes: 3 additions & 1 deletion lib/podcast_press.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'podcast_press/audio_file'
require 'podcast_press/uploader'
require 'podcast_press/config'

module PodcastPress
Expand All @@ -7,6 +8,7 @@ def self.press!(filename, params={})
audio_file = AudioFile.new(filename)
audio_file.tag!(params)
audio_file.rename!(params[:filename])
audio_file.upload!(params[:s3_bucket])
return audio_file
end

Expand All @@ -19,4 +21,4 @@ def self.config(&block)
@configuration = Config.new
yield @configuration
end
end
end
7 changes: 6 additions & 1 deletion lib/podcast_press/audio_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def rename!(new_filename)
File.rename(@filename, File.join(dir, new_filename))
end

def upload!(s3_bucket)
return unless s3_bucket
Uploader.new(s3_bucket).upload(@filename)
end

def episode_number
@params.episode_number.to_i.to_s
end
Expand All @@ -62,4 +67,4 @@ def runtime
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/podcast_press/uploader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'aws/s3'

class Uploader
attr_accessor :bucket_name

def initialize(bucket_name)
self.bucket_name = bucket_name

AWS::S3::Base.establish_connection!(
access_key_id: ENV['AMAZON_ACCESS_KEY_ID'],
secret_access_key: ENV['AMAZON_SECRET_ACCESS_KEY']
)
end

def upload(file_path)
AWS::S3::S3Object.store(
File.basename(file_path),
open(file_path),
bucket_name,
access: :public_read
)
end
end
1 change: 1 addition & 0 deletions podcast_press.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |gem|

gem.add_dependency "taglib-ruby", "~> 0.5.0"
gem.add_dependency "commander", "~> 4.1.2"
gem.add_dependency "aws-s3", "~> 0.6.3"

gem.add_development_dependency "debugger"
gem.add_development_dependency "minitest-reporters"
Expand Down
15 changes: 15 additions & 0 deletions specs/s3_mocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AWS::S3::S3Object
cattr_accessor :store_call_args

def self.store(*args)
self.store_call_args = args
end
end

class AWS::S3::Base
cattr_accessor :establish_connection_call_args

def self.establish_connection!(*args)
self.establish_connection_call_args = args
end
end
2 changes: 1 addition & 1 deletion specs/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def file_has_artwork_assertion(audio_filename, pic_filename)
tag.picture.length.must_equal(expected_data.length)
end
end
end
end
37 changes: 37 additions & 0 deletions specs/uploading_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative './spec_setup'
require_relative '../lib/podcast_press'
require_relative './s3_mocks'

describe PodcastPress do
before do
@file = File.new(FILENAME, 'w')
end

after do
File.delete(@file.path)
end

describe "calling #press! with s3 upload" do
before do
ENV['AMAZON_ACCESS_KEY_ID'] = 'abc'
ENV['AMAZON_SECRET_ACCESS_KEY'] = '123'
PodcastPress.press!(@file.path, s3_bucket: 'test_bucket')
end

it "makes a connection to s3" do
AWS::S3::S3Object.establish_connection_call_args.must_equal([{
access_key_id: 'abc',
secret_access_key: '123',
}])
end

it "uploads a file to S3" do
args = AWS::S3::S3Object.store_call_args
args[0].must_equal 'test_file.mp3'
args[1].must_be_kind_of File
args[2].must_equal 'test_bucket'
args[3].must_equal({ access: :public_read })
end
end
end

0 comments on commit a335d1d

Please sign in to comment.