Class: BadPigeon::TimelineEntry

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/bad_pigeon/elements/timeline_entry.rb

Overview

Represents an “entry” which is a part of a timeline response. An entry in most cases is a wrapper for either one tweet or a group of connected tweets (e.g. a parent and a reply).

Tweets can be extracted from an entry using (#items) method, which returns an array of tweets as instances of TimelineTweet class.

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertions

extended, included

Constructor Details

#initialize(json) ⇒ TimelineEntry

Returns a new instance of TimelineEntry.



23
24
25
26
27
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 23

def initialize(json)
  @json = json

  assert { json['entryType'] == json['__typename'] }
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



21
22
23
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 21

def json
  @json
end

Instance Method Details

#all_tweetsObject



37
38
39
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 37

def all_tweets
  items.map(&:tweet).compact
end

#componentObject



33
34
35
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 33

def component
  @json['clientEventInfo'] && @json['clientEventInfo']['component']
end

#item_from_content(item_content) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 55

def item_from_content(item_content)
  case item_content['itemType']
  when 'TimelineTweet'
    TimelineTweet.new(item_content)
  when 'TimelineUser'
    nil
  else
    assert("Unknown itemContent type: #{item_content['itemType']}")
    nil
  end
end

#itemsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 41

def items
  case self.type
  when Type::ITEM
    [item_from_content(@json['itemContent'])].compact
  when Type::MODULE
    @json['items'].map { |i| item_from_content(i['item']['itemContent']) }.compact
  when Type::CURSOR
    []
  else
    assert("Unknown entry type: #{type}")
    []
  end
end

#typeObject



29
30
31
# File 'lib/bad_pigeon/elements/timeline_entry.rb', line 29

def type
  @json['entryType']
end