create project

This commit is contained in:
Dylan Greene 2016-04-27 15:06:24 -04:00
commit 7b8a0009ed
9 changed files with 200 additions and 0 deletions

20
.editorconfig Normal file
View file

@ -0,0 +1,20 @@
# Get the plugin for your editor and your
# tab settings will be set automatically.
# http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
# Indentation override for all JS under lib directory
[*.js]
indent_size = 4

2
.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules

5
.travis.yml Normal file
View file

@ -0,0 +1,5 @@
language: node_js
node_js:
- '6'
- '5'
- '4'

33
index.js Normal file
View file

@ -0,0 +1,33 @@
'use strict';
const got = require('got');
const uniqueRandomArray = require('unique-random-array');
const randomCache = {};
function formatResult(getRandomImage) {
const imageData = getRandomImage();
if (!imageData) {
return;
}
return `http://imgur.com/${imageData.hash}${imageData.ext.replace(/\?.*/, '')}`;
}
function storeResults(images, subreddit) {
const getRandomImage = uniqueRandomArray(images);
randomCache[subreddit] = getRandomImage;
return getRandomImage;
}
module.exports = function (subreddit) {
subreddit = (typeof subreddit === 'string' && subreddit.length !== 0) ? subreddit : 'puppies';
if (randomCache[subreddit]) {
return Promise.resolve(formatResult(randomCache[subreddit]));
}
return got(`https://imgur.com/r/${subreddit}/hot.json`, {json: true})
.then(response => storeResults(response.body.data, subreddit))
.then(getRandomImage => formatResult(getRandomImage));
};

21
license Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Dylan Greene <dylang@gmail.com> (github.com/dylang)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

35
package.json Normal file
View file

@ -0,0 +1,35 @@
{
"name": "random-puppy",
"version": "1.0.0",
"description": "Get a random puppy image url.",
"license": "MIT",
"repository": "dylang/random-puppy",
"author": {
"name": "Dylan Greene",
"email": "dylang@gmail.com",
"url": "github.com/dylang"
},
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"puppy", "doggie", "dog", "imgur", "random", "placeholder"
],
"dependencies": {
"got": "^6.3.0",
"unique-random-array": "^1.0.0"
},
"devDependencies": {
"ava": "^0.14.0",
"xo": "^0.14.0"
},
"xo": {
"space": 4
}
}

40
readme.md Normal file
View file

@ -0,0 +1,40 @@
# random-puppy [![Build Status](https://travis-ci.org/dylang/random-puppy.svg?branch=master)](https://travis-ci.org/dylang/random-puppy)
> Get a random puppy image url.
<img src="http://i.imgur.com/0zZ8m6B.jpg" width="300px">
## Install
```
$ npm install --save random-puppy
```
## Usage
```js
const randomPuppy = require('random-puppy');
randomPuppy()
.then(url => {
console.log(url);
})
//=> 'http://imgur.com/IoI8uS5'
```
## API
### randomPuppy()
Returns a random puppy image from http://imgur.com/ from https://www.reddit.com/r/puppy
### randomPuppy(subreddit)
Returns a random image from the selected subreddit. Warning: We cannot promise it will be a puppy.
## License
MIT © [Dylan Greene](https://github.com/dylang)

42
test.js Normal file
View file

@ -0,0 +1,42 @@
import test from 'ava';
import randomPuppy from './';
const imgurRegEx = /^https?:\/\/(\w+\.)?imgur.com\/[a-zA-Z0-9]+(\.[a-zA-Z]{3})?$/;
test('get random', async t => {
const result = await randomPuppy();
t.regex(result, imgurRegEx);
});
test('get more random', async t => {
const result1 = await randomPuppy();
t.regex(result1, imgurRegEx);
const result2 = await randomPuppy();
t.regex(result2, imgurRegEx);
const result3 = await randomPuppy();
t.regex(result3, imgurRegEx);
const result4 = await randomPuppy();
t.regex(result4, imgurRegEx);
});
test('different subreddit', async t => {
const result1 = await randomPuppy('aww');
t.regex(result1, imgurRegEx);
const result2 = await randomPuppy('aww');
t.regex(result2, imgurRegEx);
const result3 = await randomPuppy('aww');
t.regex(result3, imgurRegEx);
const result4 = await randomPuppy('aww');
t.regex(result4, imgurRegEx);
});
test('invalid subreddit', async t => {
const result1 = await randomPuppy('23rkljr2klj3');
t.falsy(result1);
const result2 = await randomPuppy('');
t.regex(result2, imgurRegEx);
const result3 = await randomPuppy({});
t.regex(result3, imgurRegEx);
const result4 = await randomPuppy(false);
t.regex(result4, imgurRegEx);
});