Class: TinyPNG::Image

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

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Image

Returns a new instance of Image.



7
8
9
# File 'lib/tinypng/image.rb', line 7

def initialize params={}
  @params = params
end

Instance Method Details

#inputObject



11
12
13
# File 'lib/tinypng/image.rb', line 11

def input
  @params['input']
end

#outputObject



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

def output
  @params['output']
end

#to_fileObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tinypng/image.rb', line 19

def to_file
  Tempfile.new(['tinypng', '.png']).tap do |file|
    begin
      image_response = TinyPNG::Client.get(output['url'])
    rescue => e
      raise Exception.new("Network error: #{e}")
    end

    file.write image_response
    file.rewind
  end
end