Class: Rack::Deduce::Ingest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/deduce/ingest.rb,
lib/rack/deduce/ingest/helpers.rb,
lib/rack/deduce/ingest/version.rb

Defined Under Namespace

Modules: Helpers Classes: Railtie

Constant Summary collapse

URL =
"//lore.deduce.com/p/collect"
EVENT_URL =

only https

"https://event.deduce.com/p/event"
TIMEOUT =
1.1
VERHASH =
Digest::SHA1.hexdigest("ruby/#{VERSION}")[0..16]
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, site_id, apikey, options = {}) ⇒ Ingest

Returns a new instance of Ingest.

Parameters:

  • site_id (String)

    The site ID provided to you by Deduce

  • apikey (String)

    The apikey provided to you by Deduce

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :ssl (Boolean) — default: true

    Use SSL?



206
207
208
209
# File 'lib/rack/deduce/ingest.rb', line 206

def initialize(app, site_id, apikey, options = {})
  self.class.init(app, site_id, apikey, options)
  self.class
end

Class Attribute Details

.apikeyObject

Returns the value of attribute apikey.



25
26
27
# File 'lib/rack/deduce/ingest.rb', line 25

def apikey
  @apikey
end

.site_idObject

Returns the value of attribute site_id.



25
26
27
# File 'lib/rack/deduce/ingest.rb', line 25

def site_id
  @site_id
end

.sslObject

Returns the value of attribute ssl.



25
26
27
# File 'lib/rack/deduce/ingest.rb', line 25

def ssl
  @ssl
end

.testmodeObject

Returns the value of attribute testmode.



25
26
27
# File 'lib/rack/deduce/ingest.rb', line 25

def testmode
  @testmode
end

Class Method Details

.event(email, ip, event, additional = {}, options = {}) ⇒ String

Returns on error, returns an error message; or nothing on success.

Parameters:

  • email (String)

    The user’s email address

  • ip (String)

    The user’s ip address

  • event (String)

    The event type

  • additional (Hash) (defaults to: {})

    Additional data to send

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    on error, returns an error message; or nothing on success



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rack/deduce/ingest.rb', line 132

def event(email, ip, event, additional = {}, options = {})
  return if limited?
  return "invalid email" unless email_valid? email

  event_data = additional.dup
  event_data[:email] = email  # fixup will hash the email
  event_data[:ip]    = ip
  event_data[:event] = event

  events [event_data], options
end

.events(evts, options = {}) ⇒ String

Returns on error, returns an error message; or nothing on success.

Parameters:

  • event (Array)

    data

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    on error, returns an error message; or nothing on success



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rack/deduce/ingest.rb', line 87

def events(evts, options = {})
  return if limited?

  site_id = @site_id
  apikey  = @apikey
  timeout = options[:timeout] || (TIMEOUT + evts.length / 10)

  post_data  = { site: site_id, apikey: apikey, vers: VERHASH }
  post_data[:backfill] = true if options[:backfill]
  post_data[:testmode] = true if options[:testmode] || @testmode
  post_data[:events]   = evts.map{ |e| fixup_event( e.dup ) }

  uri  = URI(options[:url] || EVENT_URL)
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = (uri.scheme == 'https')
  http.open_timeout = timeout
  http.read_timeout = timeout
  http.ssl_timeout  = timeout

  hreq = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
  hreq.body = post_data.to_json

  p post_data.to_json

  begin
    res = http.request(hreq)
    if res.code != '200'
      raise "#{res.code} - #{res.body}"
    end
  rescue => e
    adjust_fail
    return e
  end
  adjust_ok
  # return nil on success, else return the error
  return nil
end

.html(email, options = {}) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rack/deduce/ingest.rb', line 41

def html(email, options = {})

  site_id = options[:site_id] || @site_id
  use_ssl = @ssl
  use_ssl = options[:ssl] if options.include? :ssl

  user_data = {site: site_id, vers: VERHASH}
  user_data[:testmode] = true if options[:testmode] || @testmode

  if email_valid?(email)
    email = email.strip
    email_lc = email.downcase
    email_uc = email.upcase
    user_data[:ehls1] = Digest::SHA1.hexdigest(email_lc)
    user_data[:ehus1] = Digest::SHA1.hexdigest(email_uc)
    user_data[:ehlm5] = Digest::MD5.hexdigest(email_lc)
    user_data[:ehum5] = Digest::MD5.hexdigest(email_uc)
    user_data[:ehls2] = Digest::SHA256.hexdigest(email_lc)
    user_data[:ehus2] = Digest::SHA256.hexdigest(email_uc)
  end

  url = options[:url]

  if !url
    if use_ssl.nil?
      url = URL
    elsif use_ssl
      url = 'https:' + URL
    else
      url = 'http:'  + URL
    end
  end

  html = <<EOS
<script type="text/javascript">
var dd_info = #{JSON.pretty_generate(user_data)};
</script>
<script type="text/javascript" src="#{url}" async></script>
EOS

  return html
end

.init(app, site_id, apikey, options = {}) ⇒ Object

Parameters:

  • site_id (String)

    The site ID provided to you by Deduce

  • apikey (String)

    The apikey provided to you by Deduce

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :ssl (Boolean) — default: true

    Use SSL?



31
32
33
34
35
36
37
38
39
# File 'lib/rack/deduce/ingest.rb', line 31

def init(app, site_id, apikey, options = {})
  @app = app

  self.site_id      = site_id
  self.apikey       = apikey
  self.ssl          = options[:ssl]
  self.testmode     = options[:testmode]
  self.ssl          = ssl
end