Skip to content

Commit

Permalink
Merge branch 'pr-9'
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
	bin/gif.js
  • Loading branch information
henvic committed May 16, 2015
2 parents 7c86e29 + 2bdc76e commit 7f16243
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Option | Usage | Type
`-n`, `--number` | **Required** | `Number`
`-R`, `--reaction` | **Required** | `String`
`-c`, `--comment` | *Optional* | `String`
`-b`, `--best` | *Optional* | `Boolean`
`-B`, `--bestof` | *Optional* | `Number`
`-i`, `--image` | *Optional* | `String`
`-r`, `--repo` | *Optional* | `String`
`-u`, `--user` | *Optional* | `String`
Expand Down Expand Up @@ -65,6 +67,18 @@ gh gif 75 --image http://media1.giphy.com/media/5DQdk5oZzNgGc/original.gif
gh gif --reaction happy --number 1 --number 2
```
* Comment on pull request/issue #75 with the best search result
```
gh gif 75 --reaction happy --best
```
* Comment on pull request/issue #75 with a random GIF of the first three result items.
```
gh gif 75 --reaction happy --bestof 3
```
## Testing
Check [Travis](https://travis-ci.org/node-gh/gh-gif) for continous integration results.
Expand Down
21 changes: 20 additions & 1 deletion bin/gif.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Gif.DETAILS = {
'number': [Number, Array],
'reaction': String,
'comment': String,
'best': Boolean,
'bestof': Number,
'repo': String,
'user': String
},
Expand All @@ -47,6 +49,8 @@ Gif.DETAILS = {
'n': [ '--number' ],
'R': [ '--reaction' ],
'c': [ '--comment' ],
'b': [ '--best' ],
'B': [ '--bestof' ],
'r': [ '--repo' ],
'u': [ '--user' ]
},
Expand Down Expand Up @@ -93,6 +97,10 @@ Gif.prototype.image = function(image) {
});
};

Gif.prototype.getRandomItem = function (array) {
return array[Math.floor(Math.random() * array.length)];
};

Gif.prototype.reaction = function() {
var instance = this,
options = instance.options,
Expand All @@ -111,8 +119,19 @@ Gif.prototype.reaction = function() {
return;
}

random = result[Math.floor(Math.random() * result.length)];
if (options.best) {
options.image = result[0].images.original.url;
instance.image(options.image);
return;
}

if (options.bestof > 0) {
result = result.slice(0, options.bestof);
}

random = instance.getRandomItem(result);
options.image = random.images.original.url;

instance.image(options.image);
});
};
Expand Down

0 comments on commit 7f16243

Please sign in to comment.