Class: Pork::ReactionProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/pork_sandwich/reaction_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, rules = {:pull_secondary_influence_user_info => false}) ⇒ ReactionProcessor

Returns a new instance of ReactionProcessor.



4
5
6
7
8
# File 'lib/pork_sandwich/reaction_processor.rb', line 4

def initialize(user, rules = {:pull_secondary_influence_user_info => false})
  @rules = rules
  @user = user
  
end

Instance Attribute Details

#rulesObject

Returns the value of attribute rules.



3
4
5
# File 'lib/pork_sandwich/reaction_processor.rb', line 3

def rules
  @rules
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/pork_sandwich/reaction_processor.rb', line 3

def user
  @user
end

Instance Method Details

#fill_in_user_infoObject



118
119
120
121
122
# File 'lib/pork_sandwich/reaction_processor.rb', line 118

def 
  if not user.db_object
    user.db_object = $PULLER.pull({:user=>user}, &USER_PULL)[:db_object]
  end
end

#find_or_create_secondary_account(screen_name) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pork_sandwich/reaction_processor.rb', line 124

def (screen_name)
   = TwitterAccount.find_by_screen_name(screen_name)
  unless   
    if rules[:pull_secondary_influence_user_info]
       = $PULLER.pull({:user => Pork::TwitterUser.new(:twitter_screen_name => screen_name)})[:db_object]
    else
       = TwitterAccount.create(:screen_name => screen_name)
    end
  end
  
end

#parse_tweet_for_influentials(tweet) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pork_sandwich/reaction_processor.rb', line 86

def parse_tweet_for_influentials(tweet) 
  mention_regex = /[@]\w+/
  reply_regex = /^[@]\w+/
  rt_regex = /(^[Rr][Tt] ?[@]\w+(:?)| [Rr][Tt] ?[@]\w+(:?))/  
  t_copy = tweet.text.dup
  reply_screen_name = t_copy.scan(reply_regex)
  reply_screen_name.each do |ru|
    t_copy.slice!(/^#{ru}/)
  end
  rt_screen_names = t_copy.scan(rt_regex)
  unless rt_screen_names.empty?
    rt_screen_names.map! do |rt| rt.first end
  end
  rt_screen_names.each do |rtu|
    t_copy.slice!(/^#{rtu}| #{rtu}/)
  end
  mention_screen_names = t_copy.scan(mention_regex)
  sanitize_screen_names(reply_screen_name.first, rt_screen_names, mention_screen_names)
end

#process_reactionsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pork_sandwich/reaction_processor.rb', line 10

def process_reactions()  
  if rules[:mention_from] or rules[:reply_from] or rules[:rt_from]
    if user.twitter_id
      tweet_ids = $TWITERATOR.twiterate({}, {:user_id => user.twitter_id}, &USER_TWEETS_ITER)[:tweets]
    else
      tweet_ids = $TWITERATOR.twiterate({}, {:screen_name => user.twitter_screen_name}, &USER_TWEETS_ITER)[:tweets]        
    end 
    if rules[:rt_from]
      $TWITERATOR.twiterate({}, {:from => user.twitter_screen_name, :search_query => "RT"}, &SEARCH_ITER)[:tweets].each do |tweet_id|
        tweet_ids << tweet_id
      end
    end
    tweets_from_tweet_ids(tweet_ids).each do |tweet|
      influentials = parse_tweet_for_influentials(tweet)
      if rules[:mention_from] 
        influentials[:mention_screen_names].each do |screen_name|
          save_from(screen_name, tweet, 'mention')
        end
      end
      if rules[:reply_from] 
        save_from(influentials[:reply_screen_name], tweet, 'reply')
      end
      if rules[:rt_from]
        influentials[:rt_screen_names].each do |screen_name|
          save_from(screen_name, tweet, 'retweet')
        end
        pull_and_save_rts_from
      end
    end
  end
  if rules[:mention_to] or rules[:reply_to] or rules[:rt_to]
    tweet_ids = []
    $TWITERATOR.twiterate({}, {:search_query => "#{user.twitter_screen_name}"}, &SEARCH_ITER)[:tweets].each do |tweet_id|
      tweet_ids << tweet_id
    end
    tweets_from_tweet_ids(tweet_ids).each do |tweet|
      source_user_screen_name = tweet.from_user
      influentials = parse_tweet_for_influentials(tweet)
      if rules[:mention_to]
        influentials[:mention_screen_names].each do |screen_name|
          if screen_name == user.twitter_screen_name
            save_to(source_user_screen_name, tweet, 'mention')
          end
        end
      end
      if rules[:reply_to]
        if influentials[:reply_screen_name] == user.twitter_screen_name
          save_to(source_user_screen_name, tweet, 'reply')
        end
      end
      if rules[:rt_to]
        influentials[:rt_screen_names].each do |screen_name|
          if screen_name == user.twitter_screen_name
            save_to(source_user_screen_name, tweet, 'retweet')
          end
        end
      end
    end
  end
end

#pull_and_save_rts_fromObject



136
137
138
# File 'lib/pork_sandwich/reaction_processor.rb', line 136

def pull_and_save_rts_from

end

#sanitize_screen_names(reply_screen_name, rt_screen_names, mention_screen_names) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pork_sandwich/reaction_processor.rb', line 106

def sanitize_screen_names(reply_screen_name, rt_screen_names, mention_screen_names)
    sanitizing_regex = / ?([rR][tT])? ?@/
    reply_screen_name ? reply_screen_name.slice!(sanitizing_regex) : nil
    rt_screen_names.each do |rtu| 
      rtu.slice!(sanitizing_regex) 
    end
    mention_screen_names.each do |mu| 
      mu.slice!(sanitizing_regex) 
    end
    {:reply_screen_name => reply_screen_name, :rt_screen_names => rt_screen_names, :mention_screen_names => mention_screen_names}
end

#save_from(secondary_screen_name, tweet, type) ⇒ Object



72
73
74
# File 'lib/pork_sandwich/reaction_processor.rb', line 72

def save_from(secondary_screen_name, tweet, type)
  $SAVER.save({:initiator => user.db_object, :responder => (secondary_screen_name), :tweet => tweet, :type => type}, &REACTION_SAVE)
end

#save_to(secondary_screen_name, tweet, type) ⇒ Object



76
77
78
# File 'lib/pork_sandwich/reaction_processor.rb', line 76

def save_to(secondary_screen_name, tweet, type)
   $SAVER.save({:initiator => (secondary_screen_name), :responder => user.db_object, :tweet => tweet, :type => type}, &REACTION_SAVE)
end

#tweets_from_tweet_ids(tweet_ids) ⇒ Object



80
81
82
# File 'lib/pork_sandwich/reaction_processor.rb', line 80

def tweets_from_tweet_ids(tweet_ids)
  tweets = tweet_ids.map do |tweet_id| Tweet.find(tweet_id) end
end