Class: ImgShark::Image

Inherits:
Map
  • Object
show all
Defined in:
lib/imgshark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#blobObject

Returns the value of attribute blob.



57
58
59
# File 'lib/imgshark.rb', line 57

def blob
  @blob
end

#imgsharkObject (readonly)

Returns the value of attribute imgshark.



56
57
58
# File 'lib/imgshark.rb', line 56

def imgshark
  @imgshark
end

Instance Method Details

#amazon_pathObject



107
108
109
# File 'lib/imgshark.rb', line 107

def amazon_path
  "#{imgshark.bucket}/#{filename}"
end

#amazon_urlObject



103
104
105
# File 'lib/imgshark.rb', line 103

def amazon_url
  "http://s3.amazonaws.com/#{amazon_path}"
end

#attributesObject



85
86
87
# File 'lib/imgshark.rb', line 85

def attributes
  {url: url, h: height, w: width, amazon_url: amazon_url }  
end

#exists?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/imgshark.rb', line 69

def exists?
  !!imgshark.find(key, {})
end

#filenameObject



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

#heightObject



95
96
97
# File 'lib/imgshark.rb', line 95

def height
  self.h.to_i
end

#keyObject



115
116
117
# File 'lib/imgshark.rb', line 115

def key
  Digest::SHA1.hexdigest("#{url}#{height}#{width}")
end

#on_amazon?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/imgshark.rb', line 111

def on_amazon?
  Http.head(amazon_url).status == 200
end

#resizeObject



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

#saveObject



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

#widthObject



99
100
101
# File 'lib/imgshark.rb', line 99

def width
  self.w.to_i
end