Class: Resizor::ResizorAsset

Inherits:
Object
  • Object
show all
Defined in:
lib/resizor/asset.rb

Direct Known Subclasses

AttachedResizorAsset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ResizorAsset

Returns a new instance of ResizorAsset.



5
6
7
# File 'lib/resizor/asset.rb', line 5

def initialize(options={})
  options.each { |k,v| send "#{k}=".to_sym, v }
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def height
  @height
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def id
  @id
end

#mime_typeObject

Returns the value of attribute mime_type.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def mime_type
  @mime_type
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def name
  @name
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def path
  @path
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def size
  @size
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/resizor/asset.rb', line 3

def width
  @width
end

Instance Method Details

#destroyObject



39
40
41
42
43
44
45
46
# File 'lib/resizor/asset.rb', line 39

def destroy
  if id && id.to_s != ''
    ret = Resizor.delete("/assets/#{id}.json")
    if ret.code == 200
     return true
    end
  end
end

#resize_token_for(options = {}) ⇒ Object



17
18
19
20
# File 'lib/resizor/asset.rb', line 17

def resize_token_for(options={})
  options = {:size => '200', :format => 'jpg'}.merge(options)
  Digest::SHA1.hexdigest("#{Resizor.api_key}-#{id}-#{options[:size]}-#{options[:format]}")
end

#save_to_resizor(params = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/resizor/asset.rb', line 22

def save_to_resizor(params = {})
  if path && File.exists?(path)
    ret = Resizor.post('/assets.json', params.merge(:file => File.open(path, 'rb')))
    if ret.code == 201
      @id = ret.body['asset']['id']
      @name = "#{ret.body['asset']['name']}.#{ret.body['asset']['extension']}"
      @mime_type = ret.body['asset']['mime_type']
      @size = ret.body['asset']['file_size']
      @width = ret.body['asset']['width']
      @height = ret.body['asset']['height']
    else
      return false
    end
  end
  return true
end

#url(options = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/resizor/asset.rb', line 9

def url(options={})
  options = {:size => '200', :format => 'jpg', :cdn_host => !Resizor.cdn_host.nil?}.merge(options)
  options[:size] = [options[:size], "q", options.delete(:quality)].join if options[:quality]

  options[:cdn_host] ? cdn_compatible_url(options) : query_string_url(options)
end