Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
roboto
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-1.9.3-p551
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

9 changes: 8 additions & 1 deletion app/controllers/roboto/robots_controller.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

6 changes: 6 additions & 0 deletions lib/generators/roboto/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/generators/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/roboto.rb
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions roboto.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion spec/generators/roboto/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
69 changes: 65 additions & 4 deletions spec/requests/user_visits_robots_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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