Skip to content

Instantly share code, notes, and snippets.

@ender672
Created January 30, 2012 02:28
Show Gist options
  • Save ender672/1702073 to your computer and use it in GitHub Desktop.
Save ender672/1702073 to your computer and use it in GitHub Desktop.
Rack application that returns image information as JSON
require 'rack'
require './jimmy'
run Rack::Jimmy.new('.')
require 'rubygems'
require 'json'
require 'rack/utils'
require 'time'
require 'axon'
module Rack
class Jimmy < File
class ImageJSON
def initialize(path)
@path = path
end
def each
Axon.jpeg_file(@path) do |im|
json = { :width => im.width, :height => im.height }.to_json
yield json
end
end
end
def serving(env)
header = { "Last-Modified" => F.mtime(@path).httpdate }
header["Content-Type"] = "application/json"
if env["REQUEST_METHOD"] == "HEAD"
[200, header, []]
else
[200, header, ImageJSON.new(@path)]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment