animalcracker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.log
3
+ assets/*
4
+ tmp/*
5
+ pkg/*
data/README.markdown ADDED
@@ -0,0 +1,3 @@
1
+ # Animal Cracker
2
+
3
+ Is yummy ...
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Default task: run all tests'
6
+ task :default => [:test]
7
+
8
+ desc "Run all tests"
9
+ Rake::TestTask.new("test") do |t|
10
+ t.libs.concat ['./lib', './test']
11
+ t.test_files = FileList['test/*_test.rb']
12
+ t.verbose = false
13
+ end
14
+
15
+ #
16
+ # Some monks like diamonds. I like gems.
17
+
18
+ begin
19
+ require 'jeweler'
20
+ Jeweler::Tasks.new do |gem|
21
+ gem.name = "animalcracker"
22
+ gem.summary = "A sweet Sinatra extension for asset hosting"
23
+ gem.description = "A sweet Sinatra extension for asset hosting. Roarrrrrrrr"
24
+ gem.email = "gus@gusg.us"
25
+ gem.homepage = "http://github.com/thumblemonks/animalcracker"
26
+ gem.authors = ["Justin 'Gus' Knowlden"]
27
+ gem.add_dependency "sinatra"
28
+ gem.add_development_dependency "riot"
29
+ gem.add_development_dependency "chicago"
30
+ end
31
+ Jeweler::GemcutterTasks.new
32
+ rescue LoadError
33
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
34
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{animalcracker}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Justin 'Gus' Knowlden"]
12
+ s.date = %q{2009-12-21}
13
+ s.description = %q{A sweet Sinatra extension for asset hosting. Roarrrrrrrr}
14
+ s.email = %q{gus@gusg.us}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README.markdown",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "animalcracker.gemspec",
24
+ "lib/animalcracker.rb",
25
+ "lib/animalcracker/asset_host.rb",
26
+ "test/animalcracker_test.rb",
27
+ "test/asset_host_test.rb",
28
+ "test/file_asset_host_test.rb",
29
+ "test/memory_asset_host_test.rb",
30
+ "test/teststrap.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/thumblemonks/animalcracker}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{A sweet Sinatra extension for asset hosting}
37
+ s.test_files = [
38
+ "test/animalcracker_test.rb",
39
+ "test/asset_host_test.rb",
40
+ "test/file_asset_host_test.rb",
41
+ "test/memory_asset_host_test.rb",
42
+ "test/teststrap.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<sinatra>, [">= 0"])
51
+ s.add_development_dependency(%q<riot>, [">= 0"])
52
+ s.add_development_dependency(%q<chicago>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<sinatra>, [">= 0"])
55
+ s.add_dependency(%q<riot>, [">= 0"])
56
+ s.add_dependency(%q<chicago>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<sinatra>, [">= 0"])
60
+ s.add_dependency(%q<riot>, [">= 0"])
61
+ s.add_dependency(%q<chicago>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,22 @@
1
+ require 'animalcracker/asset_host'
2
+ require 'sinatra'
3
+
4
+ module AnimalCracker
5
+ module Server
6
+
7
+ def self.extended(base)
8
+ base.get("*") do
9
+ begin
10
+ params[:splat].first.split(",").map { |asset_path| AssetHost[asset_path] || not_found }
11
+ rescue NotFound
12
+ not_found
13
+ end
14
+ end
15
+ end
16
+
17
+ end # Server
18
+ end # AnimalCracker
19
+
20
+ module Sinatra
21
+ register AnimalCracker::Server
22
+ end
@@ -0,0 +1,52 @@
1
+ require 'pathname'
2
+
3
+ module AnimalCracker
4
+ class NotFound < Exception; end
5
+ class ReadOnly < Exception; end
6
+
7
+ # Generic asset hosting proxy. Will default to MemoryAssetHost unless configured otherwise
8
+ class AssetHost
9
+ class << self
10
+ def configure(configuration)
11
+ asset_path = configuration["asset_path"]
12
+ self.asset_host = (asset_path == ":memory:") ? MemoryAssetHost.new : FileAssetHost.new(asset_path)
13
+ end
14
+
15
+ def [](*args) asset_host.find(*args); end
16
+ def []=(*args) asset_host.store(*args); end
17
+
18
+ def asset_host; @asset_host ||= MemoryAssetHost.new; end
19
+ def asset_host=(asset_host) @asset_host = asset_host; end
20
+ end
21
+ end # AssetHost
22
+
23
+ # Interface for retrieving assets from the filesystem given some root path.
24
+ class FileAssetHost
25
+ attr_reader :root
26
+ def initialize(root_path)
27
+ @root = Pathname.new(root_path).realpath
28
+ end
29
+
30
+ def find(path_to_asset)
31
+ (@root + path_to_asset.gsub(/^\//, '')).read
32
+ rescue Errno::ENOENT
33
+ raise(NotFound, "Could not find #{path_to_asset}")
34
+ end
35
+
36
+ def store(path_to_asset, contents) raise(ReadOnly, "Cannot store files with FileAssetHost"); end
37
+ end # FileAssetHost
38
+
39
+ # Basically, a hash of pathnames to file-contents. Useful for testing, but maybe even other things.
40
+ class MemoryAssetHost < Hash
41
+ def initialize(database=nil)
42
+ super()
43
+ update(database || {})
44
+ end
45
+
46
+ def find(path_to_asset)
47
+ self[path_to_asset] || raise(NotFound, "Could not find #{path_to_asset}")
48
+ end
49
+
50
+ def store(path_to_asset, contents) self[path_to_asset] = contents; end
51
+ end # MemoryAsset
52
+ end # AnimalCracker
@@ -0,0 +1,34 @@
1
+ require 'teststrap'
2
+
3
+ context "AnimalCracker Server:" do
4
+ setup do
5
+ mock_app { register AnimalCracker::Server }
6
+ end
7
+
8
+ context "unable to find basic asset" do
9
+ setup { get "/some/asset.ext" }
10
+ asserts_response_status 404
11
+ end # unable to find basic asset
12
+
13
+ context "get a basic asset" do
14
+ setup do
15
+ AnimalCracker::AssetHost["/some/asset.ext"] = "Foo"
16
+ get "/some/asset.ext"
17
+ end
18
+
19
+ asserts_response_status 200
20
+ asserts_response_body "Foo"
21
+ end # get a basic asset
22
+
23
+ context "get a grouping of assets" do
24
+ setup do
25
+ AnimalCracker::AssetHost["/foo/bar"] = "function a() {}"
26
+ AnimalCracker::AssetHost["/goo/car"] = "function b() {}"
27
+ get "/foo/bar,/goo/car"
28
+ end
29
+
30
+ asserts_response_status 200
31
+ asserts_response_body "function a() {}function b() {}"
32
+ end # get a grouping of assets
33
+
34
+ end # AnimalCracker Server
@@ -0,0 +1,45 @@
1
+ require 'teststrap'
2
+
3
+ context "Configuring Asset Host" do
4
+ setup { AnimalCracker::AssetHost }
5
+
6
+ asserts("default asset host") { topic.asset_host }.kind_of(AnimalCracker::MemoryAssetHost)
7
+
8
+ context "when asset_path is :memory:" do
9
+ setup do
10
+ topic.configure({"asset_path" => ":memory:"})
11
+ topic.asset_host
12
+ end
13
+
14
+ asserts_topic.kind_of(AnimalCracker::MemoryAssetHost)
15
+ end # when asset_path is :memory:
16
+
17
+ context "when asset_path does not match :memory:" do
18
+ setup do
19
+ topic.configure({"asset_path" => "tmp"})
20
+ topic.asset_host
21
+ end
22
+
23
+ asserts_topic.kind_of(AnimalCracker::FileAssetHost)
24
+
25
+ asserts("root path") do
26
+ topic.root
27
+ end.equals(Pathname("tmp").realpath)
28
+ end # when asset_path does not match :memory:
29
+
30
+ context "using proxy methods" do
31
+ mocha_support
32
+
33
+ should "call proxy [] to find on actual asset host" do
34
+ topic.asset_host.expects(:find).with("/baz").returns("happy ending")
35
+ topic["/baz"]
36
+ end.equals("happy ending")
37
+
38
+ should "call proxy []= to store on actual asset host" do
39
+ topic.asset_host.expects(:store).with("/bax", "jaw")
40
+ topic["/bax"] = "jaw"
41
+ end
42
+ end # using proxy methods
43
+
44
+ teardown { AnimalCracker::AssetHost.asset_host = nil }
45
+ end # Configuring Asset Host
@@ -0,0 +1,24 @@
1
+ require 'teststrap'
2
+
3
+ context "A File Asset Host" do
4
+ setup do
5
+ @root = Pathname.new(__FILE__).dirname + "../tmp/animalcracker"
6
+ (@root + "foo").mkpath
7
+ AnimalCracker::FileAssetHost.new(@root.realpath)
8
+ end
9
+
10
+ asserts("store") do
11
+ topic.store("a", "b")
12
+ end.raises(AnimalCracker::ReadOnly, "Cannot store files with FileAssetHost")
13
+
14
+ asserts("find scopes search to root path") do
15
+ (@root + "foo/bar").open("w+") { |f| f.write("disco jaws") }
16
+ topic.find("/foo/bar")
17
+ end.equals("disco jaws")
18
+
19
+ asserts("nonexistent file") do
20
+ topic.find("/foo/barge")
21
+ end.raises(AnimalCracker::NotFound, "Could not find /foo/barge")
22
+
23
+ teardown { @root.rmtree }
24
+ end # A File Asset Host
@@ -0,0 +1,27 @@
1
+ require 'teststrap'
2
+
3
+ context "Memory Asset Host" do
4
+ setup do
5
+ AnimalCracker::MemoryAssetHost.new({"/bar" => "juice"})
6
+ end
7
+
8
+ asserts("nonexistent asset") do
9
+ topic.find("/foo")
10
+ end.raises(AnimalCracker::NotFound, "Could not find /foo")
11
+
12
+ should("return asset contents if defined") { topic.find("/bar") }.equals("juice")
13
+
14
+ should("allow asset to be defined") do
15
+ topic.store("/bar/man", "bouncer")
16
+ topic.find("/bar/man")
17
+ end.equals("bouncer")
18
+ end # Memory Asset Host
19
+
20
+ context "Memory Asset Host accessed as AnimalCracker::AssetHost" do
21
+ setup do
22
+ AnimalCracker::AssetHost.asset_host = AnimalCracker::MemoryAssetHost.new({"/dude" => "lebowski"})
23
+ AnimalCracker::AssetHost
24
+ end
25
+
26
+ asserts("asset host") { topic.asset_host }.kind_of(AnimalCracker::MemoryAssetHost)
27
+ end # Memory Asset Host accessed as AnimalCracker::AssetHost
data/test/teststrap.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+
3
+ require 'chicago/riot'
4
+ require 'mocha_standalone'
5
+
6
+ require 'animalcracker'
7
+
8
+ class Riot::Situation
9
+ include Mocha::API
10
+ end
11
+
12
+ Riot::Context.class_eval do
13
+ def mocha_support
14
+ teardown { mocha_verify; mocha_teardown }
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: animalcracker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin 'Gus' Knowlden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-21 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: riot
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: chicago
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: A sweet Sinatra extension for asset hosting. Roarrrrrrrr
46
+ email: gus@gusg.us
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - README.markdown
53
+ files:
54
+ - .gitignore
55
+ - README.markdown
56
+ - Rakefile
57
+ - VERSION
58
+ - animalcracker.gemspec
59
+ - lib/animalcracker.rb
60
+ - lib/animalcracker/asset_host.rb
61
+ - test/animalcracker_test.rb
62
+ - test/asset_host_test.rb
63
+ - test/file_asset_host_test.rb
64
+ - test/memory_asset_host_test.rb
65
+ - test/teststrap.rb
66
+ has_rdoc: true
67
+ homepage: http://github.com/thumblemonks/animalcracker
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: A sweet Sinatra extension for asset hosting
94
+ test_files:
95
+ - test/animalcracker_test.rb
96
+ - test/asset_host_test.rb
97
+ - test/file_asset_host_test.rb
98
+ - test/memory_asset_host_test.rb
99
+ - test/teststrap.rb