Class: ImgShark::Image
- Inherits:
-
Map
- Object
- Map
- ImgShark::Image
- Defined in:
- lib/imgshark.rb
Instance Attribute Summary collapse
-
#blob ⇒ Object
Returns the value of attribute blob.
-
#imgshark ⇒ Object
readonly
Returns the value of attribute imgshark.
Instance Method Summary collapse
- #amazon_path ⇒ Object
- #amazon_url ⇒ Object
- #attributes ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
- #height ⇒ Object
-
#initialize(imgshark, attributes) ⇒ Image
constructor
A new instance of Image.
- #key ⇒ Object
- #on_amazon? ⇒ Boolean
- #resize ⇒ Object
- #save ⇒ Object
- #upload(opts = {:access => :public_read}) ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(imgshark, attributes) ⇒ Image
Returns a new instance of Image.
59 60 61 62 |
# File 'lib/imgshark.rb', line 59 def initialize(imgshark, attributes) @imgshark = imgshark super(attributes) end |
Instance Attribute Details
#blob ⇒ Object
Returns the value of attribute blob.
57 58 59 |
# File 'lib/imgshark.rb', line 57 def blob @blob end |
#imgshark ⇒ Object (readonly)
Returns the value of attribute imgshark.
56 57 58 |
# File 'lib/imgshark.rb', line 56 def imgshark @imgshark end |
Instance Method Details
#amazon_path ⇒ Object
107 108 109 |
# File 'lib/imgshark.rb', line 107 def amazon_path "#{imgshark.bucket}/#{filename}" end |
#amazon_url ⇒ Object
103 104 105 |
# File 'lib/imgshark.rb', line 103 def amazon_url "http://s3.amazonaws.com/#{amazon_path}" end |
#attributes ⇒ Object
85 86 87 |
# File 'lib/imgshark.rb', line 85 def attributes {url: url, h: height, w: width, amazon_url: amazon_url } end |
#exists? ⇒ Boolean
69 70 71 |
# File 'lib/imgshark.rb', line 69 def exists? !!imgshark.find(key, {}) end |
#filename ⇒ Object
89 90 91 92 93 |
# File 'lib/imgshark.rb', line 89 def filename @filename = url.split('/', 4).last ext = File.extname(@filename) @filename.gsub(ext, "_#{height}X#{width}#{ext}") end |
#height ⇒ Object
95 96 97 |
# File 'lib/imgshark.rb', line 95 def height self.h.to_i end |
#key ⇒ Object
115 116 117 |
# File 'lib/imgshark.rb', line 115 def key Digest::SHA1.hexdigest("#{url}#{height}#{width}") end |
#on_amazon? ⇒ Boolean
111 112 113 |
# File 'lib/imgshark.rb', line 111 def on_amazon? Http.head(amazon_url).status == 200 end |
#resize ⇒ Object
73 74 75 76 77 |
# File 'lib/imgshark.rb', line 73 def resize data = Http.get(url) # todo check status data = Magick::Image.from_blob(data).first self.blob = data.resize_to_fill(width, height).to_blob end |
#save ⇒ Object
64 65 66 67 |
# File 'lib/imgshark.rb', line 64 def save response = imgshark.redis.set(key, attributes.to_json) response == 'OK' end |
#upload(opts = {:access => :public_read}) ⇒ Object
79 80 81 82 83 |
# File 'lib/imgshark.rb', line 79 def upload(opts = {:access => :public_read}) raise 'no blob' unless blob # TODO upload only if the image isn't there AWS::S3::S3Object.store(filename, blob, imgshark.bucket, opts) end |
#width ⇒ Object
99 100 101 |
# File 'lib/imgshark.rb', line 99 def width self.w.to_i end |