Module: Redd::Clients::Base::Read

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

Overview

Methods that require the “read” scope

Instance Method Summary collapse

Instance Method Details

#from_fullname(*fnames) ⇒ Objects::Listing<Objects::Thing>

Returns A listing of things with the fullname.

Parameters:

  • fnames (Array<String>)

    A list of fullnames.

Returns:



9
10
11
12
# File 'lib/redd/clients/base/read.rb', line 9

def from_fullname(*fnames)
  names = fnames.join(',')
  request_object(:get, '/api/info', id: names)
end

#from_url(url) ⇒ Objects::Thing

Returns The thing.

Parameters:

  • url (String)

    The url of the thing.

Returns:



16
17
18
# File 'lib/redd/clients/base/read.rb', line 16

def from_url(url)
  request_object(:get, '/api/info', url: url).first
end

#get_comments(subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

TODO:

Move all listing methods into a helper?

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

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

    The time period to consider when sorting.

Returns:



70
71
72
73
74
75
76
77
# File 'lib/redd/clients/base/read.rb', line 70

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |subreddit = nil, **params|
    srname = property(subreddit, :display_name) if subreddit
    path = "/#{sort}.json"
    path = path.prepend("/r/#{srname}") if subreddit
    request_object(:get, path, params)
  end
end

#get_controversial(subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

TODO:

Move all listing methods into a helper?

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

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

    The time period to consider when sorting.

Returns:



70
71
72
73
74
75
76
77
# File 'lib/redd/clients/base/read.rb', line 70

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |subreddit = nil, **params|
    srname = property(subreddit, :display_name) if subreddit
    path = "/#{sort}.json"
    path = path.prepend("/r/#{srname}") if subreddit
    request_object(:get, path, params)
  end
end

#get_hot(subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

TODO:

Move all listing methods into a helper?

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

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

    The time period to consider when sorting.

Returns:



70
71
72
73
74
75
76
77
# File 'lib/redd/clients/base/read.rb', line 70

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |subreddit = nil, **params|
    srname = property(subreddit, :display_name) if subreddit
    path = "/#{sort}.json"
    path = path.prepend("/r/#{srname}") if subreddit
    request_object(:get, path, params)
  end
end

#get_new(subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

TODO:

Move all listing methods into a helper?

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

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

    The time period to consider when sorting.

Returns:



70
71
72
73
74
75
76
77
# File 'lib/redd/clients/base/read.rb', line 70

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |subreddit = nil, **params|
    srname = property(subreddit, :display_name) if subreddit
    path = "/#{sort}.json"
    path = path.prepend("/r/#{srname}") if subreddit
    request_object(:get, path, params)
  end
end

#get_top(subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

TODO:

Move all listing methods into a helper?

Note:

The option :t only applies to the top and controversial sorts.

Get the appropriate listing.

Parameters:

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..1000)

    The maximum number of things to return.

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

    The time period to consider when sorting.

Returns:



70
71
72
73
74
75
76
77
# File 'lib/redd/clients/base/read.rb', line 70

%w(hot new top controversial comments).each do |sort|
  define_method :"get_#{sort}" do |subreddit = nil, **params|
    srname = property(subreddit, :display_name) if subreddit
    path = "/#{sort}.json"
    path = path.prepend("/r/#{srname}") if subreddit
    request_object(:get, path, params)
  end
end

#multi_from_path(path) ⇒ Objects::LabeledMulti

Fetch an individual multi from its path.

Parameters:

  • path (String)

    The multi’s path.

Returns:



41
42
43
44
45
# File 'lib/redd/clients/base/read.rb', line 41

def multi_from_path(path)
  # rubocop:disable Style/RegexpLiteral
  without_slash = path.gsub(/^\//, '')
  request_object(:get, '/api/multi/' + without_slash)
end

#my_multisObject

Fetch a list of multis belonging to the user.



33
34
35
36
# File 'lib/redd/clients/base/read.rb', line 33

def my_multis
  multis = get('/api/multi/mine').body
  multis.map { |thing| object_from_body(thing) }
end

#search(query, subreddit = nil, **params) ⇒ Objects::Listing<Objects::Thing>

Note:

The option :t only applies to the top and controversial sorts.

Search.

Parameters:

  • query (String)

    The query string.

  • subreddit (Objects::Subreddit, String) (defaults to: nil)

    The subreddit to query.

  • params (Hash)

    A list of params to send with the request.

Options Hash (**params):

  • :after (String)

    Return results after the given fullname.

  • :before (String)

    Return results before the given fullname.

  • :count (Integer)

    The number of items already seen in the listing.

  • :limit (1..100)

    The maximum number of things to return.

  • :syntax (:cloudsearch, :lucene, :plain)

    The type of syntax to use.

  • :sort (:relevance, :new, :hot, :top, :comments)

    The way to sort the results.

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

    The time period to consider when sorting.

Returns:



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/redd/clients/base/read.rb', line 100

def search(query, subreddit = nil, **params)
  path = '/search.json'
  params[:q] = query
  if subreddit
    params[:restrict_sr] = true
    srname = property(subreddit, :display_name)
    path = path.prepend("/r/#{srname}")
  end

  request_object(:get, path, params)
end

#subreddit_from_name(name) ⇒ Objects::Subreddit

Returns The subreddit if found.

Parameters:

  • name (String)

    The subreddit’s display name.

Returns:



28
29
30
# File 'lib/redd/clients/base/read.rb', line 28

def subreddit_from_name(name)
  request_object(:get, "/r/#{name}/about.json")
end

#user_from_name(name) ⇒ Objects::User

Returns The user.

Parameters:

  • name (String)

    The username.

Returns:



22
23
24
# File 'lib/redd/clients/base/read.rb', line 22

def user_from_name(name)
  request_object(:get, "/user/#{name}/about.json")
end