npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redwrap

v0.0.4

Published

Redwrap simplifies Reddit API requests by providing an easy to use wrapper with jQuery style chaining.

Downloads

124

Readme

#redwrap

##Description

Redwrap is a node.js module that simplifies Reddit API requests through jquery style chaining methods.

##How to use

Start by requiring the redwrap module

reddit = require('redwrap');

This gives us access to 3 types of requests.

###1.The basic user request

Redwrap uses the request module to issue requests behind the scenes. If you are familiar with request, you will notice the arguments for all of our callbacks in redwrap are the same.

reddit.user('username', function(err, res, body){
  console.log(body); //outputs json for the requested username
});

###2.The basic subreddit request

reddit.r('WTF', function(err, res, body){
  console.log(body); //outputs json for first page of WTF subreddit
});	

###3.The basic list request

With the list method the first argument is optional, and we will see why in a moment, but let's include one for now.

reddit.list('hot', function(err, res, body){
	console.log(body); //json for the front page of reddit w/ 'hot' filter
});

That's cool, but we can make it even easier by chaining other methods onto our request. So let's look at how to format our previous request, using chaining this time.

reddit.list().hot().exe(function(err, res, body){
	console.log(body);
});

Take note of the .exe() method. This is an optional method that can be placed at the end of our chain which takes a callback as its only argument. You might use this as a way of breaking up long chains into a more readable structure. For shorter chains, we can just add our callback to the last method in the chain.

###Filters

Filters in redwrap are methods we can chain to our requests. These filters correspond exactly with the filters you see on the reddit site. In the last example, we saw our first use of a filter, when we chained .hot() to our request. Below is a list of possible filters. You can find

User filters

  • overview
  • comments
  • submitted
  • liked
  • disliked
  • hidden
  • saved
  • about

Subreddit filters

  • hot
  • new
  • controversial
  • top

###Queries

Queries are applied to our request chain in the same way filters are. They allow us to make a more targeted request from the Reddit API. The main difference between filters and queries is that queries require an argument when called. In this next example we will be requesting the 'top' '100' comments from this 'year', for the given username.

reddit.user('username').sort('top').from('year').limit(100, function(err, res, body){
	console.log(body); //top 100 comments this year for username
});

Here is a list of the possible queries along with acceptable arguments. You can learn more about the arguments from the Reddit API documentation.

  • sort() Possible arguments: 'hot', 'top', 'new', 'controversial'
  • from() Possible arguments: 'all','day','week','month','year'
  • limit() Possible arguments: Any integer from 1 - 100;
  • after() Possible arguments: a post ID
  • before() Possible arguments: a post ID
  • count() Possible arguments: An integer from 1 - 100;

###Advanced - Making multiple requests with the .all() method.

Use this at your own risk

Before using this feature it is important that you be aware of the rules found in the Reddit API documentation. Currently, there is no throttle on the number of requests per min the .all() method makes. It will continue to make requests until it completes, or until Reddit cuts you off. Also note that the default limit is set to 100. You can change this by adding your own limit query to the request chain, but it isn't recommended. I would like to expand on this feature in the near future, so please, if you have any feature requests let me know.

Here is the basic pattern for making multiple requests with redwrap.


reddit.user('username').comments().sort('top').all(function(res) {

	res.on('data', function(data) {
		console.log(data);
	});
	res.on('error', function(e) {
		console.log(e);
	});
	res.on('end', function(data){
		console.log(data);
	});
	
});

If you have experience with the http module in node, then this should look very familiar. When you use the .all method, it passes back an event emitter object that you can attach your event listeners to. The standard data, error, and end events are possible. (The 'end' event is actually bugged at the moment. If you have a fix let me know.)

That's all for now. My goal is to expand the features of redwrap to cover as much of the Reddit API as is needed by devs, so let me know if there is a feature you would like to see included. Enjoy!