class Rack::PageSpeed::Filters::Base

Attributes

document[R]
options[R]

Public Class Methods

available_filters() click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 17
def available_filters
  @@subclasses
end
inherited(klass) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 13
def inherited klass
  @@subclasses << klass
end
name(_name = nil) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 29
def name _name = nil
  _name ? @name = _name : @name ||= underscore(to_s)
end
new(options = {}) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 8
def initialize options = {}
  @options = options
end
new(options = {}) click to toggle source
Calls superclass method
# File lib/rack/pagespeed/filters/base.rb, line 23
def new options = {}
  options[:store] ? super(options) : raise("#{name} requires :store to be specified.")
end
priority(_number = nil) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 33
def priority _number = nil
  _number ? @priority = _number.to_i : @priority
end
requires_store() click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 21
def requires_store
  instance_eval do
    def new options = {}
      options[:store] ? super(options) : raise("#{name} requires :store to be specified.")
    end
  end
end

Private Class Methods

underscore(word) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 38
def underscore word
  word.split('::').last.
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

Private Instance Methods

content?(node) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 58
def content? node
  content_for(node)[0] == 200
end
content_for(node) click to toggle source

Retrieve the referenced asset through the Rack application

# File lib/rack/pagespeed/filters/base.rb, line 81
def content_for node
  path = path_for node
  return [404, {}, ""] unless path && is_local?(path)
  app = options[:app]
  env = options[:env].dup
  env['PATH_INFO'] = path
  app.call(env)
end
content_only(node) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 62
def content_only node
  status, headers, body = content_for(node)
  return nil if status != 200
  full_body = ""; body.each do |part| full_body << part end
  full_body
end
is_local?(path) click to toggle source

Asset is not local if it has either a scheme (e.g. ‘http:’) or a host (e.g. ‘//google.com/’)

# File lib/rack/pagespeed/filters/base.rb, line 49
def is_local? path
  uri = URI.parse(path)
  uri.scheme.nil? && uri.host.nil?
  rescue URI::BadURIError
    false
  rescue URI::InvalidURIError
    false
end
path_for(node) click to toggle source
# File lib/rack/pagespeed/filters/base.rb, line 69
def path_for node
  case node.name
    when 'script'
      node['src']
    when 'img'
      node['src']
    when 'link'
      node['href']
  end
end