Class: Rack::SearchTerms

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack-search_terms.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SearchTerms

Returns a new instance of SearchTerms.



7
8
9
# File 'lib/rack-search_terms.rb', line 7

def initialize app
  @app = app
end

Class Method Details

.versionObject



37
# File 'lib/rack-search_terms.rb', line 37

def self.version; '0.1.0'; end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
# File 'lib/rack-search_terms.rb', line 11

def call env
  @env = env
  @env.merge! 'search_terms' => search_terms
  @app.call @env
end

#refererObject



17
18
19
# File 'lib/rack-search_terms.rb', line 17

def referer
  @env['HTTP_REFERER']
end

#search_termsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack-search_terms.rb', line 21

def search_terms
  case referer 
  when /^http:\/\/www\.google\..*?\/imgres/
    prev = terms_from_param 'prev'
    parse_query(prev.split('?')[1])['q']
  when /^http:\/\/www\.google\..*?\//
    terms_from_param 'q'
  when /^http:\/\/www.bing.com/
    terms_from_param 'q'
  end
end

#terms_from_param(param) ⇒ Object



33
34
35
# File 'lib/rack-search_terms.rb', line 33

def terms_from_param param
  parse_query(URI.parse(referer).query)[param]
end