module Redd::Clients::Base::Utilities

Internal methods that make life easier. @todo Move this out to Redd::Utils?

Constants

OBJECT_KINDS

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

Public Instance Methods

append_to_listing(meth, path, params, multiplier, listing) click to toggle source

appends items to a listing when the limit is > 100 @param [Symbol] meth The method to use. @param [String] path The path to visit. @param [Hash] params The data to send with the request. @param [Integer] multiplier The number of times to get listings @param [Redd::Objects::Listing] listing The listing to append to @return [String] returns the last Redd::Objects::Listing#after.

# 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) click to toggle source

@param [Objects::Submission, Objects::Comment] base The start of the

comment tree.

@author Bryce Boe (@bboe) in Python @return [Array<Objects::Comment, Objects::MoreComments>] A linear

array of the submission's comments or the comments' replies.
# 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) click to toggle source

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

@param [Hash] body A JSON hash. @return [Objects::Thing, Objects::Listing]

# 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) click to toggle source

Get a given property of a given object. @param [Objects::Base, String] object The object with the property. @param [Symbol] property The property to get.

# 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 = {}) click to toggle source

Request and create an object from the response. @param [Symbol] meth The method to use. @param [String] path The path to visit. @param [Hash] params The data to send with the request. @return [Objects::Base] The object returned from the request.

# 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

Private Instance Methods

flatten_body(body) click to toggle source

Take a multilevel body ({kind: “tx”, data: {…}}) and flatten it into something like {kind: “tx”, …} @param [Hash] body The response body. @return [Hash] The flattened hash.

# File lib/redd/clients/base/utilities.rb, line 132
def flatten_body(body)
  data = body[:data] || body
  data[:kind] = body[:kind]
  data
end
object_from_kind(kind) click to toggle source

@param [String] kind A kind in the format /t/. @return [Objects::Base, Objects::Listing] The appropriate object for

a given kind.
# File lib/redd/clients/base/utilities.rb, line 141
def object_from_kind(kind)
  OBJECT_KINDS.fetch(kind, Objects::Base)
end