Class: Referrer
- Inherits:
-
Object
- Object
- Referrer
- Defined in:
- lib/attlib/referrer.rb
Instance Attribute Summary collapse
-
#keywords ⇒ Object
readonly
Returns the value of attribute keywords.
-
#possible_keyword_parameters ⇒ Object
readonly
Returns the value of attribute possible_keyword_parameters.
-
#referrer_url ⇒ Object
readonly
Returns the value of attribute referrer_url.
-
#search_engine ⇒ Object
readonly
Returns the value of attribute search_engine.
Instance Method Summary collapse
- #get_keywords ⇒ Object
-
#initialize(referrer_url) ⇒ Referrer
constructor
A new instance of Referrer.
- #is_search_engine? ⇒ Boolean
- #uri?(string) ⇒ Boolean
Constructor Details
#initialize(referrer_url) ⇒ Referrer
Returns a new instance of Referrer.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/attlib/referrer.rb', line 24 def initialize(referrer_url) # Check if the URI is valid if uri?(referrer_url) @referrer_url = URI(referrer_url) # Check if the referrer is a search engine and if so, assign the values to :search_engine and :keywords # First check if the domain + path matches (e.g. google.co.uk/products) any of the search engines in the lookup hash if $SEARCH_ENGINE_LOOKUP[@referrer_url.host + @referrer_url.path] @search_engine = $SEARCH_ENGINE_LOOKUP[@referrer_url.host + @referrer_url.path]['name'] @possible_keyword_parameters = $SEARCH_ENGINE_LOOKUP[@referrer_url.host + @referrer_url.path]['parameters'] @keywords = get_keywords # If not, check if the domain by itself matches (e.g. google.co.uk) elsif $SEARCH_ENGINE_LOOKUP[@referrer_url.host] @search_engine = $SEARCH_ENGINE_LOOKUP[@referrer_url.host]['name'] @possible_keyword_parameters = $SEARCH_ENGINE_LOOKUP[@referrer_url.host]['parameters'] @keywords = get_keywords # Otherwise referrer is not a search engine else @search_engine = nil @possible_keyword_parameters = nil @keywords = nil end else # Otherwise the URI is not valid raise ArgumentError, "#{referrer_url} is not a valid URL" end end |
Instance Attribute Details
#keywords ⇒ Object (readonly)
Returns the value of attribute keywords.
22 23 24 |
# File 'lib/attlib/referrer.rb', line 22 def keywords @keywords end |
#possible_keyword_parameters ⇒ Object (readonly)
Returns the value of attribute possible_keyword_parameters.
22 23 24 |
# File 'lib/attlib/referrer.rb', line 22 def possible_keyword_parameters @possible_keyword_parameters end |
#referrer_url ⇒ Object (readonly)
Returns the value of attribute referrer_url.
22 23 24 |
# File 'lib/attlib/referrer.rb', line 22 def referrer_url @referrer_url end |
#search_engine ⇒ Object (readonly)
Returns the value of attribute search_engine.
22 23 24 |
# File 'lib/attlib/referrer.rb', line 22 def search_engine @search_engine end |
Instance Method Details
#get_keywords ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/attlib/referrer.rb', line 59 def get_keywords # only get keywords if there's a query string to extract them from... if @referrer_url.query query_parameters = CGI.parse(@referrer_url.query) # try each possible keyword parameter with the query string until one returns a result possible_keyword_parameters.each do | parameter | if query_parameters.has_key?(parameter) return query_parameters[parameter].to_s end end end end |
#is_search_engine? ⇒ Boolean
55 56 57 |
# File 'lib/attlib/referrer.rb', line 55 def is_search_engine? @search_engine end |
#uri?(string) ⇒ Boolean
73 74 75 76 77 78 79 80 |
# File 'lib/attlib/referrer.rb', line 73 def uri?(string) uri = URI.parse(string) %w( http https ).include?(uri.scheme) rescue URI::BadURIError false rescue URI::InvalidURIError false end |