module NeonRAW::Clients::Base::Utilities
Utilities
for the base client.
Public Instance Methods
Search for subreddits. @!method find_subreddits
(query) @param query [String] The name to search for (50 characters maximum). @return [Array<String>] Returns the list of subreddits.
# File lib/NeonRAW/clients/base/utilities.rb, line 10 def find_subreddits(query) params = { query: query } data = request_data('/api/subreddits_by_topic', :get, params) data.map { |subreddit| subreddit[:name] } end
Flattens comment trees into a single array. @!method flatten_tree
(comments) @param comments [Array] A list of comments to be checked for replies
to flatten.
@return [Array] Returns a list of the flattened comments.
# File lib/NeonRAW/clients/base/utilities.rb, line 43 def flatten_tree(comments) flattened = [] stack = comments.dup until stack.empty? comment = stack.shift if comment.is_a?(Objects::Comment) # MoreComments can be mixed in. replies = comment.replies stack = replies + stack unless replies.nil? end flattened << comment end flattened end
Get info on a link/comment/subreddit. @!method info(params = {}) @param params [Hash] The parameters. @option params :name [String] The fullname of the thing. @option params :url [String] The URL of the thing. @return [NeonRAW::Objects::Listing] Returns a listing with the items. @note :name and :url can take multiple fullnames separated by commas. @see www.reddit.com/dev/api#fullnames
# File lib/NeonRAW/clients/base/utilities.rb, line 90 def info(params = {}) params[:id] = params[:name] params.delete(:name) build_listing('/api/info', params) end
Fetches a list of wiki pages from Reddit. @!method wikipages @return [Array<String>] Returns a list of wiki pages.
# File lib/NeonRAW/clients/base/utilities.rb, line 60 def wikipages request_data('/wiki/pages', :get)[:data] end
Private Instance Methods
Streams listing items continuously. @!method stream(path, params) @param path [String] The API path for the listing you want streamed. @param params [Hash] The optional parameters for the request. @return [Enumerator] Returns an enumerator for the streamed data.
# File lib/NeonRAW/clients/base/utilities.rb, line 69 def stream(path, params) Enumerator.new do |data_stream| before = params[:before] loop do params[:before] = before listing = build_listing(path, params) listing.each { |thing| data_stream << thing } before = listing.first.name unless listing.empty? sleep(1) end end end