Class: Stilts::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/stilts/image.rb

Overview

Intermediate utility class for image manipulation

Constant Summary collapse

SLUG_ABBREVIATIONS =
{ 
  :device_width_ratio => "dwr"
}
TRANSFORM_OPTIONS =
[
  "device_width_ratio"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Image

:nodoc:



18
19
20
21
# File 'lib/stilts/image.rb', line 18

def initialize(source, options)
  self.source               = source
  self.device_width_ratio   = options[:device_width_ratio] || 1
end

Instance Attribute Details

#device_width_ratioObject

The public location of the image



15
16
17
# File 'lib/stilts/image.rb', line 15

def device_width_ratio
  @device_width_ratio
end

#sourceObject

The public location of the image



15
16
17
# File 'lib/stilts/image.rb', line 15

def source
  @source
end

Instance Method Details

#cdn_urlObject

Location on server that will server this image



41
42
43
44
45
46
47
# File 'lib/stilts/image.rb', line 41

def cdn_url
  Stilts.configuration.protocol + "://" +
  Stilts.configuration.cdn_host +
  "/images/" +
  Stilts.configuration.api_key + "/" +
  self.name
end

#deliverObject



49
50
51
# File 'lib/stilts/image.rb', line 49

def deliver
  Stilts.sender.send_image_transform_data("[#{self.to_json}]")
end

#nameObject

The full image name, a combination of url_id and params_slug



36
37
38
# File 'lib/stilts/image.rb', line 36

def name
  url_id + "_" + params_slug
end

#params_slugObject

Combines resizing parameters into a short, unique slug to be appended to the final image name



29
30
31
32
33
# File 'lib/stilts/image.rb', line 29

def params_slug
  TRANSFORM_OPTIONS.map do |option|
    param_slug(option)
  end.join("")
end

#to_hashObject

A hash representation of the image attributes



59
60
61
62
63
64
# File 'lib/stilts/image.rb', line 59

def to_hash
  {
    :source => self.source,
    :name => self.name,
  }
end

#to_jsonObject

Ready images for transport across voidy vastness of teh interweb



54
55
56
# File 'lib/stilts/image.rb', line 54

def to_json
  JSON.generate(self.to_hash)
end

#url_idObject

Generates a SHA1 represenation for a particular URL



24
25
26
# File 'lib/stilts/image.rb', line 24

def url_id
  Digest::SHA1.hexdigest(self.source)
end