Skip to content
Browse files

render about page for robots (#19)

  • Loading branch information...
1 parent 0ebe229 commit d5fbb478856d61682f9bfc6d0681a9c783dfa3de @yannlugrin yannlugrin committed Mar 7, 2012
Showing with 58 additions and 12 deletions.
  1. +1 −0 Gemfile
  2. +1 −0 Gemfile.lock
  3. +17 −0 app/controllers/application_controller.rb
  4. +9 −12 app/controllers/home_controller.rb
  5. +30 −0 app/controllers/timer_controller.rb
View
1 Gemfile
@@ -6,6 +6,7 @@ gem 'thin'
gem 'faye'
gem 'faye-redis'
gem 'migrant'
+gem 'nokogiri'
# Assets dependencies
group :assets do
View
1 Gemfile.lock
@@ -287,6 +287,7 @@ DEPENDENCIES
jquery-rails
migrant
newrelic_rpm
+ nokogiri
pg
pry-rails
pry-remote
View
17 app/controllers/application_controller.rb
@@ -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
View
21 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
View
30 app/controllers/timer_controller.rb
@@ -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)
@@ -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
@@ -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.
Something went wrong with that request. Please try again.