mcnamara 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/Change.log +11 -0
  2. data/LICENSE +19 -0
  3. data/README +42 -0
  4. data/Rakefile +31 -0
  5. data/VERSION +1 -0
  6. data/lib/mcnamara.rb +121 -0
  7. metadata +61 -0
data/Change.log ADDED
@@ -0,0 +1,11 @@
1
+ -- Nov. 25th, 2009
2
+
3
+ * added some file name functionality to support a request:
4
+ "Would be nice to be able to name CSS files that cover multiple versions of a specific folder, e.g.:
5
+ style-msie-lte-6.css (less than or equal to IE 6.0)
6
+ style-msie-gt-6.css (greater than)
7
+ style-msie-7-to-8.css OR style-msie-8-to-7.css (reversed)"
8
+
9
+ -- Nov. 24th, 2009
10
+
11
+ * coded initial version of McNamara
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Kristan 'Krispy' Uccello <krispy@soldierofcode.com> - Soldier Of Code
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,42 @@
1
+ = McNamara
2
+
3
+ STILL A WORK IN PROGRESS
4
+
5
+ After working on Spy vs Spy (http://github.com/kuccello/spy-vs-spy) I wanted to build a system that would allow me to serve up css specific to various browsers if I needed to. How many times have you fixed a css design in one browser only to have it break in another. Then you fix the other and it break the first one! McNamara attempts to solve this issue by allowing you to have css browser/platform overrides that will be served up in place of your default one.
6
+
7
+ Here's how it works:
8
+
9
+ Say you have a web application that serves up css from the directory "/home/kuccello/myapp/public/css" and you have a css file called "style.css" in that directory. Now, you have a special case for Internet Explorer 6.0 and another case for Safari. What you would do is create another file in your css directory (can even be a sub directory deeply nested!) called "style-msie-6-0.css" and you put your full css into that (that is all the contents of style with the changes needed for internet explorer. As well you would create a file called "style-safari.css" where you would put all of the contents of "style.css" with the changes specific for Safari. Then you put the McNamara middleware upstream of what ever is serving your css and voila! when the browser is Safari the "style-safari.css" is served and when the browser is IE6.0 then the "style-msie-6-0.css" is served and all you need to reference in your html is "style.css"!!
10
+
11
+ Now there is more, you can also use comment style overrides in your file names too.
12
+
13
+ >> style-msie-lte-6.css (less than or equal to IE 6.0)
14
+ >> style-msie-gt-6.css (greater than)
15
+ >> style-msie-7-to-8.css OR style-msie-8-to-7.css (reversed)
16
+ >> style-msie-6-to-8.css OR style-msie-8-to-6.css TWO VERSION DIFFERENCE
17
+
18
+ = usage:
19
+
20
+ require 'mcnamara'
21
+
22
+ use SOC::McNamara, "/usr/local/some_css_dir"
23
+
24
+ All you need to do is have browser overrides for the css you want.
25
+ stylename-<browser lowercase>-<version.major>-<version.minor>-<version.sub>.css
26
+ stylename-<browser lowercase>-<version.major>-<version.minor>.css
27
+ stylename-<browser lowercase>-<version.major>-to-<version.major-1>.css
28
+ stylename-<browser lowercase>-<version.major>-to-<version.major-2>.css
29
+ stylename-<browser lowercase>-<version.major-1>-to-<version.major>.css
30
+ stylename-<browser lowercase>-<version.major-2>-to-<version.major>.css
31
+ stylename-<browser lowercase>-lte-<version.major>.css
32
+ stylename-<browser lowercase>-gte-<version.major>.css
33
+ stylename-<browser lowercase>-gt-<version.major>.css
34
+ stylename-<browser lowercase>-lt-<version.major>.css
35
+ stylename-<browser lowercase>-<version.major>.css
36
+ stylename-<browser lowercase>.css
37
+
38
+ = notes:
39
+
40
+ you can have spy-vs-spy upstream of this middleware but it is not a requirement - McNamara will create its own Spy vs Spy instance as needed and put it into the rack env
41
+
42
+ if you use this middleware then you don't need spy-vs-spy downstream
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rake/testtask'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = "mcnamara"
7
+ s.description = s.summary = "Rack middleware to serve up browser version/platform specific css"
8
+ s.email = "kuccello@gmail.com"
9
+ s.homepage = "http://github.com/kuccello/Mcnamara"
10
+ s.authors = ['Kristan "Krispy" Uccello']
11
+ s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
12
+ # s.version = "0.3"
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ Rake::TestTask.new do |t|
20
+ t.libs << "test"
21
+ t.test_files = FileList['test/*-test.rb']
22
+ t.verbose = true
23
+ end
24
+
25
+ require 'rake/rdoctask'
26
+ desc "Generate documentation"
27
+ Rake::RDocTask.new do |rd|
28
+ rd.main = "README.rdoc"
29
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
30
+ rd.rdoc_dir = 'rdoc'
31
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
data/lib/mcnamara.rb ADDED
@@ -0,0 +1,121 @@
1
+ =begin
2
+ Copyright notice
3
+ =============================================================================
4
+ Copyright (c) 2009 Kristan 'Krispy' Uccello <krispy@soldierofcode.com> - Soldier Of Code
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ ==============================================================================
24
+
25
+ ==============================================================================
26
+ LICENSE
27
+ ==============================================================================
28
+ see LICENSE file for details
29
+
30
+ =end
31
+ require 'rubygems'
32
+ require 'spy_vs_spy'
33
+
34
+ module SoldierOfCode
35
+
36
+ # MAC~nu~Mare-ah
37
+ class McNamara
38
+
39
+ def initialize(app, css_dir)
40
+ @app = app
41
+ @css_dir = css_dir
42
+ end
43
+
44
+ def build_possible_overrides(spy_vs_spy)
45
+
46
+ possible_overrides = []
47
+
48
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}-#{spy_vs_spy.version.minor}-#{spy_vs_spy.version.sub}.css" if spy_vs_spy.version.major && spy_vs_spy.version.minor && spy_vs_spy.version.sub
49
+
50
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}-#{spy_vs_spy.version.minor}.css" if spy_vs_spy.version.major && spy_vs_spy.version.minor
51
+
52
+ # trickier conditionals
53
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}-to-#{spy_vs_spy.version.major.to_i-1}.css" if spy_vs_spy.version.major && (spy_vs_spy.version.major.to_i - 1 > 0)
54
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major.to_i-1}-to-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major && (spy_vs_spy.version.major.to_i - 1 > 0)
55
+
56
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}-to-#{spy_vs_spy.version.major.to_i-2}.css" if spy_vs_spy.version.major && (spy_vs_spy.version.major.to_i - 2 > 0)
57
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major.to_i-2}-to-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major && (spy_vs_spy.version.major.to_i - 2 > 0)
58
+
59
+ # multi version coverage
60
+ possible_overrides << "-#{spy_vs_spy.browser}-gte-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
61
+ possible_overrides << "-#{spy_vs_spy.browser}-lte-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
62
+ possible_overrides << "-#{spy_vs_spy.browser}-gt-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
63
+ possible_overrides << "-#{spy_vs_spy.browser}-lt-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
64
+
65
+ possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
66
+
67
+ possible_overrides << "-#{spy_vs_spy.browser}.css"
68
+
69
+ return possible_overrides
70
+
71
+ end
72
+
73
+ def call(env)
74
+
75
+ begin
76
+
77
+ # look for what css we are after
78
+ request_path = env['REQUEST_PATH']
79
+ if request_path =~ /\.css$/
80
+
81
+ # establish what browser we are dealing with
82
+ # hey! is spy-vs-spy upstream?
83
+ spy_vs_spy = env['soldierofcode.spy-vs-spy']
84
+ unless spy_vs_spy
85
+ http_user_agent = env['HTTP_USER_AGENT']
86
+ env['soldierofcode.spy-vs-spy'] = SpyVsSpy.new(http_user_agent)
87
+ spy_vs_spy = env['soldierofcode.spy-vs-spy']
88
+ end
89
+
90
+ # need a base directory to scann...
91
+ # TODO -- ripe for a cache
92
+ possible_overrides = build_possible_overrides(spy_vs_spy)
93
+
94
+ Dir["#{@css_dir}/**/*.css"].each do |css_file|
95
+ possible_overrides.each do |override|
96
+ request_path_sub = request_path[1..-1].downcase.sub(/\.css/, override)
97
+
98
+ file_of_intrest = request_path_sub.split("/").last
99
+ # puts "#{__FILE__}:#{__LINE__} #{__method__} #{file_of_intrest.downcase} |||| #{css_file.downcase[1..-1]}"
100
+
101
+ if css_file[1..-1].downcase =~ Regexp.new("#{file_of_intrest.downcase}$")
102
+ # puts "#{__FILE__}:#{__LINE__} #{__method__} I'm IN!!"
103
+ # this is the file we want to serve
104
+ css = '/* Oh oo - I droped the bomb */'
105
+ File.open(css_file, "r") {|f| css = f.read} # file is closed when the block is done. TODO -- cache this
106
+ return [200, {"Content-Type"=>"text/css"}, css]
107
+ end
108
+ end
109
+ end
110
+
111
+ end
112
+ rescue => e
113
+ puts "#{__FILE__}:#{__LINE__} #{__method__} #{e} - #{e.backtrace}"
114
+ end
115
+
116
+ @app.call(env)
117
+
118
+ end
119
+ end
120
+ end
121
+ SOC = SoldierOfCode unless SOC
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mcnamara
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Kristan "Krispy" Uccello
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rack middleware to serve up browser version/platform specific css
17
+ email: kuccello@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
25
+ files:
26
+ - Change.log
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - VERSION
31
+ - lib/mcnamara.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/kuccello/Mcnamara
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Rack middleware to serve up browser version/platform specific css
60
+ test_files: []
61
+