Module: Gricer::ActiveModel::Request

Included in:
Gricer::ActiveRecord::Request, Mongoid::Request
Defined in:
lib/gricer/active_model/request.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extend the ActiveModel with the statistics class methods.

See Also:



9
10
11
# File 'lib/gricer/active_model/request.rb', line 9

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#agent_header=(agent_header) ⇒ Gricer::Agent

Find or Create Gricer::Agent corrosponding to the given user agent string as given in the HTTP header

Parameters:

  • agent_header (String)

    A user agent string as in a HTTP header

Returns:

  • (Gricer::Agent)


54
55
56
# File 'lib/gricer/active_model/request.rb', line 54

def agent_header=(agent_header)
  self.agent = "Gricer::#{model_type}::Agent".constantize.find_or_create_by_request_header agent_header
end

#ip_address=(ip) ⇒ Object

Store the ip address from which the request was sent for init_session

See Also:

  • #init_session


33
34
35
# File 'lib/gricer/active_model/request.rb', line 33

def ip_address=(ip)
  @ip_address = ip
end

#localeObject



42
43
44
45
46
47
48
# File 'lib/gricer/active_model/request.rb', line 42

def locale
  if locale_minor
    "#{locale_major}-#{locale_minor}"
  else
    locale_major
  end
end

#locale=(locale) ⇒ Object



38
39
40
# File 'lib/gricer/active_model/request.rb', line 38

def locale=(locale)
  self.locale_major, self.locale_minor = locale.try(:to_s).try(:downcase).try(:split, '-')
end

#params=(params) ⇒ Object

Parse the params to fill param_id and format attributes



103
104
105
106
# File 'lib/gricer/active_model/request.rb', line 103

def params=(params)
  self.param_id = params[:id]
  self.format = params[:format]
end

#referer=(referer) ⇒ Object

Parse the referer to fill referer and search engine attributes

Parameters:

  • referer (String)

    The Referer as given in HTTP request



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gricer/active_model/request.rb', line 61

def referer=(referer)
  if referer
    void, self.referer_protocol, self.referer_host, self.referer_path, self.referer_params = referer.match(/([A-Za-z0-9]*):\/\/([^\/]*)([^\?]*)[\?]?(.*)/).to_a
  
    # Sanatize/Normalize referer values
    self.referer_protocol = referer_protocol.try(:upcase)
    self.referer_host = referer_host.try(:downcase)
    self.referer_path = '/' if referer_path.blank?
    self.referer_params = '' if referer_params.blank?
    params = CGI::parse(referer_params) 
  
    # Detect Search Engines
    if referer_host =~ /^www\.google(?:\.com?)?(?:\.[a-z]{2})?$/ and ['/search', '/url'].include? referer_path
      self.search_engine = 'Google'
      self.search_query = params['q'].try(:first)    
    elsif referer_host == 'www.bing.com' and referer_path == '/search'
      self.search_engine = 'Bing'
      self.search_query = params['q'].try(:first)
    elsif referer_host =~ /search\.yahoo\.com$/ and referer_path =~ /\/search;/
      self.search_engine = 'Yahoo'
      self.search_query = params['p'].try(:first)
    elsif referer_host == 'www.baidu.com' and referer_path == '/s'
      self.search_engine = 'Baidu'
      self.search_query = params['wd'].try(:first)
    elsif referer_host =~ /ask\.com$/ and referer_path =~ /^\/web/
      self.search_engine = 'Ask'
      self.search_query = params['q'].try(:first)
    elsif referer_host == 'search.aol.com' and referer_path == '/aol/search'
      self.search_engine = 'AOL'
      self.search_query = params['q'].try(:first)
    elsif referer_host =~ /metacrawler\.com$/ and referer_path =~ /search\/web/
      self.search_engine = 'MetaCrawler'
      self.search_query = params['q'].try(:first)
    elsif referer_host =~ /dogpile\.com/ and referer_path =~ /dogpile\/ws\/results\/Web\//
      self.search_engine = 'Dogpile'
      void, self.search_query = referer_path.match(/ws\/results\/Web\/([^\/]*)\//).to_a
      self.search_query = CGI::unescape(self.search_query).gsub('!FE', '.')
    end
  end
end

#request=(request) ⇒ Object

Parse the Ruby on Rails request to fill attributes



24
25
26
27
28
29
# File 'lib/gricer/active_model/request.rb', line 24

def request=(request)  
  return if request.blank?
  info = Gricer::Parsers::Request.get_info request
  @request_locale = info.delete :req_locale
  self.attributes = info       
end