Top Level Namespace

Defined Under Namespace

Modules: Pork

Constant Summary collapse

TWEET_SAVE =
lambda do |tweet_to_save, rules|
  tweet = Tweet.new(:text => tweet_to_save.text, 
                   :time_of_tweet => tweet_to_save.created_at,
                   :to_user_id_search => tweet_to_save.to_user_id,
                   :iso_language_code => tweet_to_save.iso_language_code,
                   :source => tweet_to_save.source,
                   :profile_image_url => tweet_to_save.profile_image_url,
                   :from_user_id_search => tweet_to_save.from_user_id,     
                   :to_user => tweet_to_save.to_user,
                   :from_user  => tweet_to_save.from_user,
                   :status_id  => tweet_to_save.status_id
                    )

  unless rules['tags'].nil?
    rules['tags'].each do |tag_name, tag_value|
      tweet.tag_list << tag_value
    end
  end

  tweet.save
  tweet
  #If not in DB
  #Active record save
  #According to rules 
  #save associations

end
USER_TWEET_SAVE =
lambda do |tweet_to_save, rules|
  tweet = Tweet.new(:text => tweet_to_save.text, 
                   :time_of_tweet => tweet_to_save.created_at,
                   :to_user_id => tweet_to_save.in_reply_to_user_id,
                   :source => tweet_to_save.source,
                   :profile_image_url => tweet_to_save.user.profile_image_url,     
                   :to_user => tweet_to_save.in_reply_to_screen_name,
                   :from_user  => tweet_to_save.user.screen_name,
                   :twitter_account_id => tweet_to_save.user.id,
                   :status_id  => tweet_to_save.id,
                   :truncated => tweet_to_save.truncated
                    )

  unless rules['tags'].nil?
    rules['tags'].each do |tag_name, tag_value|
      tweet.tag_list << tag_value
    end
  end

  tweet.save
  tweet
end
TWITTER_ACCOUNT_SAVE =
lambda do |, rules|
  if .class == Pork::TwitterUser
     = {:twitter_id => .twitter_id,
                                      :screen_name => .twitter_screen_name}
     = TwitterAccount.new()

     = TwitterAccount.find(.twitter_id) rescue nil
                                      
  else
     = {:twitter_id => .id,
                        :name => .name, 
                        :screen_name => .screen_name, 
                        :description => .description, 
                        :location => .location, 
                        :profile_image_url => .profile_image_url, 
                        :url => .url, 
                        :protected => .protected, 
                        :followers_count => .followers_count, 
                        :statuses_count => .statuses_count, 
                        :friends_count => .friends_count, 
                        :profile_background_image_url => .profile_background_image_url, 
                        :profile_background_tile => .profile_background_tile,
                        :favourites_count => .favourites_count, 
                        :time_zone => .time_zone, 
                        :utc_offset => .utc_offset,
                        :time_of_user_creation => .created_at,
                        :profile_background_color => .profile_background_color,
                        :profile_text_color => .profile_text_color,
                        :profile_link_color => .profile_link_color,
                        :profile_sidebar_fill_color => .profile_sidebar_fill_color,
                        :profile_sidebar_border_color => .profile_sidebar_border_color,
                        :notifications => .notifications,
                        :verified => .verified,
                        :twitter_id_for_search => .twitter_id_for_search}    
     = TwitterAccount.new()

     = TwitterAccount.find_by_screen_name(.screen_name)

  end


  if 

     = 
    unless rules['tags'].nil?
      rules['tags'].each do |tag_name, tag_value|
        .tag_list << tag_value
      end
    end
    .update_attributes()  
  else
    unless rules['tags'].nil?
      rules['tags'].each do |tag_name, tag_value|
        .tag_list << tag_value
      end
    end
  end                    
  
  .save
  

end
CALL_SAVE =
lambda do |call_to_save, rules|
  call = Call.new(:query => call_to_save.query,
                  :completed_in =>  call_to_save.completed_in,
                  :since_id => call_to_save.since_id,
                  :max_id => call_to_save.max_id,
                  :refresh_url => call_to_save.refresh_url,
                  :results_per_page => call_to_save.results_per_page,
                  :next_page => call_to_save.next_page,
                  :page => call_to_save.page,
                  :api => call_to_save.api_id)
  unless rules['tags'].nil?
    rules['tags'].each do |tag_name, tag_value|
      call.tag_list << tag_value
    end
  end
  call.save
  call
