commit 7b8a0009ed34d415e7fc6c68033a56e0d3df600e Author: Dylan Greene Date: Wed Apr 27 15:06:24 2016 -0400 create project diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..69f2c40 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..391f0a4 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +*.js text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a56a7ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d4619bb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - '6' + - '5' + - '4' diff --git a/index.js b/index.js new file mode 100644 index 0000000..d1a6467 --- /dev/null +++ b/index.js @@ -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)); +}; diff --git a/license b/license new file mode 100644 index 0000000..aaac20b --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Dylan Greene (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. diff --git a/package.json b/package.json new file mode 100644 index 0000000..ac1cf6e --- /dev/null +++ b/package.json @@ -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 + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..37e3b0e --- /dev/null +++ b/readme.md @@ -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. + + + +## 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) diff --git a/test.js b/test.js new file mode 100644 index 0000000..f3c1a90 --- /dev/null +++ b/test.js @@ -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); +});