Class: Redd::Models::FrontPage

Inherits:
BasicModel show all
Defined in:
lib/redd/models/front_page.rb

Overview

The front page. FIXME: deal with serious code duplication from Subreddit

Instance Attribute Summary

Attributes inherited from BasicModel

#client

Instance Method Summary collapse

Methods inherited from BasicModel

from_id, #initialize, #inspect, #method_missing, #respond_to_missing?, #to_ary, #to_h

Constructor Details

This class inherits a constructor from Redd::Models::BasicModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Redd::Models::BasicModel

Instance Method Details

#comment_stream(**params, &block) ⇒ Object

Stream newly submitted comments.


65
66
67
68
69
70
71
72
# File 'lib/redd/models/front_page.rb', line 65

def comment_stream(**params, &block)
  params[:limit] ||= 100
  stream = Utilities::Stream.new do |previous|
    before = previous ? previous.first.name : nil
    listing(:comments, params.merge(before: before))
  end
  block_given? ? stream.stream(&block) : stream.enum_for(:stream)
end

#comments(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#controversial(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#gilded(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#hot(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#listing(sort, **params) ⇒ Listing<Submission>

Note:

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

Get the appropriate listing.

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

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

    the time period to consider when sorting.


36
37
38
39
# File 'lib/redd/models/front_page.rb', line 36

def listing(sort, **params)
  params[:t] = params.delete(:time) if params.key?(:time)
  @client.model(:get, "/#{sort}", params)
end

#new(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#post_stream(**params, &block) ⇒ Object

Stream newly submitted posts.


55
56
57
58
59
60
61
62
# File 'lib/redd/models/front_page.rb', line 55

def post_stream(**params, &block)
  params[:limit] ||= 100
  stream = Utilities::Stream.new do |previous|
    before = previous ? previous.first.name : nil
    listing(:new, params.merge(before: before))
  end
  block_given? ? stream.stream(&block) : stream.enum_for(:stream)
end

#rising(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#top(**params) ⇒ Object

See Also:


50
51
52
# File 'lib/redd/models/front_page.rb', line 50

%i(hot new top controversial comments rising gilded).each do |sort|
  define_method(sort) { |**params| listing(sort, **params) }
end

#wiki_page(title) ⇒ WikiPage

Get a wiki page by its title.


19
20
21
# File 'lib/redd/models/front_page.rb', line 19

def wiki_page(title)
  WikiPage.new(@client, title: title)
end

#wiki_pagesArray<String>


12
13
14
# File 'lib/redd/models/front_page.rb', line 12

def wiki_pages
  @client.get('/wiki/pages').body[:data]
end