Skip to content

Commit d3a07c5

Browse files
Add CssFetcher
1 parent ab0a3e3 commit d3a07c5

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

.rubocop_todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2015-10-01 20:43:37 -0500 using RuboCop version 0.34.1.
3+
# on 2015-10-03 13:02:27 -0500 using RuboCop version 0.34.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 2
9+
# Offense count: 1
1010
# Configuration parameters: AllowURI, URISchemes.
1111
Metrics/LineLength:
1212
Max: 90

lib/critical_path_css/css_fetcher.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module CriticalPathCss
2+
class CssFetcher
3+
require 'phantomjs'
4+
require 'critical_path_css/configuration'
5+
6+
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/../penthouse/penthouse.js"
7+
8+
def initialize(main_css_relative_path)
9+
@main_css_path = "#{Rails.root}/public#{main_css_relative_path}"
10+
@config = Configuration.new
11+
end
12+
13+
def fetch
14+
@config.routes.map { |route| [route, css_for_route(route)] }.to_h
15+
end
16+
17+
private
18+
19+
def css_for_route(route)
20+
Phantomjs.run(PENTHOUSE_PATH, @config.base_url + route, @main_css_path)
21+
end
22+
end
23+
end

lib/critical_path_css_rails.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
module CriticalPathCss
2-
require 'phantomjs'
2+
require 'critical_path_css/css_fetcher'
33

44
CACHE_NAMESPACE = 'critical-path-css'
5-
PENTHOUSE_PATH = "#{File.dirname(__FILE__)}/penthouse/penthouse.js"
6-
7-
def self.generate(main_css_path)
8-
full_main_css_path = "#{Rails.root}/public#{main_css_path}"
9-
config = CriticalPathCss::Configuration.new(Rails.root, Rails.env)
10-
11-
config.routes.each do |route|
12-
css = Phantomjs.run(PENTHOUSE_PATH, config.base_url + route, full_main_css_path)
135

6+
def self.generate(main_css_relative_path)
7+
CssFetcher.new(main_css_relative_path).fetch.each do |route, css|
148
Rails.cache.write(route, css, namespace: CACHE_NAMESPACE)
159
end
1610
end

0 commit comments

Comments
 (0)