storage 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -2
- data/lib/storage/strategies/s3.rb +0 -3
- data/lib/storage/version.rb +1 -1
- data/spec/storage/strategies/s3_spec.rb +26 -51
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: debdbb389e3c318ff68a502dc87ac52fbd047894
         | 
| 4 | 
            +
              data.tar.gz: 1857d2fa6a260d0862400d519d2ac0c34c4f1a51
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 8a1e73f719687b06f538a307b7caaa06ee3f598d44f0b2baa2e6f4c4233281e91235d6d426f2e90bf45479af58ee047f815e5eba82e30e7b5c314e904c240cce
         | 
| 7 | 
            +
              data.tar.gz: 8338bc57f95931f7e6db01c5ae8ba0c8ee9d7339b38e0bb57fde7051afb2a4f68e8cc5554228d2b4b72f800fde66f0025e20cfc4b36846cfe3e9f74cf084267e
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -34,11 +34,15 @@ Storage.store "some/file.rb", name: "file.rb", bucket: "sample" | |
| 34 34 | 
             
            Storage.store File.open("some/file.rb"), name: "file.rb", bucket: "sample", public: true
         | 
| 35 35 |  | 
| 36 36 | 
             
            # Retrieve the public url for that file
         | 
| 37 | 
            -
            Storage.get "file.rb"
         | 
| 37 | 
            +
            Storage.get "file.rb", bucket: "sample"
         | 
| 38 38 | 
             
            #=> http://s3.amazon.com/sample-files/file.rb
         | 
| 39 39 |  | 
| 40 | 
            +
            # Retrieve the public url for a private file,
         | 
| 41 | 
            +
            # setting expiration to 5 minutes (300 seconds).
         | 
| 42 | 
            +
            Storage.get "private.rb", bucket: "sample", expires: Time.now.to_i + 300
         | 
| 43 | 
            +
             | 
| 40 44 | 
             
            # Remove a file.
         | 
| 41 | 
            -
            Storage.remove "file.rb"
         | 
| 45 | 
            +
            Storage.remove "file.rb", bucket: "sample"
         | 