end
RELATIONSHIP_SAVE =
lambda do |users_to_save, rules|
  follower = users_to_save[:follower]
  friend = users_to_save[:friend]      

  twitter_relationship = TwitterRelationship.new(:follower_id => follower.db_object.id, 
                                                 :friend_id => friend.db_object.id, 
                                                 :current => true)
  if rules[:complete_friend_set]
    twitter_relationship[:complete_friend_set] = rules[:complete_friend_set]
  end
  if rules[:complete_follower_set]
     twitter_relationship[:complete_follower_set] = rules[:complete_follower_set]
  end

  unless rules['tags'].nil?
    rules['tags'].each do |tag_name, tag_value|
      twitter_relationship.tag_list << tag_value
    end
  end
  twitter_relationship.save
  twitter_relationship
end
REACTION_SAVE =
lambda do |reaction_to_save, rules|

  initiator = reaction_to_save[:initiator]
  responder = reaction_to_save[:responder]
  tweet = reaction_to_save[:tweet]
  reaction_type = reaction_to_save[:type]

  tweet_reaction = TweetReaction.new(:tweet_id => tweet.id, 
                                     :reaction_id => Reaction.find_by_reaction_type(reaction_type).id, 
                                     :initiator_id => initiator.id, 
                                     :responder_id => responder.id)

  
  
  unless rules['tags'].nil?
    rules['tags'].each do |tag_name, tag_value|
      tweet_reaction.tag_list << tag_value
    end
  end

  tweet_reaction.save
  tweet_reaction
end
TREND_SAVE =
lambda do |trend_to_save, rules|
  trend = Trend.new(:name => trend_to_save[:name], :query => trend_to_save[:query])
  trend.save
  trend
end
ACCOUNT_INFO =
lambda do |user_object, auth_object|
  $PORK_LOG.write("Pulling User Account info for user: #{user_object.search}") if $PORK_LOG
  @pull_data = auth_object ? auth_object.user(user_object.search) : Twitter.user(user_object.search)  
  {:pull_data => @pull_data, :db_object => $SAVER.save(@pull_data, &TWITTER_ACCOUNT_SAVE)}
end
FOLLOWERS =
lambda do |user, auth_object|
  user.
  rules = {:user_id => user.twitter_id}
  follower_relationship_db_ids = []
  # unless user.desired_follower_count
  #   $SAVER.rules[:complete_follower_set] = true
  # end
  loop do
    rules[:cursor] = -1 if !rules[:cursor]
    if auth_object
      @pull_data = auth_object.followers(rules)
    else
      @url = "http://twitter.com/statuses/followers.json?" 
      rules.each {|k,v| @url << URI.escape("#{k.to_s}=#{v.to_s}&")}
      @url.chop!
      @pull_data = Hashie::Mash.new(JSON.parse(Net::HTTP.get(URI.parse(@url))))    
    end
    @pull_data.users.each do |follower_mash|
      db_user_object = $SAVER.save(follower_mash, &TWITTER_ACCOUNT_SAVE)  
      follower_relationship_db_ids << $SAVER.save({:friend => user, :follower => Pork::TwitterUser.new(:twitter_id => follower_mash.id, :twitter_screen_name => follower_mash.screen_name, :db_object => db_user_object)}, &RELATIONSHIP_SAVE).id 
    end
    if @pull_data.next_cursor == 0
      break
    else
      rules[:cursor] = @pull_data.next_cursor
    end
  end
  {:follower_relationship_db_ids => follower_relationship_db_ids}
end
FOLLOWER_IDS =
lambda do |user, auth_object|
  user.
  $SAVER.rules[:complete_follower_set] = true
  follower_relationship_db_ids = []
  @pull_data = auth_object ? auth_object.follower_ids({:user_id => user.twitter_id}) : Twitter.follower_ids(user.twitter_id)
  @pull_data.each do |user_id| 
    db_user_object = $SAVER.save(Pork::TwitterUser.new(:twitter_id => user_id), &TWITTER_ACCOUNT_SAVE)
    follower_relationship_db_ids << $SAVER.save({:friend => user, 
                                                 :follower => Pork::TwitterUser.new(:twitter_id => user_id, 
                                                                                    :db_object => db_user_object)},
                                                 &RELATIONSHIP_SAVE)
  end
  $SAVER.rules[:complete_follower_set] = false
  {:follower_relationship_db_ids => follower_relationship_db_ids}
