Skip to content

Instantly share code, notes, and snippets.

@bgswan
Created November 29, 2012 10:52
Show Gist options
  • Save bgswan/4168173 to your computer and use it in GitHub Desktop.
Save bgswan/4168173 to your computer and use it in GitHub Desktop.
class Analytics
ConnectionFailure = Class.new(StandardError)
def self.sync(account)
new(Analytics::Client.new(CC.config[:analytics_api_key])).sync(account)
end
def initialize(analytics_client)
@analytics_client = analytics_client
end
def sync(account)
account.users.each do |user|
create_or_update_user(user)
end
rescue SocketError => ex
raise ConnectionFailure.new(ex.message)
end
private
def create_or_update_user(user)
attributes = user_attributes(user).merge(account_attributes)
analytics_client.create_or_update(attributes)
end
def user_attributes(user)
{
id: user.id,
email: user.email,
account_admin: account.administered_by?(user)
}
end
def account_attributes
@account_attributes ||= {
account_id: account.id,
account_name: account.name,
account_user_count: account.users.count
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment