From 5f7bc6a4038acb67e39719a7fd114ecb925e1492 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 23 Mar 2011 14:42:54 -0400 Subject: [PATCH] initial commit --- .autotest | 6 + .gitignore | 2 + .rspec | 2 + Gemfile | 18 +++ Gemfile.lock | 83 +++++++++++ autotest/discover.rb | 1 + config.rb | 3 + config.ru | 6 + lib/avm_example.rb | 63 ++++++++ public/index.css | 77 ++++++++++ sass/index.scss | 59 ++++++++ spec/lib/avm_example_spec.rb | 53 +++++++ spec/spec_helper.rb | 8 + spec/xmp/bad.xmp | 1 + spec/xmp/good.xmp | 273 +++++++++++++++++++++++++++++++++++ views/index.haml | 4 + views/layout.haml | 17 +++ views/print_data.haml | 4 + 18 files changed, 680 insertions(+) create mode 100644 .autotest create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 autotest/discover.rb create mode 100644 config.rb create mode 100644 config.ru create mode 100644 lib/avm_example.rb create mode 100644 public/index.css create mode 100644 sass/index.scss create mode 100644 spec/lib/avm_example_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/xmp/bad.xmp create mode 100644 spec/xmp/good.xmp create mode 100644 views/index.haml create mode 100644 views/layout.haml create mode 100644 views/print_data.haml diff --git a/.autotest b/.autotest new file mode 100644 index 0000000..2f80a87 --- /dev/null +++ b/.autotest @@ -0,0 +1,6 @@ +Autotest.add_hook(:initialize) do |at| + at.add_mapping(%r{views/.*\.haml}, true) do |filename, matches| + [ 'spec/avm_example_spec.rb' ] + end +end + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8bf9ddc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.sass-cache + diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..2d0bd84 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +-c + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..5f8e9f1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,18 @@ +source :rubygems + +#gem 'ruby-avm-library', :git => 'git@github.com:johnbintz/ruby-avm-library.git' +gem 'ruby-avm-library', :path => '../ruby-avm-library' +gem 'sinatra' +gem 'haml' +gem 'sinatra-flash' +gem 'coderay' +gem 'sinatra-content-for' + +group :development do + gem 'shotgun' + gem 'mocha' + gem 'autotest' + gem 'rspec' + gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' +end + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..f203aa5 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,83 @@ +GIT + remote: git://github.com/jnicklas/capybara.git + revision: 549e67336c712a1ef2119ce5ff64dbbc7542480f + specs: + capybara (0.4.1.1) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + selenium-webdriver (>= 0.0.27) + xpath (~> 0.1.3) + +PATH + remote: ../ruby-avm-library + specs: + ruby-avm-library (0.0.1) + nokogiri + thor + +GEM + remote: http://rubygems.org/ + specs: + ZenTest (4.5.0) + autotest (4.4.6) + ZenTest (>= 4.4.1) + childprocess (0.1.8) + ffi (~> 1.0.6) + coderay (0.9.7) + diff-lcs (1.1.2) + ffi (1.0.7) + rake (>= 0.8.7) + haml (3.0.25) + json_pure (1.5.1) + mime-types (1.16) + mocha (0.9.12) + nokogiri (1.4.4) + rack (1.2.2) + rack-test (0.5.7) + rack (>= 1.0) + rake (0.8.7) + rspec (2.5.0) + rspec-core (~> 2.5.0) + rspec-expectations (~> 2.5.0) + rspec-mocks (~> 2.5.0) + rspec-core (2.5.1) + rspec-expectations (2.5.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.5.0) + rubyzip (0.9.4) + selenium-webdriver (0.1.4) + childprocess (>= 0.1.7) + ffi (>= 1.0.7) + json_pure + rubyzip + shotgun (0.9) + rack (>= 1.0) + sinatra (1.2.1) + rack (~> 1.1) + tilt (>= 1.2.2, < 2.0) + sinatra-content-for (0.2) + sinatra + sinatra-flash (0.3.0) + sinatra (>= 1.0.0) + thor (0.14.6) + tilt (1.2.2) + xpath (0.1.3) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + autotest + capybara! + coderay + haml + mocha + rspec + ruby-avm-library! + shotgun + sinatra + sinatra-content-for + sinatra-flash diff --git a/autotest/discover.rb b/autotest/discover.rb new file mode 100644 index 0000000..47355c9 --- /dev/null +++ b/autotest/discover.rb @@ -0,0 +1 @@ +Autotest.add_discovery { 'rspec2' } diff --git a/config.rb b/config.rb new file mode 100644 index 0000000..68d6821 --- /dev/null +++ b/config.rb @@ -0,0 +1,3 @@ +http_path = "/" +css_dir = "public" +sass_dir = "sass" diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..0ccc975 --- /dev/null +++ b/config.ru @@ -0,0 +1,6 @@ +$: << File.expand_path("lib") + +require 'avm_example' + +run AVMExample.new + diff --git a/lib/avm_example.rb b/lib/avm_example.rb new file mode 100644 index 0000000..600c645 --- /dev/null +++ b/lib/avm_example.rb @@ -0,0 +1,63 @@ +require 'sinatra/base' +require 'sinatra/flash' +require 'sinatra/content_for' +require 'avm/image' +require 'coderay' +require 'pp' + +class AVMExample < Sinatra::Base + use Rack::Session::Cookie, :key => 'avm.session', + :path => '/', + :expire_after => 60 + + register Sinatra::Flash + helpers Sinatra::ContentFor + + helpers do + def uploaded? + @is_uploaded ||= ensure_uploaded_xmp + end + + def tempfile + ensure_uploaded_xmp + @tempfile + end + + def filename + ensure_uploaded_xmp + @filename + end + end + + post '/upload' do + redirect_with_warn("No file provided!") if !uploaded? + + begin + @image = AVM::Image.from_xml(params[:file][:tempfile].read) + redirect_with_warn("Bad XMP file!") if !@image.valid? + rescue StandardError => e + redirect_with_warn("Error reading XMP file!") + $stderr.puts e.to_s + end + + flash[:info] = %{File processed! Data structure is below.} + haml :print_data + end + + get %r{/?} do + haml :index + end + + private + def redirect_with_warn(warning) + flash[:warn] = warning + redirect '/' + end + + def ensure_uploaded_xmp + return false if !params[:file] + return false if !(@tempfile = params[:file][:tempfile]) + return false if !(@filename = params[:file][:filename]) + true + end +end diff --git a/public/index.css b/public/index.css new file mode 100644 index 0000000..9d88dc5 --- /dev/null +++ b/public/index.css @@ -0,0 +1,77 @@ +@charset "UTF-8"; +/* line 3, ../sass/index.scss */ +body { + background-color: #bbb; +} + +/* line 7, ../sass/index.scss */ +pre { + border: solid #333 1px; + background-color: #222; + padding: 10px; + overflow: auto; + color: #ddd; +} +/* line 14, ../sass/index.scss */ +pre .sy { + color: #a40; +} +/* line 15, ../sass/index.scss */ +pre .dl { + color: #0c7; +} +/* line 16, ../sass/index.scss */ +pre .k { + color: #970; +} +/* line 17, ../sass/index.scss */ +pre .co, pre .i { + color: #05c; +} +/* line 18, ../sass/index.scss */ +pre .fl { + color: #3c1; +} + +/* line 21, ../sass/index.scss */ +#container { + width: 30%; + margin: 10% auto; + text-align: center; + border: solid #555 1px; + padding: 50px; + -moz-box-shadow: 0 0 10px 10px #ccccdd; + -webkit-box-shadow: 0 0 10px 10px #ccccdd; + -o-box-shadow: 0 0 10px 10px #ccccdd; + box-shadow: 0 0 10px 10px #ccccdd; + background-color: #f0f0f0; +} +/* line 30, ../sass/index.scss */ +#container h2 { + margin: 0 0 10px; +} +/* line 34, ../sass/index.scss */ +#container .flash { + border-style: solid; + border-color: #ccc; + border-width: 1px 0; + padding: 3px 0; +} +/* line 42, ../sass/index.scss */ +#container .flash#warning { + color: #a00; + font-weight: bold; +} +/* line 47, ../sass/index.scss */ +#container .flash#info { + color: #363; +} + +/* line 53, ../sass/index.scss */ +#footer { + font-size: 80%; + text-align: center; + padding: 10px; + margin-top: 10px; + color: #555; +} diff --git a/sass/index.scss b/sass/index.scss new file mode 100644 index 0000000..ee1e1ee --- /dev/null +++ b/sass/index.scss @@ -0,0 +1,59 @@ +@import 'compass/css3'; + +body { + background-color: #bbb; +} + +pre { + border: solid #333 1px; + background-color: #222; + padding: 10px; + overflow: auto; + color: #ddd; + + .sy { color: #a40 } + .dl { color: #0c7 } + .k { color: #970 } + .co, .i { color: #05c } + .fl { color: #3c1 } +} + +#container { + width: 30%; + margin: 10% auto; + text-align: center; + border: solid #555 1px; + padding: 50px; + @include single-box-shadow(#ccd, 0, 0, 10px, 10px); + background-color: #f0f0f0; + + h2 { + margin: 0 0 10px; + } + + .flash { + border: { + style: solid; + color: #ccc; + width: 1px 0; + } + padding: 3px 0; + + &#warning { + color: #a00; + font-weight: bold; + } + + &#info { + color: #363; + } + } +} + +#footer { + font-size: 80%; + text-align: center; + padding: 10px; + margin-top: 10px; + color: #555; +} diff --git a/spec/lib/avm_example_spec.rb b/spec/lib/avm_example_spec.rb new file mode 100644 index 0000000..85b7227 --- /dev/null +++ b/spec/lib/avm_example_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' +require 'avm_example' + +describe AVMExample do + before { Capybara.app = self.class.describes } + + let(:title) { 'Upload an XMP file' } + + describe 'GET "index"' do + it "should return a form" do + visit '/' + page.should have_content(title) + page.should have_xpath('//form[@action="/upload" and @method="post" and @enctype="multipart/form-data"]') + + within('form') do + page.should have_selector('input[type="file"][name="file"]') + page.should have_selector('input[type="submit"]') + end + end + end + + describe 'POST "upload"' do + before { visit '/' } + + context 'no file' do + it "should fail" do + click_button("Upload") + page.should have_content(title) + page.should have_content("No file provided!") + end + end + + context 'bad xmp file' do + it "should fail" do + attach_file('file', 'spec/xmp/bad.xmp') + click_button("Upload") + page.should have_content(title) + page.should have_content("Bad XMP file!") + end + end + + context 'good xmp file' do + it "should fail" do + attach_file('file', 'spec/xmp/good.xmp') + click_button("Upload") + page.should have_content(title) + page.should have_content("Arp's Loop") + page.should have_content("NASA, ESA, and D.") + end + end + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..90b73e8 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,8 @@ +require 'mocha' +require 'capybara/rspec' + +RSpec.configure do |config| + config.mock_with :mocha + config.include Capybara +end + diff --git a/spec/xmp/bad.xmp b/spec/xmp/bad.xmp new file mode 100644 index 0000000..0dab0bf --- /dev/null +++ b/spec/xmp/bad.xmp @@ -0,0 +1 @@ + + + + + 48DFC39DCAA99A18E444091BA6C7B52F + STScI + 2008-01-08 + NASA, ESA, and D. de Mello (Catholic University of America and GSFC;NASA, ESA, and the Hubble Heritage Team (STScI/AURA) + Arp's Loop, imaged by the Hubble Space Telescope. It is located 12 million light-years away in the constellation Ursa Major. + 2007-11-28T13:25:46-05:00 File M82IsB071127.psd opened 2007-11-28T13:43:26-05:00 File M82IsB071127.psd saved 2007-12-12T12:35:16-05:00 File M82IsB071127.psd opened 2007-12-12T14:52:31-05:00 File M82IsB071212.psd saved 2007-12-12T16:54:03-05:00 File M82IsB071212.psd saved 2007-12-12T17:15:01-05:00 File BlueBlobs071212flat.psd saved 2007-12-13T09:43:34-05:00 File BlueBlobs071212flat.psd opened 2007-12-13T09:44:33-05:00 File BlueBlobs16.tif saved 2007-12-13T14:24:48-05:00 File BlueBlobs16crop_cln.psd opened 2007-12-13T14:34:30-05:00 File BlueBlobs16crop_cln.psd saved 2007-12-13T14:35:46-05:00 File p0802a1.tif saved 2007-12-13T14:40:44-05:00 File p0802a1.tif saved 2007-12-13T14:41:28-05:00 File BlueBlobs071213clean.psd saved 2007-12-13T14:41:59-05:00 File p0802a1 opened 2007-12-13T14:42:28-05:00 File p0802a1.tif saved + 3 + Adobe RGB (1998) + + + Adobe Photoshop CS5 Macintosh + 2011-03-03T14:24:10-05:00 + 2011-02-16T13:31:55-05:00 + 2007-12-13T14:41:28-05:00 + + + uuid:C660B4051BABDC11B13CB5CC852C8B0A + xmp.iid:03EF56B1B9206811AFFD8085DBA8F510 + uuid:C660B4051BABDC11B13CB5CC852C8B0A + + xmp.iid:02EF56B1B9206811AFFD8085DBA8F510 + uuid:C660B4051BABDC11B13CB5CC852C8B0A + uuid:C660B4051BABDC11B13CB5CC852C8B0A + + + + + saved + xmp.iid:01EF56B1B9206811AFFD8085DBA8F510 + 2011-02-16T13:31:55-05:00 + Adobe Photoshop CS5 Macintosh + / + + + saved + xmp.iid:02EF56B1B9206811AFFD8085DBA8F510 + 2011-02-16T13:31:55-05:00 + Adobe Photoshop CS5 Macintosh + / + + + derived + converted from image/tiff to image/jpeg + + + saved + xmp.iid:03EF56B1B9206811AFFD8085DBA8F510 + 2011-02-16T13:31:55-05:00 + Adobe Photoshop CS5 Macintosh + / + + + + + + image/jpeg + + + Public + + + + + Space Telescope Science Institute Office of Public Outreach + + + + + A Hubble Space Telescope image of an area in the neighborhood of the interacting galaxies, M81 and M82, which lie 12 million light-years away in the constellation Ursa Major. The gravity from each galaxy dramatically affected the other during their last close encounter, 200 million years ago. Gas density waves rippling around M81 make it a grand design spiral. M82 is undergoing a starburst at its core, creating glowing fingers of hydrogen. The visible light Hubble image shows bright blue star clusters found along a wispy bridge of gas that was tidally stretched between the two galaxies, and a third companion galaxy. Turbulence in the normally low-density gas may have enhanced the density locally to trigger starbirth. The so-called "blue blobs" are clumped together in a structure called Arp's Loop. Hubble reveals the clusters contain the equivalent of five Orion Nebulae. Hubble's Advanced Camera for Surveys imaged the region in blue and infrared light in September of 2006. + + + + + Arp's Loop + A0952+69 + + + + + Hubble View of Arp's Loop + + + + + 1.2 + TAN + J2000 + Observation + Full + ICRS + 31.32 + vamp://stsci + STScI + Good + 2250;2265 + Galaxy > Dwarf;September 22,2006 + J9RA1B010;J9RA1B020 + 2006-09-22;2006-09-22 + 475;814 + g;I + Optical;Optical + Blue;Red + ACS;ACS + HST;HST + 10915 + STScI-PRC-2008-02-b + http://hubblesite.org/newscenter/archive/releases/2008/02/image/b + 12 million light-years (3.6 Megaparsecs) + 3700000 + X.Galaxy > Dwarf + http://hubblesite.org/newscenter/archive/releases/2008/02 + hs-2008-02-b-full-jpg.jpg + http://archive.stsci.edu/pub/stpr/hs-2008-02-b-full-jpg.jpg + 03/03/11 + + + 2.76212980700322 + + + + + 0.00000000000000 + + + + + 5.00000000000000 + + + + + 0.00000000000000 + + + + + 500.00000000000000 + + + + + -41.81356223064012 + + + + + ArcSinh(x) + + + + + 1.3902e-05 + 1.3902008e-05 + + + + + 1294.0 + 968.5 + + + + + 2586.0 + 1935.0 + + + + + 149.3752491 + 69.2787976 + + + + + -1.1866504188e-05 + -7.2270075932e-06 + -7.1871389407e-06 + 1.1875865591e-05 + + + + + WCS retrieved using CXCs PinpointWCS + + + + + True + + + http://hubblesite.org/copyright/ + + + http://hubblesite.org/copyright/ + + + + + 0221 + 2586 + 1935 + 65535 + + + 1 + 2586 + 1935 + 2 + 3 + 300/1 + 300/1 + 2 + + + 8 + 8 + 8 + + + + + + 3700 San Martin Drive + Baltimore + MD + 21218 + USA + 410-338-4444 + outreach@stsci.edu + http://hubblesite.org + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/views/index.haml b/views/index.haml new file mode 100644 index 0000000..fe55570 --- /dev/null +++ b/views/index.haml @@ -0,0 +1,4 @@ +%h2 Upload an XMP file +%form{:action => '/upload', :method => :post, :enctype => 'multipart/form-data'} + %input{:type => :file, :name => :file} + %input{:type => :submit, :value => "Upload"} diff --git a/views/layout.haml b/views/layout.haml new file mode 100644 index 0000000..43c0b78 --- /dev/null +++ b/views/layout.haml @@ -0,0 +1,17 @@ +!!! +%html + %head + %title AVM Test + %link{:rel => :stylesheet, :href => 'index.css', :type => 'text/css'} + %body + #container + - if flash[:warn] + .flash#warning= flash[:warn] + - if flash[:info] + .flash#info= flash[:info] + = yield + %a{:name => "data"} + #data + = yield_content :data + #footer + Example usage application for the Ruby AVM Library. Get the source. diff --git a/views/print_data.haml b/views/print_data.haml new file mode 100644 index 0000000..2e00b01 --- /dev/null +++ b/views/print_data.haml @@ -0,0 +1,4 @@ +- content_for :data do + %pre= CodeRay.scan(@image.to_h.pretty_inspect, :ruby).html + += haml :index