Module: Snoo::Listings

Defined in:
lib/snoo/listings.rb

Overview

Methods for getting thing listings. Comments, links, etc

Author:

Instance Method Summary collapse

Instance Method Details

#get_comments(opts = {}) ⇒ Object

Get a comment listing from the site

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :subreddit (String)

    The subreddit to fetch the comments of

  • :link_id (String)

    The link to get the comments of

  • :comment_id (String)

    The parent comment of a thread.

  • :context (Fixnum)

    The context of the thread, that is, how far above the comment_id to return

  • :limit (Fixnum) — default: 100

    The total number of comments to return. If you have gold this can include the whole thread, but is buggy. Recommend no more than 1000

  • :depth (Fixnum)

    How deep to render a comment thread.

  • :sort (old, new, hot, top, controversial, best)

    The sort used.



18
19
20
21
22
23
# File 'lib/snoo/listings.rb', line 18

def get_comments opts = {}
  query = { limit: 100 }
  query.merge! opts
  url = "%s/comments/%s%s.json" % [('/r/' + opts[:subreddit] if opts[:subreddit]), opts[:link_id], ('/blah/' + opts[:comment_id] if opts[:comment_id])]
  get(url, query: query)
end

#get_listing(opts = {}) ⇒ Object

Gets a listing of links from reddit.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :subreddit (String)

    The subreddit targeted. Can be psuedo-subreddits like all or mod. If blank, the front page

  • :page (new, controversial, top, saved)

    The page to view.

  • :sort (new, rising)

    The sorting method. Only relevant on the new page

  • :t (hour, day, week, month, year)

    The timeframe. Only relevant on some pages, such as top. Leave empty for all time

  • :limit (1..100)

    The number of things to return.

  • :after (String)

    Get things after this thing id

  • :before (String)

    Get things before this thing id



36
37
38
39
40
41
42
43
44
# File 'lib/snoo/listings.rb', line 36

def get_listing opts = {}
  # Build the basic url
  url = "%s/%s.json" % [('/r/' + opts[:subreddit] if opts[:subreddit] ), (opts[:page] if opts[:page])]
  # Delete subreddit and page from the hash, they dont belong in the query
  [:subreddit, :page].each {|k| opts.delete k}
  query = opts
  # Make the request
  get(url, query: query)
end

#search(query, opts = {}) ⇒ Object

Search reddit

Parameters:

  • query (String)

    The search query.

  • opts (Hash) (defaults to: {})

    a customizable set of options

  • opts (Hash) (defaults to: {})

    An options hash

Options Hash (opts):

  • :restrict_sr (true, false)

    Restrict to the calling subreddit

  • :subreddit (String)

    The calling subreddit.

  • :limit (1..100)

    The amount of results to return

  • :before (String)

    Return things before this id

  • :after (String)

    Return things after this id

  • :sort (relevance, new, top)

    The sorting of the results.

  • :syntax (cloudsearch, lucene)

    The search syntax.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/snoo/listings.rb', line 58

def search query, opts = {}
  # This supports searches with and without a subreddit
  url = "%s/search.json" % ('/r/' + opts[:subreddit] if opts[:subreddit])

  # Construct the query
  httpquery = {q: query}
  opts.delete :subreddit
  httpquery.merge! opts

  get(url, query: httpquery)
end