class RailsAngularSeo::Middleware

Attributes

base_path[RW]
phantomjs_options[RW]
phantomjs_path[RW]
seo_id[RW]
server_name[RW]

Public Class Methods

new(app) click to toggle source
# File lib/rails_angular_seo/middleware.rb, line 17
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rails_angular_seo/middleware.rb, line 21
def call(env)
  if will_render?(env)
    self.class.server_name ||= env["HTTP_HOST"]
    self.class.phantomjs_path ||= 'phantomjs'
    self.class.phantomjs_options ||= ["--ssl-protocol=any"]
    path = URI("#{env["rack.url_scheme"]}://#{self.class.server_name}#{env["REQUEST_URI"]}")
    path_without_port = "#{env["rack.url_scheme"]}://#{path.host}#{env["REQUEST_URI"]}"
    RailsAngularSeo::Renderer.render(self.class.phantomjs_path, self.class.seo_id, path_without_port, {phantomjs_options: self.class.phantomjs_options})
  else
    @app.call(env)
  end
end

Private Instance Methods

is_bot?(env) click to toggle source
# File lib/rails_angular_seo/middleware.rb, line 40
def is_bot?(env)
  [
      "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
      "Googlebot/2.1 (+http://www.google.com/bot.html)",
      "compatible; Mediapartners-Google/2.1; +http://www.google.com/bot.html",
      "AdsBot-Google (+http://www.google.com/adsbot.html)",
      "Mediapartners-Google",
      "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
      "Mozilla/5.0 (compatible; bingbot/2.0 +http://www.bing.com/bingbot.htm)",
      "Baiduspider+(+http://www.baidu.com/search/spider_jp.html)",
      "Baiduspider+(+http://www.baidu.com/search/spider.htm)",
      "BaiDuSpider"
  ].include?(env["HTTP_USER_AGENT"])
end
is_renderable?(env) click to toggle source
# File lib/rails_angular_seo/middleware.rb, line 55
def is_renderable?(env)
  path = env["PATH_INFO"]
  content_type = path.index(".") && path.split(".").last

  path.start_with?(self.class.base_path) & [nil, "htm", "html"].include?(content_type) && env["REQUEST_METHOD"] == "GET"
end
will_render?(env) click to toggle source
# File lib/rails_angular_seo/middleware.rb, line 36
def will_render?(env)
  is_bot?(env) && is_renderable?(env)
end