app/models/create_twitter_user_request.rb
# These validation methods (fresh?, no_need_to_import_friendships? and diff.empty?) are not implemented in
# Rails default validation callbacks due to too heavy.
These validation methods (fresh?, no_need_to_import_friendships. and diff.empty?) are not implemented in
Rails default validation callbacks due to too heavy.
These validation methods ( fresh? no_need_to_import_friendships . and diff . empty? ) are not implemented in Rails default validation callbacks due to too heavy .
def build_twitter_user
    latest = TwitterUser.latest_by(uid: uid)
    unless latest
      twitter_user = TwitterUser.build_by(user: fetch_user)
      relations = fetch_relations!(twitter_user)
      twitter_user.build_friends_and_followers(relations[:friend_ids], relations[:follower_ids])
      twitter_user.build_other_relations(relations)
      twitter_user.user_id = user_id
      return twitter_user
    end
    raise RecentlyUpdated if latest.fresh?
    twitter_user = TwitterUser.build_by(user: fetch_user)
    relations = fetch_relations!(twitter_user)
    twitter_user.build_friends_and_followers(relations[:friend_ids], relations[:follower_ids])
    if twitter_user.no_need_to_import_friendships?
      raise CreateTwitterUserRequest::TooManyFriends.new("Already exists. #{user_id}")
    end
    if latest.diff(twitter_user).empty?
      raise CreateTwitterUserRequest::NotChanged.new('Before build')
    end
    twitter_user.build_other_relations(relations)
    twitter_user.user_id = user_id
    twitter_user
  rescue Error => e
    raise
  rescue Twitter::Error::Forbidden => e
    if e.message.start_with? 'To protect our users from spam and other malicious activity, this account is temporarily locked.'
      raise Forbidden
    else
      logger.warn "#{self.class}##{__method__} #{e.class} #{e.message} #{self.inspect}"
      logger.info e.backtrace.join("\n")
      raise
    end
  rescue => e
    logger.warn "#{self.class}##{__method__} #{e.class} #{e.message} #{self.inspect}"
    logger.info e.backtrace.join("\n")
    raise
  end
def build_twitter_user latest = TwitterUser . latest_by ( uid : uid ) unless latest twitter_user = TwitterUser . build_by ( user : fetch_user ) relations = fetch_relations! ( twitter_user ) twitter_user . build_friends_and_followers ( relations [ :friend_ids ] , relations [ :follower_ids ] ) twitter_user . build_other_relations ( relations ) twitter_user . user_id = user_id return twitter_user end raise RecentlyUpdated if latest . fresh? twitter_user = TwitterUser . build_by ( user : fetch_user ) relations = fetch_relations! ( twitter_user ) twitter_user . build_friends_and_followers ( relations [ :friend_ids ] , relations [ :follower_ids ] ) if twitter_user . no_need_to_import_friendships? raise CreateTwitterUserRequest :: TooManyFriends . new ( "Already exists. #{user_id}" ) end if latest . diff ( twitter_user ) . empty? raise CreateTwitterUserRequest :: NotChanged . new ( 'Before build' ) end twitter_user . build_other_relations ( relations ) twitter_user . user_id = user_id twitter_user rescue Error => e raise rescue Twitter :: Error :: Forbidden => e if e . message . start_with? 'To protect our users from spam and other malicious activity, this account is temporarily locked.' raise Forbidden else logger . warn "#{self.class}##{__method__} #{e.class} #{e.message} #{self.inspect}" logger . info e . backtrace . join ( "\n" ) raise end rescue => e logger . warn "#{self.class}##{__method__} #{e.class} #{e.message} #{self.inspect}" logger . info e . backtrace . join ( "\n" ) raise end
These validation methods (fresh?, no_need_to_import_friendships?
These validation methods ( fresh? no_need_to_import_friendships?