referthis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/referee.rb +11 -0
  2. data/lib/referral.rb +81 -0
  3. data/lib/sms.rb +14 -0
  4. metadata +47 -0
data/lib/referee.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Referee < ActiveRecord::Base
2
+ attr_accessible :endpoint
3
+ has_many :referrals
4
+
5
+ def self.construct(endpoint)
6
+ referee = Referee.new
7
+ referee.endpoint = endpoint
8
+ referee.save
9
+ return referee
10
+ end
11
+ end
data/lib/referral.rb ADDED
@@ -0,0 +1,81 @@
1
+ class Referral < ActiveRecord::Base
2
+ attr_accessible :referee_id, :user_id
3
+
4
+ def self.construct(referee_id)
5
+ referral = Referral.new
6
+ referral.referee_id = referee_id
7
+ referral.user_id = @user_current_id
8
+ referral.save
9
+ return referral
10
+ end
11
+
12
+ def self.generate_referral(endpoints, user_current_id)
13
+ threads = []
14
+ @user_current_id = user_current_id
15
+ if endpoints[:phone_number] != ''
16
+ threads << Thread.new('sms'){
17
+ phone_number = SMS.sieve(endpoints[:phone_number])
18
+ if Referee.exists?(:endpoint => phone_number)
19
+ SMS.send_referral(self.construct(Referee.find_by_endpoint(phone_number.to_s).id))
20
+ else
21
+ SMS.send_referral(self.construct(Referee.construct(phone_number.to_s).id))
22
+ end
23
+ }
24
+ end
25
+ if endpoints[:email_address] != ''
26
+ threads << Thread.new('email'){
27
+ email_address = endpoints[:email_address]
28
+ begin
29
+ if Referee.exists?(:endpoint => email_address)
30
+ Mailer.send_referral(self.construct(Referee.find_by_endpoint(email_address).id)).deliver
31
+ else
32
+ Mailer.send_referral(self.construct(Referee.construct(email_address).id)).deliver
33
+ end
34
+ rescue Net::SMTPSyntaxError
35
+ puts 'Hello'
36
+ end
37
+ }
38
+ end
39
+ threads.each {|thread| thread.join}
40
+ end
41
+
42
+ def self.percent_clicked_through(email=nil, sms=nil)
43
+ if email || sms
44
+ referrals = Referral.all
45
+ if referrals.any?
46
+ count_email = 0
47
+ clicked_through_email = 0
48
+ count_sms = 0
49
+ clicked_through_sms = 0
50
+ for referral in referrals
51
+ if email
52
+ if Referee.find(referral.referee_id).endpoint.include? '@'
53
+ count_email += 1
54
+ clicked_through_email += 1 if referral.visits > 0
55
+ end
56
+ end
57
+ if sms
58
+ if !Referee.find(referral.referee_id).endpoint.include? '@'
59
+ count_sms += 1
60
+ clicked_through_sms += 1 if referral.visits > 0
61
+ end
62
+ end
63
+ end
64
+ puts ((clicked_through_email.to_f / count_email.to_f) * 100).to_s + '%' if email
65
+ puts ((clicked_through_sms.to_f / count_sms.to_f) * 100).to_s + '%' if sms
66
+ puts ((Referral.where('visits > 0').count.to_f / Referral.count.to_f) * 100).to_s + '%'
67
+ else
68
+ puts '0%'
69
+ end
70
+ end
71
+ end
72
+
73
+ def self.resolve_token(token)
74
+ referral_id = Base64::decode64(token)
75
+ if self.exists?(:id => referral_id)
76
+ referral = self.find(referral_id)
77
+ referral.visits = referral.visits + 1
78
+ referral.save
79
+ end
80
+ end
81
+ end
data/lib/sms.rb ADDED
@@ -0,0 +1,14 @@
1
+ class SMS
2
+ @@account_sid = 'AC0b322d7367604e7a852a1d59193738a2'
3
+ @@auth_token = 'c32bcf082cb7cee728a99832858db23b'
4
+ @@client = Twilio::REST::Client.new(@@account_sid, @@auth_token)
5
+ @@account = @@client.account
6
+
7
+ def self.sieve(number)
8
+ return number.to_s.gsub(/\D/, '')
9
+ end
10
+
11
+ def self.send_referral(referral)
12
+ @@account.sms.messages.create(:from => '+18599030353', :to => Referee.find(referral.referee_id).endpoint, :body => '<user_name> has referred you to <app_name>. You might be interested in checking out the following: <url>' + Base64::encode64(referral.id.to_s).chop + '.')
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: referthis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ''
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/referee.rb
21
+ - lib/referral.rb
22
+ - lib/sms.rb
23
+ homepage:
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: ''
47
+ test_files: []