Created
January 30, 2012 02:28
-
-
Save ender672/1702073 to your computer and use it in GitHub Desktop.
Rack application that returns image information as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rack' | |
require './jimmy' | |
run Rack::Jimmy.new('.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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