Module: Redd::Clients::Base::Utilities

Included in:
Redd::Clients::Base
Defined in:
lib/redd/clients/base/utilities.rb

Overview

TODO:

Move this out to Redd::Utils?

Internal methods that make life easier.

Constant Summary collapse

OBJECT_KINDS =

The kind strings and the objects that should be used for them.

{
  'Listing'      => Objects::Listing,
  'wikipage'     => Objects::WikiPage,
  'LabeledMulti' => Objects::LabeledMulti,
  'more'         => Objects::MoreComments,
  't1'           => Objects::Comment,
  't2'           => Objects::User,
  't3'           => Objects::Submission,
  't4'           => Objects::PrivateMessage,
  't5'           => Objects::Subreddit
}.freeze

Instance Method Summary collapse

Instance Method Details

#append_to_listing(meth, path, params, multiplier, listing) ⇒ String

appends items to a listing when the limit is > 100



74
75
76
77
78
79
80
81
82
83
# File 'lib/redd/clients/base/utilities.rb', line 74

def append_to_listing(meth, path, params, multiplier, listing)
  multiplier.times do
    body = send(meth, path, params).body
    new_listing = object_from_body(body)
    params[:after] = new_listing.after
    new_listing.each { |item| listing << item }
    break if params[:after].nil?
  end
  params[:after]
end

#flat_comments(base) ⇒ Array<Objects::Comment, Objects::MoreComments>

Returns A linear array of the submission’s comments or the comments’ replies.

Author:

  • Bryce Boe (@bboe) in Python



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/redd/clients/base/utilities.rb', line 102

def flat_comments(base)
  meth = (base.is_a?(Objects::Submission) ? :comments : :replies)
  stack = base.send(meth).dup
  flattened = []

  until stack.empty?
    comment = stack.shift
    if comment.is_a?(Objects::Comment)
      replies = comment.replies
      stack = replies + stack if replies
    end
    flattened << comment
  end

  flattened
end

#object_from_body(body) ⇒ Objects::Thing, Objects::Listing

Create an object instance with the correct attributes when given a body.



90
91
92
93
94
95
# File 'lib/redd/clients/base/utilities.rb', line 90

def object_from_body(body)
  return nil unless body.is_a?(Hash)
  object = object_from_kind(body[:kind])
  flat = flatten_body(body)
  object.new(self, flat)
end

#property(object, property) ⇒ Object

Get a given property of a given object.



122
123
124
# File 'lib/redd/clients/base/utilities.rb', line 122

def property(object, property)
  object.respond_to?(property) ? object.send(property) : object.to_s
end

#request_object(meth, path, params = {}) ⇒ Objects::Base

Request and create an object from the response.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redd/clients/base/utilities.rb', line 39

def request_object(meth, path, params = {})
  unless params[:limit].nil?
    if params[:limit] > 100
      multiplier = params[:limit] / 100
      last_block_limit = (params[:limit] - (multiplier * 100)) - 1
      multiplier -= 1
      params[:limit] = 100
      body = send(meth, path, params).body
      listing = object_from_body(body)
      params[:after] = listing.after
      return listing if params[:after].nil?

      after = append_to_listing(meth, path, params, multiplier, listing)

      params[:limit] = last_block_limit
      params[:after] = after

      unless multiplier == 9 || last_block_limit.zero?
        append_to_listing(meth, path, params, 1, listing)
      end
      return listing
    end
    params[:limit] -= 1
  end
  body = send(meth, path, params).body
  object_from_body(body)
end