class Refile::TinyPNG

Refile::TinyPNG.configure do |config|

config.key = ENV['TINYPNG_KEY']

end %i(fill fit limit pad convert).each do |name|

Refile.processor(name, Refile::TinyPNG.new(Refile::MiniMagick.new(name)))

end

Public Class Methods

configuration() click to toggle source
# File lib/refile/tinypng.rb, line 17
def self.configuration
  @configuration ||= Refile::TinyPNG::Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/refile/tinypng.rb, line 13
def self.configure
  yield configuration
end
new(processor) click to toggle source
# File lib/refile/tinypng.rb, line 25
def initialize(processor)
  @processor = processor
end

Public Instance Methods

call(*args) click to toggle source
# File lib/refile/tinypng.rb, line 29
def call(*args)
  processed_file = @processor.call(*args)
  processed_file.close unless processed_file.closed?

  key = Refile::TinyPNG.configuration.key
  input = processed_file.path
  output = processed_file.path

  uri = URI.parse('https://api.tinypng.com/shrink')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth('api', key)

  response = http.request(request, ::File.binread(input))
  if response.code == '201'
    ::File.binwrite(output, http.get(response['location']).body)
  else
    raise response.body
  end

  ::File.open(processed_file.path, 'rb')
end