Class: Wuclan::Twitter::Scrape::JsonUserTweetPair

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/wuclan/twitter/scrape/twitter_json_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, moreinfo) ⇒ JsonUserTweetPair



6
7
8
9
10
11
12
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 6

def initialize raw, moreinfo
  self.raw = raw
  self.moreinfo = moreinfo
  fix_raw_user!
  fix_raw_tweet!
  # p ['new', self.class, raw, moreinfo]
end

Instance Attribute Details

#moreinfoObject

Returns the value of attribute moreinfo.



5
6
7
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 5

def moreinfo
  @moreinfo
end

#rawObject

Returns the value of attribute raw.



5
6
7
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 5

def raw
  @raw
end

Instance Method Details

#each {|tweet| ... } ⇒ Object

generate all the contained TwitterXXX objects

Yields:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 22

def each
  return unless healthy?
  if is_partial?
    yield user
  else
    yield user
    yield 
    yield user_style
  end
  yield tweet if tweet
end

#fix_raw_tweet!Object

Standardize the raw tweet hash’s fields for further processing with Wukong



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 86

def fix_raw_tweet!
  return unless raw_tweet
  raw_tweet['id']                     = ModelCommon.zeropad_id(  raw_tweet['id'])
  raw_tweet['created_at']             = ModelCommon.flatten_date(raw_tweet['created_at'])
  raw_tweet['favorited']              = ModelCommon.unbooleanize(raw_tweet['favorited'])
  raw_tweet['truncated']              = ModelCommon.unbooleanize(raw_tweet['truncated'])
  raw_tweet['twitter_user_id']        = ModelCommon.zeropad_id(   raw_user['id'] )
  raw_tweet['in_reply_to_user_id']    = ModelCommon.zeropad_id(  raw_tweet['in_reply_to_user_id'])   unless raw_tweet['in_reply_to_user_id'].blank?   || (raw_tweet['in_reply_to_user_id'].to_i   == 0)
  raw_tweet['in_reply_to_status_id']  = ModelCommon.zeropad_id(  raw_tweet['in_reply_to_status_id']) unless raw_tweet['in_reply_to_status_id'].blank? || (raw_tweet['in_reply_to_status_id'].to_i == 0)
  Wukong.encode_components raw_tweet, 'text', 'in_reply_to_screen_name'
end

#fix_raw_user!Object

Standardize the raw user hash’s fields for further processing with Wukong



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 68

def fix_raw_user!
  return unless raw_user
  raw_user['scraped_at'] = ModelCommon.flatten_date(self.moreinfo['scraped_at'])
  raw_user['created_at'] = ModelCommon.flatten_date(raw_user['created_at'])
  raw_user['id']         = ModelCommon.zeropad_id(  raw_user['id'])
  raw_user['protected']  = ModelCommon.unbooleanize(raw_user['protected'])
  raw_user['profile_background_tile']  = ModelCommon.unbooleanize(raw['profile_background_tile']) unless raw_user['profile_background_tile'].nil?
  Wukong.encode_components raw_user, 'name', 'location', 'description', 'url'
  # There are several users with bogus screen names
  # These we need to **URL encode** -- not XML-encode.
  if raw_user['screen_name'] !~ /\A\w+\z/
    raw_user['screen_name'] = Wukong.encode_str(raw_user['screen_name'], :url)
  end
end

#healthy?Boolean

Extracted JSON should be an array



15
16
17
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 15

def healthy?()
  raw && raw.is_a?(Hash) && (raw_tweet.nil? || raw_tweet.is_a?(Hash))
end

#is_partial?Boolean

Before mid-2009, most calls returned only the fields in TwitterUserPartial. After a mid-2009 API update, most calls return a full user record: TwitterUser, TwitterUserStyle and TwitterUserProfile

This method tries to guess, based on the fields in the raw_user, which it has.



41
42
43
44
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 41

def is_partial?
  p(raw) if !raw_user
  not raw_user.include?('friends_count')
end

#tweetObject



46
47
48
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 46

def tweet
  Tweet.from_hash raw_tweet if raw_tweet
end

#userObject

create TwitterUser object from raw info



51
52
53
54
55
56
57
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 51

def user
  if is_partial?
    TwitterUserPartial.from_hash raw_user
  else
    TwitterUser.from_hash raw_user
  end
end

#user_profileObject



58
59
60
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 58

def 
  TwitterUserProfile.from_hash raw_user
end

#user_styleObject



61
62
63
# File 'lib/wuclan/twitter/scrape/twitter_json_response.rb', line 61

def user_style
  TwitterUserStyle.from_hash raw_user
end