Created
November 29, 2012 10:52
-
-
Save bgswan/4168173 to your computer and use it in GitHub Desktop.
Alternative refactoring of http://blog.codeclimate.com/blog/2012/11/14/why-ruby-class-methods-resist-refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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