Skip to content

Instantly share code, notes, and snippets.

@azcoov
Forked from gooley/neglect.rb
Last active December 11, 2015 23:09
Show Gist options
  • Save azcoov/4674969 to your computer and use it in GitHub Desktop.
Save azcoov/4674969 to your computer and use it in GitHub Desktop.
Sample ruby helper method for the Less Neglect Ruby Gem
# TODO - make sure you have the 'lessneglect' gem installed
# https://github.com/lessneglect/lessneglect-ruby
require 'lessneglect'
class Neglect
def self.api
# TODO - replace with your Less Neglect API credentials
@@api ||= LessNeglectApi::Client.new({
:code => "asdfasdf",
:secret => "1234asdfasdf1234"
})
end
def self.log_event(user, event, custom = {})
# there may be reasons why you want to not log events.
# delete or modify these as necessary
return if user.nil?
return if Rails.env == "development"
begin
person = get_person(user)
event[:extras] ||= {}
event[:extras].merge!(custom) unless custom.nil?
api.create_event(person, event)
rescue
puts "error logging to LN"
end
end
def self.log_event_with_target(user, event, target_id, custom = {})
# there may be reasons why you want to not log events.
# delete or modify these as necessary
return if user.nil?
return if Rails.env == "development"
begin
log_event(user, {
:name => event,
:target_id => target_id
}, custom)
rescue
puts "error logging to LN"
end
end
def self.log_event_with_revenue(user, event, revenue, custom = {})
# there may be reasons why you want to not log events.
# delete or modify these as necessary
return if user.nil?
return if Rails.env == "development"
begin
log_event(user, {
:name => event,
:revenue => revenue
}, custom)
rescue
puts "error logging to LN"
end
end
def self.update_person(user)
#You might already have a user and need to update that
#user's attributes
return if user.nil?
return if Rails.env == "development"
begin
person = user_to_person(user)
api.update_person(person)
rescue
puts "error logging to LN"
end
end
def self.user_to_person(user)
# TODO - you should update this to build a full LN Person object
# with the data in your User model
LessNeglectApi::Person.new({
:name => user.name,
:email => user.email,
:external_identifer => user.id,
#example of custom metadata to send along with the user object.
:properties => {
:account_level => user.account_level,
:is_paying => user.paying?,
:created_at => user.created_at.to_i
}
})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment