Skip to content

Commit

Permalink
Merge pull request #10 from sealink/fetch
Browse files Browse the repository at this point in the history
Implements file fetching
  • Loading branch information
alxberardi committed Feb 2, 2016
2 parents 016a522 + 2698fdf commit b079742
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
6 changes: 5 additions & 1 deletion lib/pansophy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def self.push(bucket_name, remote_directory, local_directory, options = {})
Synchronizer.new(bucket_name, remote_directory, local_directory).push(options)
end

def self.fetch(bucket_name, path)
Remote::FetchFile.new(bucket_name, path).call
end

def self.read(bucket_name, path)
Remote::ReadFile.new(bucket_name, path).call
Remote::ReadFileBody.new(bucket_name, path).call
end

def self.head(bucket_name, path)
Expand Down
3 changes: 2 additions & 1 deletion lib/pansophy/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
require 'pansophy/remote/create_file'
require 'pansophy/remote/file'
require 'pansophy/remote/read_directory'
require 'pansophy/remote/read_file'
require 'pansophy/remote/fetch_file'
require 'pansophy/remote/read_file_body'
require 'pansophy/remote/read_file_head'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Pansophy
module Remote
class ReadFile
class FetchFile
include Adamantium::Flat

def initialize(bucket, path)
Expand All @@ -10,7 +10,7 @@ def initialize(bucket, path)

def call
fail ArgumentError, "#{@pathname} does not exist" if file.nil?
file.body
file
end

private
Expand Down
23 changes: 23 additions & 0 deletions lib/pansophy/remote/read_file_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Pansophy
module Remote
class ReadFileBody
include Adamantium::Flat

def initialize(bucket, path)
@bucket = bucket
@pathname = Pathname.new(path)
end

def call
file.body
end

private

def file
FetchFile.new(@bucket, @pathname).call
end
memoize :file
end
end
end
2 changes: 1 addition & 1 deletion lib/pansophy/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pansophy
VERSION = '0.3.0'
VERSION = '0.4.0'
end
45 changes: 31 additions & 14 deletions spec/remote/read_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
let(:path) { 'files/test.txt' }
let(:body) { 'Content' }

let(:read_file) { Pansophy::Remote::ReadFile.new(bucket_name, path) }
let(:read_file_body) { Pansophy::Remote::ReadFileBody.new(bucket_name, path) }

context 'when the bucket exists' do
before do
Expand All @@ -29,7 +29,7 @@
end

specify do
expect(read_file.call).to eq body
expect(read_file_body.call).to eq body
end

context 'when using the module level interface' do
Expand All @@ -38,36 +38,53 @@
end
end

context 'when reading the file head' do
shared_examples 'a file head' do
specify { expect(file_head.key).to eq path }
specify { expect(file_head.content_length).to eq body.length }
specify { expect(file_head.content_type).to be_nil }
specify { expect(file_head.etag).to match /\h{32}$/ }
specify { expect(file_head.last_modified).to be_within(1).of Time.now }
end
shared_examples 'a file head' do
specify { expect(file.key).to eq path }
specify { expect(file.content_length).to eq body.length }
specify { expect(file.content_type).to be_nil }
specify { expect(file.etag).to match /\h{32}$/ }
specify { expect(file.last_modified).to be_within(1).of Time.now }
end

context 'when reading the file head' do
let(:read_file_head) { Pansophy::Remote::ReadFileHead.new(bucket_name, path) }
subject(:file_head) { read_file_head.call }
subject(:file) { read_file_head.call }
it_behaves_like 'a file head'

context 'when using the module level interface' do
subject(:file_head) { Pansophy.head(bucket_name, path) }
subject(:file) { Pansophy.head(bucket_name, path) }
it_behaves_like 'a file head'
end
end

context 'when fetching the file' do
shared_examples 'a file' do
it_behaves_like 'a file head'
specify { expect(file.body).to eq body }
end

let(:fetch_file) { Pansophy::Remote::FetchFile.new(bucket_name, path) }
subject(:file) { fetch_file.call }
it_behaves_like 'a file'

context 'when using the module level interface' do
subject(:file) { Pansophy.fetch(bucket_name, path) }
it_behaves_like 'a file'
end
end
end

context 'when the file does not exist' do
specify do
expect { read_file.call }.to raise_error ArgumentError, "#{path} does not exist"
expect { read_file_body.call }.to raise_error ArgumentError, "#{path} does not exist"
end
end
end

context 'when the bucket exists' do
specify do
expect { read_file.call }.to raise_error ArgumentError, "Could not find bucket #{bucket_name}"
expect { read_file_body.call }
.to raise_error ArgumentError, "Could not find bucket #{bucket_name}"
end
end
end

0 comments on commit b079742

Please sign in to comment.