Class: BadPigeon::Tweet

Inherits:
Object
  • Object
show all
Extended by:
Assertions
Includes:
Assertions
Defined in:
lib/bad_pigeon/models/tweet.rb

Overview

A model that represents one tweet with an interface matching that from the original ‘twitter` Ruby gem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

extended, included

Constructor Details

#initialize(json) ⇒ Tweet

Returns a new instance of Tweet.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bad_pigeon/models/tweet.rb', line 32

def initialize(json)
  case json['__typename']
  when 'Tweet'
    @json = json
  when 'TweetWithVisibilityResults'
    @json = json['tweet']
  else
    assert("Unexpected tweet record type: #{json['__typename']}")
    @json = json['tweet']
  end
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



18
19
20
# File 'lib/bad_pigeon/models/tweet.rb', line 18

def json
  @json
end

Class Method Details

.from_result(json) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bad_pigeon/models/tweet.rb', line 20

def self.from_result(json)
  case json['__typename']
  when 'Tweet', 'TweetWithVisibilityResults'
    Tweet.new(json)
  when nil, 'TweetUnavailable', 'TweetTombstone'
    nil
  else
    assert("Unknown tweet result type: #{json['__typename']}")
    nil
  end
end

Instance Method Details

#attrsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bad_pigeon/models/tweet.rb', line 92

def attrs
  user = json['core']['user_results']['result']

  fields = {
    id: id,
    source: json['source'],
    text: text,
    truncated: false,
  }

  legacy.each do |k, v|
    next if ['retweeted_status_result', 'quoted_status_result'].include?(k)
    fields[k.to_sym] = v
  end

  user_fields = {
    id: user['rest_id'].to_i,
    id_str: user['rest_id'],
  }

  user['legacy'].each do |k, v|
    next if k =~ /^profile_\w+_extensions/
    user_fields[k.to_sym] = v
  end

  fields[:user] = StrictHash[user_fields]

  if quoted_status?
    fields[:quoted_status] = quoted_status.attrs
  elsif retweeted_status?
    fields[:retweeted_status] = retweeted_status.attrs
  end

  StrictHash[fields]
end

#created_atObject



56
57
58
# File 'lib/bad_pigeon/models/tweet.rb', line 56

def created_at
  Time.parse(legacy['created_at'])
end

#idObject



48
49
50
# File 'lib/bad_pigeon/models/tweet.rb', line 48

def id
  legacy['id_str'].to_i
end

#legacyObject



44
45
46
# File 'lib/bad_pigeon/models/tweet.rb', line 44

def legacy
  json['legacy']
end

#quoted_statusObject Also known as: quoted_tweet



80
81
82
83
# File 'lib/bad_pigeon/models/tweet.rb', line 80

def quoted_status
  qsr = json['quoted_status_result']
  qsr && qsr['result'] && Tweet.from_result(qsr['result'])
end

#quoted_status?Boolean Also known as: quoted_tweet?

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/bad_pigeon/models/tweet.rb', line 74

def quoted_status?
  # there is also legacy['is_quote_status'], but it may be true while quoted_status_result
  # is not set if the quoted status was deleted
  !!quoted_status
end

#retweet?Boolean Also known as: retweeted_status?

Returns:

  • (Boolean)


64
65
66
# File 'lib/bad_pigeon/models/tweet.rb', line 64

def retweet?
  !!legacy['retweeted_status_result']
end

#retweeted_statusObject



70
71
72
# File 'lib/bad_pigeon/models/tweet.rb', line 70

def retweeted_status
  legacy['retweeted_status_result'] && Tweet.from_result(legacy['retweeted_status_result']['result'])
end

#textObject



60
61
62
# File 'lib/bad_pigeon/models/tweet.rb', line 60

def text
  legacy['full_text']
end

#urlsObject



88
89
90
# File 'lib/bad_pigeon/models/tweet.rb', line 88

def urls
  legacy['entities']['urls'].map { |u| URLEntity.new(u) }
end

#userObject



52
53
54
# File 'lib/bad_pigeon/models/tweet.rb', line 52

def user
  User.new(json['core']['user_results']['result'])
end