Class: Redirect

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/model/redirect.rb

Constant Summary collapse

@@host =
'http://localhost:3000/'
@@path =
'a/'
@@append_tracking_info =
false
@@append_google_analytics =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_google_analyticsObject

Gets the value of the flag to determine if google analytics info should be sent with the url



21
22
23
# File 'lib/model/redirect.rb', line 21

def self.append_google_analytics
  @@append_google_analytics
end

.append_google_analytics=(google_analytics_flag) ⇒ Object

Sets the value of the flag to determine if google analytics info should be sent with the url appends utm_source=<@@host>&utm_campaign=<Redirect._id>__<RedirectLog._id>

Parameters:

  • value (Boolean)

    to set append_google_analytics flag to



15
16
17
# File 'lib/model/redirect.rb', line 15

def self.append_google_analytics= google_analytics_flag
  @@append_google_analytics = google_analytics_flag
end

.append_tracking_infoObject

Gets the value of the flag to determine if tracking info should be sent with the url



35
36
37
# File 'lib/model/redirect.rb', line 35

def self.append_tracking_info
  @@append_tracking_info
end

.append_tracking_info=(tracking_info_flag) ⇒ Object

Sets the value of the flag to determine if tracking info should be sent with the url adds the following to the url being redirected to redirect_mongo_id=<Redirect._id>&redirect_log_mongo_id=<RedirectLog._id>

Parameters:

  • value (Boolean)

    to set append_tracking_info flag to



29
30
31
# File 'lib/model/redirect.rb', line 29

def self.append_tracking_info= tracking_info_flag
  @@append_tracking_info = tracking_info_flag
end

.create_shortened_url(original_url) ⇒ Redirect

Creates a shortened url id of 8 characters, creates and saves a redirect object with the

new shortened url id.

Parameters:

  • the (String)

    url to redirect to

Returns:

  • (Redirect)

    a saved redirect object with the newly created shortened url.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/model/redirect.rb', line 68

def self.create_shortened_url(original_url)
  redir = self.new :original_url => original_url
  shortened_id =  SecureRandom.urlsafe_base64(6)
  duplicate_find = Redirect.where(:shortened_id => shortened_id) 
  if duplicate_find and duplicate_find.last and (shortened_id == duplicate_find.last.shortened_id)
    shortened_id = SecureRandom.urlsafe_base64(6)
  end
  redir.shortened_id = shortened_id
  redir.save!
  redir
end

.hostString

Gets the host for the shortened url

Returns:

  • (String)

    the host for the shortened url



60
61
62
# File 'lib/model/redirect.rb', line 60

def self.host
  @@host.end_with?('/') ? @@host : "#{@@host}/"
end

.host=(host) ⇒ Object

Sets the host to use in the shortened url

Parameters:

  • the (String)

    host to use in the shotened url



54
55
56
# File 'lib/model/redirect.rb', line 54

def self.host= host
  @@host = host
end

.pathString

Gets the path for the shortened url

Returns:

  • (String)

    the path



47
48
49
50
# File 'lib/model/redirect.rb', line 47

def self.path
  path = @@path.end_with?('/') ? @@path : "#{@@path}/"
  path.start_with?('/') ? path[1..path.length] : path
end

.path=(path) ⇒ Object

Sets the path to use in the shortened url

Parameters:

  • the (String)

    path you want to use for your shortened url instead of the default a/



41
42
43
# File 'lib/model/redirect.rb', line 41

def self.path= path
  @@path = path
end

Instance Method Details

#shortened_pathString

Returns the shortened url path consisting of the path and shortened id

Returns:

  • (String)

    the shortened path



88
89
90
# File 'lib/model/redirect.rb', line 88

def shortened_path
  "#{self.class.path}#{shortened_id}"
end

#shortened_urlString

Returns the entire shortened url which consists of the host, path and shortened id

Returns:

  • (String)

    the shortened url



82
83
84
# File 'lib/model/redirect.rb', line 82

def shortened_url
  "#{self.class.host}#{self.class.path}#{shortened_id}"
end