Class: Affilimator

Inherits:
Object
  • Object
show all
Defined in:
lib/affilimator.rb

Overview

To use, call

<%= Affilimator.enable(email, cookie).html_safe %>

in the layout file(s)

Constant Summary collapse

AFFILIMATOR_CONVERT_URL =
"https://api.affilimator.com/v1/convert"
AFFILIMATOR_CANCEL_URL =
"https://api.affilimator.com/v1/cancel"
@@private_key =
nil
@@public_key =
nil
@@logging_proc =
nil
@@minified_js =
nil
@@warned_on_config =
false

Class Method Summary collapse

Class Method Details

.aff_private_keyObject



97
98
99
# File 'lib/affilimator.rb', line 97

def self.aff_private_key
   @@private_key || ENV['AFFILIMATOR_PRIVATE_KEY']
end

.aff_public_keyObject



101
102
103
# File 'lib/affilimator.rb', line 101

def self.aff_public_key
  @@public_key || ENV['AFFILIMATOR_PUBLIC_KEY']
end

.cancel(email) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/affilimator.rb', line 30

def self.cancel(email)
  if configured
    aff_uri = URI.parse(AFFILIMATOR_CANCEL_URL)
    aff_http = Net::HTTP.new(aff_uri.host, aff_uri.port)
    aff_http.use_ssl = true
    aff_post = Net::HTTP::Post.new(aff_uri.request_uri)
    aff_post.set_form_data({"key" => aff_private_key, "email" => email})
    aff_response = aff_http.request(aff_post)
    puts "Affilimator Cancel Response: #{aff_response}"
  end
end

.config(settings) ⇒ Object



77
78
79
80
81
# File 'lib/affilimator.rb', line 77

def self.config(settings)
  @@private_key = settings[:private_key]
  @@public_key = settings[:public_key]
  @@logging_proc = settings[:logger]
end

.configuredObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/affilimator.rb', line 83

def self.configured
  if @@private_key ||ENV['AFFILIMATOR_PRIVATE_KEY'] then
    true
  else
    if not @@warned_on_config
      puts "WARN: Affilimator is not configured for this environment.  If that is not your intent, either "
      puts "WARN: configure it via the Affilimator.config() call or using the AFFILIMATOR_PRIVATE_KEY and "
      puts "WARN: AFFILIMATOR_PUBLIC_KEY environment variables."
      @@warned_on_config = true
    end
    false
  end
end

.convert(email, plan) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/affilimator.rb', line 18

def self.convert(email, plan)
  if configured
    aff_uri = URI.parse(AFFILIMATOR_CONVERT_URL)
    aff_http = Net::HTTP.new(aff_uri.host, aff_uri.port)
    aff_http.use_ssl = true
    aff_post = Net::HTTP::Post.new(aff_uri.request_uri)
    aff_post.set_form_data({"key" => aff_private_key, "email" => email, "plan" => plan})
    aff_response = aff_http.request(aff_post)
    log "Affilimator Convert Response: #{aff_response}"
  end
end

.enable(email = nil, cookies = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/affilimator.rb', line 111

def self.enable email=nil, cookies=nil
  aff_elements = "<script>#{minified_js}</script>"

  if !(aff_public_key.nil? || email.nil?)
    aff_elements += "<script>#{register_js(email,cookies)}</script>"
  end

  aff_elements
end

.log(msg) ⇒ Object



105
106
107
108
109
# File 'lib/affilimator.rb', line 105

def self.log(msg)
  if @@logging_proc then
    @@logging_proc.call(msg)
  end
end

.provision_affiliate(user_email) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/affilimator.rb', line 42

def self.provision_affiliate(user_email)
  if configured
    aff_uri = URI.parse("https://api.affilimator.com/v1/link")
    aff_http = Net::HTTP.new(aff_uri.host, aff_uri.port)
    aff_http.use_ssl = true
    aff_post = Net::HTTP::Post.new(aff_uri.request_uri)
    aff_post.set_form_data({"key" => aff_private_key, "email" => user_email})
    aff_response = aff_http.request(aff_post)
    puts "Affilimator Cancel Response: #{aff_response}"
    body = JSON.parse(aff_response.body)
    body # return a structured object
  end
end

.register_js(email, cookies = nil, include_lib = true, in_script_tag = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/affilimator.rb', line 56

def self.register_js(email, cookies = nil, include_lib = true, in_script_tag = true)
  if configured
    if cookies and cookies[:aflm_st] == "r"
      "" # user is registered, emit nothing
    else
      js = ""

      if include_lib then
        js += minified_js
      end

      js += "Affilimator.register({key: '#{aff_public_key}', email:'#{email}'})"

      if in_script_tag then
        js = "<script type=\"text/javascript\">" + js + "</script>"
      end
      js
    end
  end
end