Class: RailsCdnOptimizer::Analyzer
- Inherits:
-
Object
- Object
- RailsCdnOptimizer::Analyzer
- Defined in:
- lib/rails_cdn_optimizer/analyzer.rb
Instance Attribute Summary collapse
-
#asset_path ⇒ Object
readonly
Returns the value of attribute asset_path.
-
#cdn_host ⇒ Object
readonly
Returns the value of attribute cdn_host.
-
#max_asset_size_kb ⇒ Object
readonly
Returns the value of attribute max_asset_size_kb.
Instance Method Summary collapse
-
#analyze ⇒ Object
Analyze all files in asset_path.
-
#generate_json_report(file = "cdn_report.json") ⇒ Object
Generate JSON report.
-
#initialize(asset_path: "app/assets", cdn_host: nil, max_asset_size_kb: 500) ⇒ Analyzer
constructor
A new instance of Analyzer.
Constructor Details
#initialize(asset_path: "app/assets", cdn_host: nil, max_asset_size_kb: 500) ⇒ Analyzer
Returns a new instance of Analyzer.
9 10 11 12 13 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 9 def initialize(asset_path: "app/assets", cdn_host: nil, max_asset_size_kb: 500) @asset_path = Pathname.new(asset_path) @cdn_host = cdn_host @max_asset_size_kb = max_asset_size_kb end |
Instance Attribute Details
#asset_path ⇒ Object (readonly)
Returns the value of attribute asset_path.
7 8 9 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 7 def asset_path @asset_path end |
#cdn_host ⇒ Object (readonly)
Returns the value of attribute cdn_host.
7 8 9 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 7 def cdn_host @cdn_host end |
#max_asset_size_kb ⇒ Object (readonly)
Returns the value of attribute max_asset_size_kb.
7 8 9 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 7 def max_asset_size_kb @max_asset_size_kb end |
Instance Method Details
#analyze ⇒ Object
Analyze all files in asset_path
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 16 def analyze report = [] return report unless asset_path.exist? Find.find(asset_path.to_s) do |file| next unless File.file?(file) size_kb = (File.size(file).to_f / 1024).round(2) suggestion = size_kb > max_asset_size_kb ? "Consider optimizing" : "OK" cdn_header = cdn_host ? "Cache-Control: max-age=31536000, immutable" : "Not configured" report << { path: Pathname.new(file).relative_path_from(asset_path).to_s, size_kb: size_kb, suggestion: suggestion, cdn_header: cdn_header } end report end |
#generate_json_report(file = "cdn_report.json") ⇒ Object
Generate JSON report
40 41 42 43 44 |
# File 'lib/rails_cdn_optimizer/analyzer.rb', line 40 def generate_json_report(file = "cdn_report.json") report = analyze File.write(file, JSON.pretty_generate(report)) file end |