Class: Twitter::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/twitter/cursor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#threaded_map

Constructor Details

#initialize(attrs, collection_name, klass, client, method_name, method_options) ⇒ Twitter::Cursor

Initializes a new Cursor

Parameters:

  • attrs (Hash)
  • collection_name (String, Symbol)

    The name of the method to return the collection

  • klass (Class)

    The class to instantiate object in the collection

  • client (Twitter::Client)
  • method_name (String, Symbol)
  • method_options (Hash)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/twitter/cursor.rb', line 31

def initialize(attrs, collection_name, klass, client, method_name, method_options)
  @attrs = attrs
  @client = client
  @method_name = method_name
  @method_options = method_options
  @collection = Array(attrs[collection_name.to_sym]).map do |item|
    if klass
      klass.fetch_or_new(item)
    else
      item
    end
  end
  class_eval do
    alias_method(collection_name.to_sym, :collection)
  end
end

Instance Attribute Details

#attrsObject (readonly) Also known as: to_hash

Returns the value of attribute attrs.



6
7
8
# File 'lib/twitter/cursor.rb', line 6

def attrs
  @attrs
end

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/twitter/cursor.rb', line 6

def collection
  @collection
end

Class Method Details

.from_response(response, collection_name, klass, client, method_name, method_options) ⇒ Twitter::Cursor

Initializes a new Cursor object

Parameters:

  • response (Hash)
  • collection_name (String, Symbol)

    The name of the method to return the collection

  • klass (Class)

    The class to instantiate object in the collection

  • client (Twitter::Client)
  • method_name (String, Symbol)
  • method_options (Hash)

Returns:



18
19
20
# File 'lib/twitter/cursor.rb', line 18

def self.from_response(response, collection_name, klass, client, method_name, method_options)
  new(response[:body], collection_name, klass, client, method_name, method_options)
end

Instance Method Details

#all(collection = collection, cursor = next_cursor) ⇒ Array

Parameters:

  • collection (Array) (defaults to: collection)
  • cursor (Integer) (defaults to: next_cursor)

Returns:



51
52
53
54
55
# File 'lib/twitter/cursor.rb', line 51

def all(collection=collection, cursor=next_cursor)
  cursor = @client.send(@method_name.to_sym, @method_options.merge(:cursor => cursor))
  collection += cursor.collection
  cursor.last? ? collection.flatten : all(collection, cursor.next_cursor)
end

#eachEnumerable

Returns:



58
59
60
61
62
# File 'lib/twitter/cursor.rb', line 58

def each
  all(collection, next_cursor).each do |element|
    yield element
  end
end

#first?Boolean Also known as: first

Returns:

  • (Boolean)


75
76
77
# File 'lib/twitter/cursor.rb', line 75

def first?
  previous_cursor.zero?
end

#last?Boolean Also known as: last

Returns:

  • (Boolean)


81
82
83
# File 'lib/twitter/cursor.rb', line 81

def last?
  next_cursor.zero?
end

#next_cursorObject Also known as: next



64
65
66
# File 'lib/twitter/cursor.rb', line 64

def next_cursor
  @attrs[:next_cursor] || -1
end

#previous_cursorObject Also known as: previous



69
70
71
# File 'lib/twitter/cursor.rb', line 69

def previous_cursor
  @attrs[:previous_cursor]
end