Class: BadPigeon::TimelineInstruction

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

Overview

Represents an “instruction” which is a part of a timeline response. An instruction can be e.g. pinning one entry to the top or adding some number of entries to the list view.

A timeline includes one or more “entries” (TimelineEntry), most of which contain one or more tweets.

Defined Under Namespace

Modules: Type

Instance Method Summary collapse

Methods included from Assertions

extended, included

Constructor Details

#initialize(json, timeline_class) ⇒ TimelineInstruction

Returns a new instance of TimelineInstruction.



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

def initialize(json, timeline_class)
  @json = json
  @timeline_class = timeline_class
end

Instance Method Details

#entriesObject



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
# File 'lib/bad_pigeon/elements/timeline_instruction.rb', line 29

def entries
  case type
  when Type::ADD_ENTRIES
    expected_keys = ['entries', 'type']
    entries = @json['entries']

    case @timeline_class
    when ListTimeline
      expected_types = [TimelineEntry::Type::ITEM, TimelineEntry::Type::CURSOR]
    else
      expected_types = [TimelineEntry::Type::ITEM, TimelineEntry::Type::MODULE, TimelineEntry::Type::CURSOR]
    end

  when Type::CLEAR_CACHE
    expected_keys = ['type']
    entries = []

  when Type::PIN_ENTRY
    expected_keys = ['entry', 'type']
    entries = [@json['entry']]
    expected_types = [TimelineEntry::Type::ITEM]

  else
    assert("Unknown timeline instruction type: #{type}")
    return []
  end

  assert { @json.keys.sort == expected_keys }
  assert { entries.all? { |e| expected_types.include?(e['content']['entryType']) }}
  assert { entries.all? { |e| e.keys.sort == ['content', 'entryId', 'sortIndex'] }}

  entries.map { |e| TimelineEntry.new(e['content']) }
end

#typeObject



25
26
27
# File 'lib/bad_pigeon/elements/timeline_instruction.rb', line 25

def type
  @json['type']
end