Module: Redd::Client::Unauthenticated::Utilities

Included in:
Redd::Client::Unauthenticated
Defined in:
lib/redd/client/unauthenticated/utilities.rb

Instance Method Summary collapse

Instance Method Details

#comment_stream(*args, &block) ⇒ Object



15
16
17
# File 'lib/redd/client/unauthenticated/utilities.rb', line 15

def comment_stream(*args, &block)
  submission_stream(:comments, *args, &block)
end

#comments_from_response(*args) ⇒ Object (private)



94
95
96
97
# File 'lib/redd/client/unauthenticated/utilities.rb', line 94

def comments_from_response(*args)
  body = request(*args)[1]
  object_from_body(body)
end

#extract_attribute(object, attribute) ⇒ Object (private)



36
37
38
39
40
41
42
43
# File 'lib/redd/client/unauthenticated/utilities.rb', line 36

def extract_attribute(object, attribute)
  case object
  when ::String
    object
  else
  	object.send(attribute)
  end
end

#extract_fullname(object) ⇒ Object (private)



45
46
47
# File 'lib/redd/client/unauthenticated/utilities.rb', line 45

def extract_fullname(object)
  extract_attribute(object, :fullname)
end

#extract_id(object) ⇒ Object (private)



49
50
51
# File 'lib/redd/client/unauthenticated/utilities.rb', line 49

def extract_id(object)
  extract_attribute(object, :id)
end

#object_from_body(body) ⇒ Object (private)



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/redd/client/unauthenticated/utilities.rb', line 82

def object_from_body(body)
  return nil unless body.is_a?(Hash) && body.has_key?(:kind)
  object = object_from_kind(body[:kind])

  if object == Redd::Object::Listing
    body[:data][:children] = objects_from_listing(body)
    object.new(body)
  else
    object.new(self, body)
  end
end

#object_from_kind(kind) ⇒ Object (private)

rubocop:disable Style/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/redd/client/unauthenticated/utilities.rb', line 53

def object_from_kind(kind) # rubocop:disable Style/MethodLength
  case kind
  when "Listing"
    Redd::Object::Listing
  when "more"
    Redd::Object::MoreComments
  when "t1"
    Redd::Object::Comment
  when "t2"
    Redd::Object::User
  when "t3"
    Redd::Object::Submission
  when "t4"
    Redd::Object::PrivateMessage
  when "t5"
    Redd::Object::Subreddit
  when "wikipage"
    Redd::Object::WikiPage
  else
    Redd::Thing
  end
end

#object_from_response(*args) ⇒ Object (private)



99
100
101
102
# File 'lib/redd/client/unauthenticated/utilities.rb', line 99

def object_from_response(*args)
  body = request(*args)
  object_from_body(body)
end

#objects_from_listing(thing) ⇒ Object (private)



76
77
78
79
80
# File 'lib/redd/client/unauthenticated/utilities.rb', line 76

def objects_from_listing(thing)
  thing[:data][:children].map do |child|
    object_from_body(child)
  end
end

#submission_stream(listing, subreddit = nil, params = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redd/client/unauthenticated/utilities.rb', line 19

def submission_stream(listing, subreddit = nil, params = {}, &block)
  loop do
    # Get the latest comments from the subreddit. By the way, this line
    #   is the one where the sleeping/rate-limiting happens.
    objects = get_listing(listing, subreddit, params)
    unless objects.empty?
      # Run the loop for each of the new comments accessed.
      # I should probably add it to some sort of Set to avoid duplicates.
      objects.reverse_each { |object| block.call(object) }
      # Set the latest comment.
      params[:before] = objects.first.fullname
    end
  end
end