The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::Snoo::Subreddit - Mojo wrapper for Reddit Subreddits

SYNOPSIS

    use Mojo::Snoo::Subreddit;

    # OAuth ONLY. Reddit is deprecating cookie auth soon.
    my $snoo = Mojo::Snoo::Subreddit->new(
        name          => 'perl',
        username      => 'foobar',
        password      => 'very_secret',
        client_id     => 'oauth_client_id',
        client_secret => 'very_secret_oauth',
    );

    # print each title from /r/perl post
    # (OAuth not required for this action)
    $snoo->links->each(sub { say $_->title });

ATTRIBUTES

name

The name of the subreddit. This is required for object instantiation. The constructor can accept a single string value or key/value pairs. Examples:

    Mojo::Snoo::Subreddit->new('perl')->name;
    Mojo::Snoo::Subreddit->new(name => 'perl')->name;

about

Returns the About section of a subreddit.

    GET /r/$subreddit/about

Returns a monkey-patched object containing all of the keys under the JSON's "data" key. Example:

    my $about = Mojo::Snoo::Subreddit->new('perl')->about;

    say $about-title;
    say $about->description;
    say $about->description_html;

mods

Returns a list of the subreddit's moderators.

    GET /r/$subreddit/about/moderators

Returns a Mojo::Collection object containing a list of monkey-patched objects. Example:

    Mojo::Snoo::Subreddit->new('perl')->mods->each(
        sub {
            say $_->id;
            say $_->name;
            say $_->date;
            say $_->mod_permissions;
        }
    );

METHODS

Returns a Mojo::Collection object containing a list of Mojo::Snoo::Link objects.

    GET /r/$subreddit

Accepts arguments for limit, API endpoint parameters, and a callback (in that order). The default limit is 25 and cannot be greater than 100. Callback receives a Mojo::Message::Response object.

    Mojo::Snoo::Subreddit-new('perl')->links;
    Mojo::Snoo::Subreddit-new('perl')->links(20);
    Mojo::Snoo::Subreddit->new('pics')->links_top(
        50 => {after => 't3_92dd8'} => sub {
            my $res = shift;
            say 'Response code: ' . $res->code;
        }
      )->each(
        sub {
            say $_->title;
        }
      );

Like "links" but sorted by new.

    GET /r/$subreddit/new

Like "links" but sorted by rising.

    GET /r/$subreddit/rising

Like "links" but sorted by top (most upvoted).

    GET /r/$subreddit/top

Like "links_top" but from the past week.

    GET /r/$subreddit/top?t=week

Like "links_top" but from the past month.

    GET /r/$subreddit/top?t=month

Like "links_top" but from the past year.

    GET /r/$subreddit/top?t=year

Like "links_top" but from all time.

    GET /r/$subreddit/top?t=all

Like "links" but sorted by controversial.

    GET /r/$subreddit/controversial

Like "links_contro" but from the past week.

    GET /r/$subreddit/controversial?t=week

Like "links_contro" but from the past month.

    GET /r/$subreddit/controversial?t=month

Like "links_contro" but from the past year.

    GET /r/$subreddit/controversial?t=year

Like "links_contro" but from all time.

    GET /r/$subreddit/controversial?t=all

subscribe

Subscribe to subreddit. Accepts callback.

    POST /api/subscribe

unsubscribe

Unsubscribe from subreddit. Accepts callback.

    POST /api/subscribe

API DOCUMENTATION

Please see the official Reddit API documentation for more details regarding the usage of endpoints. For a better idea of how OAuth works, see the Quick Start and the full documentation. There is also a lot of useful information of the redditdev subreddit.

LICENSE

The (two-clause) FreeBSD License. See LICENSE for details.