Class: Referral

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/referral.rb

Direct Known Subclasses

ReferThis

Class Method Summary collapse

Class Method Details

.clicked_through_rate(email = false, sms = false, overall = true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/referral.rb', line 35

def self.clicked_through_rate(email=false, sms=false, overall=true)
  clicked_through_rate = Hash.new
  if email || sms
    referrals = Referral.all
    if referrals.any?
      count_email = 0
      clicked_through_email = 0
      count_sms = 0
      clicked_through_sms = 0
      for referral in referrals
        if email
          if Referee.find(referral.referee_id).endpoint.include? '@'
            count_email += 1 
            clicked_through_email += 1 if referral.visits > 0
          end
        end
        if sms
          if !Referee.find(referral.referee_id).endpoint.include? '@'
            count_sms += 1 
            clicked_through_sms += 1 if referral.visits > 0
          end
        end 
      end
      clicked_through_rate['email'] = ((clicked_through_email.to_f / count_email.to_f) * 100) if email
      clicked_through_rate['sms'] = ((clicked_through_sms.to_f / count_sms.to_f) * 100) if sms
    else
      return '0' 
    end
  end
  clicked_through_rate['overall'] = ((Referral.where('visits > 0').count.to_f / Referral.count.to_f) * 100) if overall
  return clicked_through_rate
end

.construct(referee_id) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/referral.rb', line 7

def self.construct(referee_id)
  referral = Referral.new
  referral.referee_id = referee_id
  referral.user_id = @user_current_id
  referral.save
  return referral
end

.generate_referral(endpoints, user_current_id, url, referrer_name, optional) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/referral.rb', line 14

def self.generate_referral(endpoints, user_current_id, url, referrer_name, optional)
  threads = []
  @user_current_id = user_current_id
  if endpoints[:phone_number] != ''
    threads << Thread.new('sms'){
      phone_number = SMS.sieve(endpoints[:phone_number])
      Referee.exists?(:endpoint => phone_number) ? SMS.send_referral(self.construct(Referee.find_by_endpoint(phone_number.to_s).id), url, referrer_name, optional) : SMS.send_referral(self.construct(Referee.construct(phone_number.to_s).id), url, referrer_name, optional)
    }
  end
  if endpoints[:email_address] != ''
    threads << Thread.new('email'){
      email_address = endpoints[:email_address]
      begin
      Referee.exists?(:endpoint => email_address) ? Mailer.send_referral(self.construct(Referee.find_by_endpoint(email_address).id), url, referrer_name, optional).deliver : Mailer.send_referral(self.construct(Referee.construct(email_address).id), url, referrer_name, optional).deliver
      rescue Net::SMTPSyntaxError
        puts 'Hello'
      end
    }
  end
  threads.each {|thread| thread.join}
end

.resolve_token(token) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/referral.rb', line 67

def self.resolve_token(token)
  referral_id = Base64::decode64(token)
  if self.exists?(:id => referral_id)
    referral = self.find(referral_id) 
    referral.visits = referral.visits + 1
    referral.save
  end
end