end
FRIENDS =
lambda do |user, auth_object|
  user.
  rules = {:user_id => user.twitter_id}
  friend_relationship_db_ids = []
  # unless user.desired_friend_count
  #   $SAVER.rules[:complete_friend_set] = true
  # end
  loop do
    rules[:cursor] = -1 if !rules[:cursor]
    if auth_object
      @pull_data = auth_object.friends(rules)
    else
      @url = "http://twitter.com/statuses/friends.json?" 
      rules.each {|k,v| @url << URI.escape("#{k.to_s}=#{v.to_s}&")}
      @url.chop!
      @pull_data = Hashie::Mash.new(JSON.parse(Net::HTTP.get(URI.parse(@url))))    
    end
    @pull_data.users.each do |friend_mash|
      db_user_object = $SAVER.save(friend_mash, &TWITTER_ACCOUNT_SAVE)  
      friend_relationship_db_ids << $SAVER.save({:friend => Pork::TwitterUser.new(:twitter_id => friend_mash.id, :twitter_screen_name => friend_mash.screen_name, :db_object => db_user_object), :follower => user}, &RELATIONSHIP_SAVE).id
    end
    if @pull_data.next_cursor == 0
      break
    else
      rules[:cursor] = @pull_data.next_cursor
    end
  end
  {:friend_relationship_db_ids => friend_relationship_db_ids}
end
FRIEND_IDS =
lambda do |user, auth_object|
  user.
  $SAVER.rules[:complete_friend_set] = true
  friend_relationship_db_ids = []
  @pull_data = auth_object ? auth_object.friend_ids({:user_id => user.twitter_id}) : Twitter.friend_ids(user.twitter_id)
  @pull_data.each do |user_id| 
    db_user_object = $SAVER.save(Pork::TwitterUser.new(:twitter_id => user_id), &TWITTER_ACCOUNT_SAVE)
    friend_relationship_db_ids << $SAVER.save({:follower => user, :friend => Pork::TwitterUser.new(:twitter_id => user_id, :db_object => db_user_object)}, &RELATIONSHIP_SAVE)
  end
  $SAVER.rules[:complete_friend_set] = false
  {:friend_relationship_db_ids => friend_relationship_db_ids}
end
TWEETS =
lambda do |user, auth_object|
  rules = {:count => 200}
  if user.twitter_id 
    @id = user.twitter_id
    @id_hash = {"user_id" => @id}
  else
    @id = user.twitter_screen_name
    @id_hash = {"screen_name" => @id}
  end
  @tweet_db_ids = []
  loop do 
    @pull_data = auth_object ? auth_object.user_timeline(rules.merge(@id_hash)) : Twitter.timeline(@id,rules)
    @pull_data.each do |result|
      if (user.since_tweet_id ? @pull_data.last.id <= user.since_tweet_id : false)
        break
      else
        @tweet_db_ids << $SAVER.save(result, &USER_TWEET_SAVE).id
      end
    end
    if @pull_data.last.id == rules[:max_id] or (user.since_tweet_id ? @pull_data.last.id <= user.since_tweet_id : false )
      break
    else
      rules[:max_id] = @pull_data.last.id
    end
  end
  # rules[:reactions] ? $REACTION_PROCESSOR.process_reactions(@tweet_db_objects) : nil
  {:db_ids => @tweet_db_ids}
end
lambda do |rules, auth_object|
  Twitter::Trends.current().each do |trend|
    $SAVER.save({:name => trend.name, :query => trend.query}, &TREND_SAVE)
  end
end
SEARCH_CRAWL =
lambda do |user, search_query, count|
  $TWITERATOR.twiterate({:collect_users => true}, {:search_query => search_query}, &SEARCH_ITER)
  # @users.keys
  search_query
end
FOLLOWER_IDS_CRAWL =
lambda do |user, search_query, count|
  $PULLER.pull({:user_id => user, :collect_users => true}, &FOLLOWER_IDS_PULL)
end
FRIEND_IDS_CRAWL =
lambda do |user, search_query, count|
  $PULLER.pull({:user_id => user, :collect_users => true}, &FRIEND_IDS_PULL)
end
FOLLOWERS_CRAWL =
lambda do |user, search_query, count|
  if not user.db_object
    user.db_object = $PULLER.pull({:user => user}, &USER_PULL)
  end
  $TWITERATOR.twiterate({:count => count}, {:collect_users => true, :user => user}, &FOLLOWERS_ITER)    
end
FRIENDS_CRAWL =
lambda do |user, search_query, count|
  if not user.db_object
    user.db_object = $PULLER.pull({:user => user}, &USER_PULL)
  end
  $TWITERATOR.twiterate({ :count => count}, {:collect_users => true, :user => user}, &FRIENDS_ITER)    
end