From 386c8f15e447e5348910bc8c04d3cc4214cd559a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Rodr=C3=ADguez?= Date: Thu, 19 Mar 2015 14:52:25 +0000 Subject: [PATCH 1/2] Changed .rvmrc file for .ruby-gemset and .ruby-version as suggested by RVM --- .ruby-gemset | 1 + .ruby-version | 1 + .rvmrc | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .ruby-gemset create mode 100644 .ruby-version delete mode 100644 .rvmrc diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..f945e01 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +roboto diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..39031fa --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-1.9.3-p551 diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index 1ca1351..0000000 --- a/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm use 1.9.3@roboto --create From 3d21135beb254cbbb44c67edfd9ce168bdae3fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Rodr=C3=ADguez?= Date: Thu, 19 Mar 2015 14:52:41 +0000 Subject: [PATCH 2/2] Added support for HTTP caching configuration --- app/controllers/roboto/robots_controller.rb | 9 ++- lib/generators/roboto/install_generator.rb | 6 ++ lib/generators/templates/initializer.rb | 13 ++++ lib/roboto.rb | 9 +++ roboto.gemspec | 2 + spec/dummy/config/environments/test.rb | 2 +- .../roboto/install_generator_spec.rb | 11 ++- spec/requests/user_visits_robots_spec.rb | 69 +++++++++++++++++-- 8 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 lib/generators/templates/initializer.rb diff --git a/app/controllers/roboto/robots_controller.rb b/app/controllers/roboto/robots_controller.rb index 2c7f46e..d25a15f 100644 --- a/app/controllers/roboto/robots_controller.rb +++ b/app/controllers/roboto/robots_controller.rb @@ -1,5 +1,7 @@ module Roboto class RobotsController < Roboto::ApplicationController + after_action :set_cache_headers, if: -> { Roboto.configuration.cache } + def show render :text => robot_contents, :layout => false, @@ -26,7 +28,12 @@ def default_robots end end - + private + def set_cache_headers + if !Roboto.configuration.cache_only_in_production || Rails.env.production? + expires_in Roboto.configuration.cache_expires_in, public: Roboto.configuration.cache_public + end + end end end diff --git a/lib/generators/roboto/install_generator.rb b/lib/generators/roboto/install_generator.rb index 58b0011..2d83374 100644 --- a/lib/generators/roboto/install_generator.rb +++ b/lib/generators/roboto/install_generator.rb @@ -20,6 +20,12 @@ def copy_locale end end + def copy_initializer + unless FileTest.exists?("config/initializers/roboto.rb") + template "initializer.rb", "config/initializers/roboto.rb" + end + end + def add_roboto_route route "mount_roboto" end diff --git a/lib/generators/templates/initializer.rb b/lib/generators/templates/initializer.rb new file mode 100644 index 0000000..5ad3337 --- /dev/null +++ b/lib/generators/templates/initializer.rb @@ -0,0 +1,13 @@ +Roboto.configure do |config| + # Enables cache + config.cache = false + + # Set max-age of the cache headers + config.cache_expires_in = 1.year + + # Enable if you only want to cache in production environment + config.cache_only_in_production = false + + # Set HTTP cache headers to 'public' + config.cache_public = false +end \ No newline at end of file diff --git a/lib/roboto.rb b/lib/roboto.rb index 38afe24..e1d9a53 100644 --- a/lib/roboto.rb +++ b/lib/roboto.rb @@ -1,8 +1,17 @@ +require 'gem_config' require 'roboto/version' require 'roboto/engine' require 'roboto/content_provider' #container for Roboto related functions module Roboto + include GemConfig::Base + + with_configuration do + has :cache, classes: [TrueClass, FalseClass], default: false + has :cache_expires_in, classes: Float, default: 1.year + has :cache_only_in_production, classes: [TrueClass, FalseClass], default: false + has :cache_public, classes: [TrueClass, FalseClass], default: false + end end diff --git a/roboto.gemspec b/roboto.gemspec index 8d72fc1..8a7801b 100644 --- a/roboto.gemspec +++ b/roboto.gemspec @@ -28,5 +28,7 @@ Gem::Specification.new do |gem| #we need this for the dummy app gem.add_development_dependency 'sqlite3' + + gem.add_runtime_dependency 'gem_config' end diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 99a51c5..b586a56 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -8,7 +8,7 @@ config.cache_classes = true # Configure static asset server for tests with Cache-Control for performance - config.serve_static_assets = true + config.serve_static_files = true config.static_cache_control = "public, max-age=3600" # Show full error reports and disable caching diff --git a/spec/generators/roboto/install_generator_spec.rb b/spec/generators/roboto/install_generator_spec.rb index e02b64a..6a7e6de 100644 --- a/spec/generators/roboto/install_generator_spec.rb +++ b/spec/generators/roboto/install_generator_spec.rb @@ -6,7 +6,7 @@ before {prepare_destination} - describe 'presence of roboto configuration file' do + describe 'presence of roboto configuration files' do before do @env_availabe = ["roboto_env", "staging", "production"] create_fake_env @@ -28,6 +28,15 @@ it { should exist } it { should contain "mount_roboto" } end + + describe 'config/initializers/roboto.rb' do + subject { file('config/initializers/roboto.rb') } + it { should exist } + it { should contain "config.cache" } + it { should contain "config.cache_only_in_production" } + it { should contain "config.cache_expires_in" } + it { should contain "config.cache_public" } + end end def create_routes_rb diff --git a/spec/requests/user_visits_robots_spec.rb b/spec/requests/user_visits_robots_spec.rb index a68bba1..4d09ab4 100644 --- a/spec/requests/user_visits_robots_spec.rb +++ b/spec/requests/user_visits_robots_spec.rb @@ -6,9 +6,70 @@ So I know what to crawl and what not to crawl } do - scenario "visit robots.txt" do - visit "/robots.txt" - page.status_code.should eql(200) + feature 'with default config' do + scenario "visit robots.txt" do + visit "/robots.txt" + page.status_code.should eql(200) + expect(page.response_headers).to include("Cache-Control") + expect(page.response_headers["Cache-Control"]).to eq(disabled_cache_response) + end + end + + feature "with cache enabled and default config" do + background do + Roboto.configure do |config| + config.cache = true + end + end + + scenario "visit robots.txt" do + visit "/robots.txt" + page.status_code.should eql(200) + expect(page.response_headers).to include("Cache-Control") + expect(page.response_headers["Cache-Control"]).to eq(cache_response_sample) + end + end + + feature "with cache enabled and alternative config" do + background do + Roboto.configure do |config| + config.cache = true + config.cache_expires_in = 10.years + config.cache_public = true + end + end + + scenario "visit robots.txt" do + visit "/robots.txt" + page.status_code.should eql(200) + expect(page.response_headers).to include("Cache-Control") + expect(page.response_headers["Cache-Control"]).to eq(cache_response_sample(10.years, true)) + end end -end + feature "with cache enabled but only in production" do + background do + Roboto.configure do |config| + config.cache = true + config.cache_expires_in = 10.years + config.cache_public = true + config.cache_only_in_production = true + end + end + + scenario "visit robots.txt with cache true and alternative config" do + visit "/robots.txt" + page.status_code.should eql(200) + expect(page.response_headers).to include("Cache-Control") + expect(page.response_headers["Cache-Control"]).to eq(disabled_cache_response) + end + end + + def cache_response_sample(max_age=1.year, public=false) + "max-age=#{max_age.to_i}, #{public ? 'public' : 'private'}" + end + + def disabled_cache_response + "max-age=0, private, must-revalidate" + end +end