Class: Jackal::Assets::Store
- Inherits:
-
Object
- Object
- Jackal::Assets::Store
- Defined in:
- lib/jackal-assets/store.rb
Overview
Object storage helper
Instance Attribute Summary collapse
-
#arguments ⇒ Hash
readonly
Initializer arguments.
-
#bucket ⇒ String
Bucket name.
-
#connection ⇒ Object
readonly
Remote connection if applicable.
Instance Method Summary collapse
-
#bucket_name ⇒ String
Name of bucket.
-
#connection_arguments ⇒ Smash
Connection arguments.
-
#delete(key) ⇒ TrueClass, FalseClass
Delete object.
-
#get(key) ⇒ File
Fetch object.
-
#initialize(args = {}) ⇒ Store
constructor
Create new instance.
-
#pack(directory, name = nil) ⇒ File
Pack directory into compressed file.
-
#put(key, file) ⇒ TrueClass
Store object.
-
#setup ⇒ Object
Setup API connection and storage bucket.
-
#unpack(object, destination, *args) ⇒ String
Unpack object.
-
#url(key, expires_in = nil) ⇒ String
URL for object.
Constructor Details
#initialize(args = {}) ⇒ Store
Create new instance
22 23 24 25 26 27 |
# File 'lib/jackal-assets/store.rb', line 22 def initialize(args={}) @arguments = args.to_smash @connection_arguments = args[:connection] @bucket_name = args[:bucket] setup end |
Instance Attribute Details
#arguments ⇒ Hash (readonly)
Returns initializer arguments.
13 14 15 |
# File 'lib/jackal-assets/store.rb', line 13 def arguments @arguments end |
#bucket ⇒ String
Returns bucket name.
11 12 13 |
# File 'lib/jackal-assets/store.rb', line 11 def bucket @bucket end |
#connection ⇒ Object (readonly)
Returns remote connection if applicable.
15 16 17 |
# File 'lib/jackal-assets/store.rb', line 15 def connection @connection end |
Instance Method Details
#bucket_name ⇒ String
Returns name of bucket.
30 31 32 33 |
# File 'lib/jackal-assets/store.rb', line 30 def bucket_name @bucket_name || Carnivore::Config.get(:jackal, :assets, :bucket) end |
#connection_arguments ⇒ Smash
Returns connection arguments.
36 37 38 39 40 |
# File 'lib/jackal-assets/store.rb', line 36 def connection_arguments (@connection_arguments || Carnivore::Config.get(:jackal, :assets, :connection) || Smash.new).to_smash end |
#delete(key) ⇒ TrueClass, FalseClass
Delete object
105 106 107 108 109 110 111 112 113 |
# File 'lib/jackal-assets/store.rb', line 105 def delete(key) remote_file = bucket.files.reload.get(key) if(remote_file) remote_file.destroy true else false end end |
#get(key) ⇒ File
Fetch object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jackal-assets/store.rb', line 62 def get(key) remote_file = bucket.files.filter(:prefix => key).detect do |item| item.name == key end if(remote_file) io = remote_file.body.io if(io.respond_to?(:path)) io else e_file = Bogo::EphemeralFile.new('jackal-asset') e_file.binmode begin while(data = io.readpartial(2048)) e_file.write data end rescue EOFError end e_file.flush e_file.rewind e_file end else raise Error::NotFound.new "Remote file does not exist! (<#{bucket.name}>:#{key})" end end |
#pack(directory, name = nil) ⇒ File
Pack directory into compressed file
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/jackal-assets/store.rb', line 134 def pack(directory, name=nil) tmp_file = Tempfile.new(name || File.basename(directory)) file_path = "#{tmp_file.path}.zip" tmp_file.delete entries = Hash[ Dir.glob(File.join(directory, '**', '{*,.*}')).map do |path| next if path.end_with?('.') [path.sub(%r{#{Regexp.escape(directory)}/?}, ''), path] end ] Zip::File.open(file_path, Zip::File::CREATE) do |zipfile| entries.keys.sort.each do |entry| path = entries[entry] if(File.directory?(path)) zipfile.mkdir(entry.dup) else zipfile.add(entry, path) end end end file = File.open(file_path, 'rb') file end |
#put(key, file) ⇒ TrueClass
Store object
93 94 95 96 97 98 99 |
# File 'lib/jackal-assets/store.rb', line 93 def put(key, file) remote_file = bucket.files.reload.get(key) || bucket.files.build(:name => key) remote_file.body = file remote_file.save true end |
#setup ⇒ Object
Setup API connection and storage bucket
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jackal-assets/store.rb', line 43 def setup Carnivore.configure!(:verify) @connection = Miasma.api( connection_arguments.deep_merge( Smash.new( :type => :storage ) ) ) @bucket = @connection.buckets.get(bucket_name) unless(@bucket) @bucket = @connection.buckets.build(:name => bucket_name).save end end |
#unpack(object, destination, *args) ⇒ String
Unpack object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/jackal-assets/store.rb', line 164 def unpack(object, destination, *args) if(File.exists?(destination) && args.include?(:disable_overwrite)) destination else unless(File.directory?(destination)) FileUtils.mkdir_p(destination) end if(object.respond_to?(:path)) to_unpack = object.path elsif(object.respond_to?(:io)) to_unpack = object.io else to_unpack = object end zfile = Zip::File.new(to_unpack) zfile. = true zfile.each do |entry| new_dest = File.join(destination, entry.name) if(File.exists?(new_dest)) FileUtils.rm_rf(new_dest) end entry. = true entry.extract(new_dest) end destination end end |
#url(key, expires_in = nil) ⇒ String
URL for object
120 121 122 123 124 125 126 127 |
# File 'lib/jackal-assets/store.rb', line 120 def url(key, expires_in=nil) remote_file = bucket.files.reload.get(key) if(remote_file) remote_file.url(expires_in) else raise Error::NotFound.new "Remote file does not exist! (<#{bucket}>:#{key})" end end |