Skip to content

Commit

Permalink
render about page for robots (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannlugrin committed Mar 7, 2012
1 parent 0ebe229 commit d5fbb47
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -6,6 +6,7 @@ gem 'thin'
gem 'faye'
gem 'faye-redis'
gem 'migrant'
gem 'nokogiri'

# Assets dependencies
group :assets do
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -287,6 +287,7 @@ DEPENDENCIES
jquery-rails
migrant
newrelic_rpm
nokogiri
pg
pry-rails
pry-remote
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -3,7 +3,24 @@ class ApplicationController < ActionController::Base

private

#
# Methods
#

# Return instance of Faye client
#
# @return [Faye::Client] Faye client instance
def faye_client
@faye_client ||= env['faye.client']
end

# Return a list of robots user agents strings
#
# @return [Array<String>] array of user agent string
def robots
Rails.cache.fetch('useragents_bots', expires_in: 1.month) do
data = Nokogiri::HTML(Net::HTTP.get_response(URI.parse('http://www.user-agents.org/allagents.xml')).body)
data.xpath('//user-agent/type[contains(text(),"R")]/../string/text() | /user-agent/type[contains(text(),"C")]/../string/text()').map(&:to_s)
end
end
end
21 changes: 9 additions & 12 deletions app/controllers/home_controller.rb
@@ -1,16 +1,13 @@
class HomeController < ApplicationController
def demo
end
def about
render 'about'
end

def presentation
render "presentation", layout: false
end
def presentation
render 'presentation', layout: false
end

def startup
render "startup", layout: false
end

def about
render "about"
end
def startup
render 'startup', layout: false
end
end
30 changes: 30 additions & 0 deletions app/controllers/timer_controller.rb
Expand Up @@ -2,6 +2,8 @@ class TimerController < ApplicationController
respond_to :json, only: [:event]
respond_to :html, only: [:index, :show]

before_filter :check_robots

def index
@timer = Timer.create!
redirect_to timer_url(@timer.url_key)
Expand Down Expand Up @@ -36,6 +38,13 @@ def event

private

#
# Methods
#

# Send event to all registred timer
#
# @return [Boolean] return false if params is invalid
def publish_timer_action(url_key, timer = {})
if !%(start stop reset).include?(timer[:event]) || (timer[:time] && !timer[:time].is_a?(Hash))
false
Expand All @@ -49,4 +58,25 @@ def publish_timer_action(url_key, timer = {})
end
end

#
# Filters
#

# Render about page when visited by a bot
#
# @return [Boolean] return false when a robot is detected
def check_robots
if robots.include?(request.user_agent)
if request.path == root_path
render 'home/about'
else
redirect_to root_url, status: :moved_permanently
end

false
else
true
end
end

end

0 comments on commit d5fbb47

Please sign in to comment.