Class: Rack::RackSeo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-seo/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options, &block) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
# File 'lib/rack-seo/base.rb', line 8

def initialize app, options, &block
  @app = app
  if options[:config]
    @config = YAML.load(IO.read(options[:config]))
  else
    @config = YAML.load(IO.read("config/rack_seo.default.yml"))
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/rack-seo/base.rb', line 4

def config
  @config
end

#current_pathObject

Returns the value of attribute current_path.



5
6
7
# File 'lib/rack-seo/base.rb', line 5

def current_path
  @current_path
end

#dispatcherObject

Returns the value of attribute dispatcher.



6
7
8
# File 'lib/rack-seo/base.rb', line 6

def dispatcher
  @dispatcher
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack-seo/base.rb', line 17

def call env
  # Setup document body ready to process
  status, headers, response = @app.call(env)
  return [status, headers, response] unless headers['Content-Type'] =~ /html/
  body = ""; response.each do |part| body << part end

  document = Rack::RackSeo::Document.new(body)
  current_path = env['PATH_INFO'] || '/'
  execute!(document, current_path)

  body = document.to_html
  headers['Content-Length'] = body.length.to_s if headers['Content-Length'] # still UTF-8 unsafe
  [status, headers, [body]]        
end

#execute!(document, current_path = '/') ⇒ Object



32
33
34
35
36
37
# File 'lib/rack-seo/base.rb', line 32

def execute!(document, current_path = '/')
  @dispatcher = RackSeo::Dispatcher.new(@config, current_path)
  set_meta_title(document, @dispatcher.title_format)
  set_meta_description(document, @dispatcher.description_selector)
  set_meta_keywords(document, @dispatcher.keywords_selector)
end

#set_meta_description(document, description_selector) ⇒ Object



45
46
47
48
# File 'lib/rack-seo/base.rb', line 45

def set_meta_description(document, description_selector)
  content = Rack::RackSeo::Summarizer.extract_description(document, description_selector)
  document.description_content = content
end

#set_meta_keywords(document, keywords_selector) ⇒ Object



50
51
52
53
# File 'lib/rack-seo/base.rb', line 50

def set_meta_keywords(document, keywords_selector)
  content = Rack::RackSeo::Summarizer.extract_keywords(document, keywords_selector)
  document.keywords_content = content
end

#set_meta_title(document, title_format) ⇒ Object



39
40
41
42
43
# File 'lib/rack-seo/base.rb', line 39

def set_meta_title(document, title_format)
  content = Rack::RackSeo::TitleFormatter.parse_meta_title(document, title_format)
  content = Rack::RackSeo::Sanitize.sanitize_meta_title(content)
  document.title_content = content
end