Class: SoldierOfCode::McNamara

Inherits:
Object
  • Object
show all
Defined in:
lib/mcnamara.rb

Overview

MAC~nu~Mare-ah

Instance Method Summary collapse

Constructor Details

#initialize(app, css_dir) ⇒ McNamara

Returns a new instance of McNamara.



39
40
41
42
# File 'lib/mcnamara.rb', line 39

def initialize(app, css_dir)
  @app = app
  @css_dir = css_dir
end

Instance Method Details

#build_possible_overrides(spy_vs_spy) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mcnamara.rb', line 44

def build_possible_overrides(spy_vs_spy)

  possible_overrides = []

  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

  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

  # trickier conditionals
  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)
  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)

  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)
  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)

  # multi version coverage
  possible_overrides << "-#{spy_vs_spy.browser}-gte-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
  possible_overrides << "-#{spy_vs_spy.browser}-lte-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
  possible_overrides << "-#{spy_vs_spy.browser}-gt-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major
  possible_overrides << "-#{spy_vs_spy.browser}-lt-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major

  possible_overrides << "-#{spy_vs_spy.browser}-#{spy_vs_spy.version.major}.css" if spy_vs_spy.version.major

  possible_overrides << "-#{spy_vs_spy.browser}.css"

  return possible_overrides

end

#call(env) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mcnamara.rb', line 73

def call(env)

  begin
    # establish what browser we are dealing with
    # hey! is spy-vs-spy upstream?
    spy_vs_spy = env['soldierofcode.spy-vs-spy']
    unless spy_vs_spy
      http_user_agent = env['HTTP_USER_AGENT']
      env['soldierofcode.spy-vs-spy'] = SpyVsSpy.new(http_user_agent)
      spy_vs_spy = env['soldierofcode.spy-vs-spy']
    end

    # look for what css we are after
    request_path = env['REQUEST_PATH']
    if request_path =~ /\.css$/

      # need a base directory to scann...
      # TODO -- ripe for a cache
      possible_overrides = build_possible_overrides(spy_vs_spy)

      Dir["#{@css_dir}/**/*.css"].each do |css_file|
        possible_overrides.each do |override|
          request_path_sub = request_path[1..-1].downcase.sub(/\.css/, override)

          file_of_intrest = request_path_sub.split("/").last
#            puts "#{__FILE__}:#{__LINE__} #{__method__} #{file_of_intrest.downcase} |||| #{css_file.downcase[1..-1]}"

          if css_file[1..-1].downcase =~ Regexp.new("#{file_of_intrest.downcase}$")
#              puts "#{__FILE__}:#{__LINE__} #{__method__} I'm IN!!"
            # this is the file we want to serve
            css = 'Oh oo - I droped the bomb'
            File.open(css_file, "r") {|f| css = f.read}
            return [200, {"Content-Type"=>"text/css"}, css]
          end
        end
      end

    end
  rescue => e
    puts "#{__FILE__}:#{__LINE__} #{__method__} #{e} - #{e.backtrace}"
  end

  @app.call(env)      

end