| 42 46 | 
             
            ```
         | 
| 43 47 |  | 
| 44 48 | 
             
            ### FileSystem
         | 
| @@ -32,9 +32,6 @@ module Storage | |
| 32 32 | 
             
                  end
         | 
| 33 33 |  | 
| 34 34 | 
             
                  def store(file, options = {})
         | 
| 35 | 
            -
                    object = find_object(file, options) rescue nil
         | 
| 36 | 
            -
                    fail FileAlreadyExistsError if object
         | 
| 37 | 
            -
             | 
| 38 35 | 
             
                    bucket = find_bucket_or_create(options.fetch(:bucket))
         | 
| 39 36 | 
             
                    file = File.open(file, "rb") unless file.respond_to?(:read) && !file.kind_of?(Pathname)
         | 
| 40 37 |  | 
    
        data/lib/storage/version.rb
    CHANGED
    
    
| @@ -41,31 +41,27 @@ describe Storage::Strategies::S3 do | |
| 41 41 |  | 
| 42 42 | 
             
                it "should save a file using file handler" do
         | 
| 43 43 | 
             
                  handler = File.open(source)
         | 
| 44 | 
            -
             | 
| 45 | 
            -
                  setup_expectations
         | 
| 44 | 
            +
                  setup_create_object
         | 
| 46 45 |  | 
| 47 46 | 
             
                  Storage.store(handler, name: "lorem.txt", bucket: "files")
         | 
| 48 47 | 
             
                end
         | 
| 49 48 |  | 
| 50 49 | 
             
                it "should save a file using a path" do
         | 
| 51 | 
            -
                   | 
| 52 | 
            -
             | 
| 50 | 
            +
                  setup_create_object
         | 
| 53 51 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files")
         | 
| 54 52 | 
             
                end
         | 
| 55 53 |  | 
| 56 54 | 
             
                it "should remove an existing file" do
         | 
| 57 55 | 
             
                  object = double("object")
         | 
| 58 56 |  | 
| 59 | 
            -
                   | 
| 60 | 
            -
                  setup_object(object)
         | 
| 57 | 
            +
                  setup_get_object object: object
         | 
| 61 58 | 
             
                  expect(object).to receive(:destroy).and_return(true)
         | 
| 62 59 |  | 
| 63 60 | 
             
                  expect(Storage.remove("lorem.txt", bucket: "files")).to be_truthy
         | 
| 64 61 | 
             
                end
         | 
| 65 62 |  | 
| 66 63 | 
             
                it "should raise when trying to removing an nonexisting file" do
         | 
| 67 | 
            -
                   | 
| 68 | 
            -
                  setup_get(nil)
         | 
| 64 | 
            +
                  setup_get_object object: nil
         | 
| 69 65 |  | 
| 70 66 | 
             
                  expect {
         | 
| 71 67 | 
             
                    Storage.remove("lorem.txt", bucket: "files")
         | 
| @@ -75,8 +71,7 @@ describe Storage::Strategies::S3 do | |
| 75 71 | 
             
                it "should retrieve an existing file (public url)" do
         | 
| 76 72 | 
             
                  object = double("object", public_url: "PUBLIC_URL")
         | 
| 77 73 |  | 
| 78 | 
            -
                   | 
| 79 | 
            -
                  setup_object(object)
         | 
| 74 | 
            +
                  setup_get_object object: object
         | 
| 80 75 |  | 
| 81 76 | 
             
                  expect(Storage.get("lorem.txt", bucket: "files")).to eq("PUBLIC_URL")
         | 
| 82 77 | 
             
                end
         | 
| @@ -85,15 +80,13 @@ describe Storage::Strategies::S3 do | |
| 85 80 | 
             
                  object = double("object", public_url: nil)
         | 
| 86 81 |  | 
| 87 82 | 
             
                  expect(object).to receive_message_chain("url").with(Time.now.to_i + 3600).and_return("PRIVATE_URL")
         | 
| 88 | 
            -
                   | 
| 89 | 
            -
                  setup_object(object)
         | 
| 83 | 
            +
                  setup_get_object object: object
         | 
| 90 84 |  | 
| 91 | 
            -
                  expect(Storage.get("lorem.txt", : | 
| 85 | 
            +
                  expect(Storage.get("lorem.txt", bucket: "files")).to eq("PRIVATE_URL")
         | 
| 92 86 | 
             
                end
         | 
| 93 87 |  | 
| 94 88 | 
             
                it "should raise when trying to retrieve an missing file" do
         | 
| 95 | 
            -
                   | 
| 96 | 
            -
                  setup_get(nil)
         | 
| 89 | 
            +
                  setup_get_object object: nil
         | 
| 97 90 |  | 
| 98 91 | 
             
                  expect {
         | 
| 99 92 | 
             
                    Storage.get("lorem.txt", bucket: "files")
         | 
| @@ -101,80 +94,62 @@ describe Storage::Strategies::S3 do | |
| 101 94 | 
             
                end
         | 
| 102 95 |  | 
| 103 96 | 
             
                it "should raise when trying to retrieve an missing bucket" do
         | 
| 104 | 
            -
                   | 
| 97 | 
            +
                  setup_get_bucket bucket: nil
         | 
| 105 98 |  | 
| 106 99 | 
             
                  expect {
         | 
| 107 100 | 
             
                    Storage.get("lorem.txt", bucket: "files")
         | 
| 108 101 | 
             
                  }.to raise_error(Storage::MissingFileError)
         | 
| 109 102 | 
             
                end
         | 
| 110 103 |  | 
| 111 | 
            -
                it "should create a bucket  | 
| 112 | 
            -
                  setup_object
         | 
| 113 | 
            -
                  setup_bucket(nil)
         | 
| 114 | 
            -
             | 
| 115 | 
            -
                  expect(connection).to receive_message_chain("directories.create").with(key: "files", public: false).and_return(bucket)
         | 
| 116 | 
            -
             | 
| 104 | 
            +
                it "should create a bucket when trying to store a file on a missing bucket" do
         | 
| 117 105 | 
             
                  bucket.as_null_object
         | 
| 106 | 
            +
                  setup_create_bucket
         | 
| 118 107 |  | 
| 119 108 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files")
         | 
| 120 109 | 
             
                end
         | 
| 121 110 |  | 
| 122 | 
            -
                it "should raise when saving a file that already exists" do
         | 
| 123 | 
            -
                  object = double("object")
         | 
| 124 | 
            -
             | 
| 125 | 
            -
                  setup_bucket(bucket)
         | 
| 126 | 
            -
                  setup_get(object)
         | 
| 127 | 
            -
             | 
| 128 | 
            -
                  expect {
         | 
| 129 | 
            -
                    Storage.store(source, name: "lorem.txt", bucket: "files")
         | 
| 130 | 
            -
                  }.to raise_error(Storage::FileAlreadyExistsError)
         | 
| 131 | 
            -
                end
         | 
| 132 | 
            -
             | 
| 133 111 | 
             
                it "should set file permission to public" do
         | 
| 134 | 
            -
                   | 
| 112 | 
            +
                  setup_create_object public: true
         | 
| 135 113 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files", public: true)
         | 
| 136 114 | 
             
                end
         | 
| 137 115 |  | 
| 138 116 | 
             
                it "should set file permission to private (default)" do
         | 
| 139 | 
            -
                   | 
| 117 | 
            +
                  setup_create_object public: false
         | 
| 140 118 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files")
         | 
| 141 119 | 
             
                end
         | 
| 142 120 |  | 
| 143 121 | 
             
                it "should set file permission to private" do
         | 
| 144 | 
            -
                   | 
| 122 | 
            +
                  setup_create_object public: false
         | 
| 145 123 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files", public: false)
         | 
| 146 124 | 
             
                end
         | 
| 147 125 |  | 
| 148 126 | 
             
                it "should set file permission to private (access option)" do
         | 
| 149 | 
            -
                   | 
| 127 | 
            +
                  setup_create_object public: false
         | 
| 150 128 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files", access: :private)
         | 
| 151 129 | 
             
                end
         | 
| 152 130 |  | 
| 153 131 | 
             
                it "should set file permission to public (access option)" do
         | 
| 154 | 
            -
                   | 
| 132 | 
            +
                  setup_create_object public: true
         | 
| 155 133 | 
             
                  Storage.store(source, name: "lorem.txt", bucket: "files", access: :public_read)
         | 
| 156 134 | 
             
                end
         | 
| 157 135 |  | 
| 158 | 
            -
                def  | 
| 136 | 
            +
                def setup_create_object(bucket: self.bucket, object: nil, public: false, file_name: "lorem.txt")
         | 
| 159 137 | 
             
                  allow(connection).to receive_message_chain("directories.get").with("files").and_return(bucket)
         | 
| 138 | 
            +
                  expect(bucket).to receive_message_chain("files.create").with(key: file_name, body: kind_of(File), public: public)
         | 
| 160 139 | 
             
                end
         | 
| 161 140 |  | 
| 162 | 
            -
                def  | 
| 163 | 
            -
                  expect( | 
| 141 | 
            +
                def setup_get_object(bucket: self.bucket, file_name: "lorem.txt", object: nil)
         | 
| 142 | 
            +
                  expect(connection).to receive_message_chain("directories.get").with("files").and_return(bucket)
         | 
| 143 | 
            +
                  expect(bucket).to receive_message_chain("files.get").with(file_name).and_return(object)
         | 
| 164 144 | 
             
                end
         | 
| 165 145 |  | 
| 166 | 
            -
                def  | 
| 167 | 
            -
                   | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 170 | 
            -
                def setup_get(object)
         | 
| 171 | 
            -
                  expect(bucket).to receive_message_chain("files.get").with("lorem.txt").and_return(object)
         | 
| 146 | 
            +
                def setup_create_bucket(bucket: nil)
         | 
| 147 | 
            +
                  allow(connection).to receive_message_chain("directories.get").with("files").and_return(bucket)
         | 
| 148 | 
            +
                  expect(connection).to receive_message_chain("directories.create").with(key: "files", public: false).and_return(self.bucket)
         | 
| 172 149 | 
             
                end
         | 
| 173 150 |  | 
| 174 | 
            -
                def  | 
| 175 | 
            -
                   | 
| 176 | 
            -
                  setup_object(object)
         | 
| 177 | 
            -
                  setup_create(public_access)
         | 
| 151 | 
            +
                def setup_get_bucket(bucket: self.bucket)
         | 
| 152 | 
            +
                  allow(connection).to receive_message_chain("directories.get").with("files").and_return(bucket)
         | 
| 178 153 | 
             
                end
         | 
| 179 154 | 
             
              end
         | 
| 180 155 | 
             
            end
         |