diff --git a/lib/pansophy.rb b/lib/pansophy.rb index b32d4db..6b58b0e 100644 --- a/lib/pansophy.rb +++ b/lib/pansophy.rb @@ -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) diff --git a/lib/pansophy/remote.rb b/lib/pansophy/remote.rb index f8d6ff1..7f70f18 100644 --- a/lib/pansophy/remote.rb +++ b/lib/pansophy/remote.rb @@ -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' diff --git a/lib/pansophy/remote/read_file.rb b/lib/pansophy/remote/fetch_file.rb similarity index 93% rename from lib/pansophy/remote/read_file.rb rename to lib/pansophy/remote/fetch_file.rb index 59c4f9d..bf812bf 100644 --- a/lib/pansophy/remote/read_file.rb +++ b/lib/pansophy/remote/fetch_file.rb @@ -1,6 +1,6 @@ module Pansophy module Remote - class ReadFile + class FetchFile include Adamantium::Flat def initialize(bucket, path) @@ -10,7 +10,7 @@ def initialize(bucket, path) def call fail ArgumentError, "#{@pathname} does not exist" if file.nil? - file.body + file end private diff --git a/lib/pansophy/remote/read_file_body.rb b/lib/pansophy/remote/read_file_body.rb new file mode 100644 index 0000000..6a5a920 --- /dev/null +++ b/lib/pansophy/remote/read_file_body.rb @@ -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 diff --git a/lib/pansophy/version.rb b/lib/pansophy/version.rb index dbd4f48..667ed60 100644 --- a/lib/pansophy/version.rb +++ b/lib/pansophy/version.rb @@ -1,3 +1,3 @@ module Pansophy - VERSION = '0.3.0' + VERSION = '0.4.0' end diff --git a/spec/remote/read_file_spec.rb b/spec/remote/read_file_spec.rb index 45e704d..24368ca 100644 --- a/spec/remote/read_file_spec.rb +++ b/spec/remote/read_file_spec.rb @@ -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 @@ -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 @@ -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