Skip to content
This repository was archived by the owner on Dec 30, 2021. It is now read-only.

Commit 93a0410

Browse files
committed
Merge pull request #8 from kaimallea/refactor
Refactor code
2 parents d20b88b + b367c9c commit 93a0410

File tree

9 files changed

+436
-274
lines changed

9 files changed

+436
-274
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
## Installation
1+
## Command-line Usage
22

3-
npm install -g imgur
3+
### Installation
44

5-
## Command-line Usage
5+
```bash
6+
npm install imgur -g
7+
```
68

7-
**You must set an API key before CLI use; get one at http://imgur.com/register/api_anon**
9+
### Usage
810

11+
Pass binary image files, urls, and/or base64-encoded image strings as arguments. Globbing is supported.
912

10-
Set your API key once and forget it (saved to ~/.imgurkey)
13+
Upload a single image:
1114

12-
imgur -k aCs53GSs4tga0ikp
15+
```bash
16+
imgur cat.png
17+
```
1318

14-
Upload a single image
19+
Upload all the jpegs in a particular folder:
1520

16-
imgur GooglePlus.png
21+
```bash
22+
imgur ~/Pictures/kittens/*.jpg
23+
```
1724

18-
Upload an entire directory (**not recursive yet**; automatically chooses pics)
25+
Upload an image from another place on the web:
1926

20-
imgur ~/Pictures/
27+
```bash
28+
imgur --url http://lolz.pics.com/troll.png
29+
```
2130

22-
Upload all .png files in the current directory
31+
Upload a Base-64 encoded image:
2332

24-
imgur *.png
25-
33+
```bash
34+
imgur --base64 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAmUlEQVQ4je2TsQ3CMBBFnxMa08WR2IQKJskIUNwMZAcYwWIQMs65JCUpEEIYW4pJy6v+6e6+/hVnnGsAzsCBMi7AsbbW/rIMsAU2xrnmkeruuzW7zgIw+JGbv6fGQpWzfy3HOsJlDQY/AlCv3jpF9oS5ZBOICKoB1YCIlCdQDR9127qyBHP5Gyw3CBXPr/qi709JHXE1S995AsqoJu8x78GsAAAAAElFTkSuQmCC
35+
```
2636

2737
## Module Usage
2838

29-
```javascript
30-
var imgur = require('imgur');
39+
### Installation
3140

32-
imgur.setKey('aCs53GSs4tga0ikp');
33-
34-
imgur.upload('/Users/kmallea/Pictures/Manga_Kai.jpg', function (response) {
35-
36-
if (response.error) {
37-
console.log(response.error);
38-
return;
39-
}
40-
41-
console.log('Direct link: ' + response.links.original);
42-
console.log('Imgur page: ' + response.links.imgur_page);
43-
});
41+
```bash
42+
npm install imgur
4443
```
45-
## Contributors
4644

47-
- Helge S. Holm (https://github.com/deestan)
48-
- Nicolas Bevacqua (https://github.com/bevacqua)
45+
### Usage
46+
47+
```javascript
48+
var imgur = require('imgur');
49+
50+
// Set a Client ID for your app
51+
imgur.setClientId('aCs53GSs4tga0ikp');
52+
53+
// Methods return promises
54+
imgur.uploadFile('/home/kai/*.png')
55+
.then(function(json) {
56+
console.log(json.data.link);
57+
})
58+
.catch(function(err) {
59+
console.error(err.message);
60+
});
61+
```

cli.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env node
2+
var imgur = require('./lib/imgur.js');
3+
var commander = require('commander');
4+
var util = require('util');
5+
6+
// Used to collect args for specific options
7+
function collect (val, arr) {
8+
arr.push(val);
9+
return arr;
10+
}
11+
12+
commander
13+
.version(imgur.VERSION)
14+
.option('-i, --info [id ...]', 'Lookup images by ID', collect, [])
15+
.option('-b, --base64 [string ...]', 'Upload a base64-encoded images', collect, [])
16+
.option('-u, --url [url ...]', 'Upload URLs', collect, [])
17+
.option('-f, --file [file ...]', 'Upload binary image files', collect, [])
18+
.parse(process.argv);
19+
20+
if (commander.info.length) {
21+
commander.info.forEach(function(id, index, array) {
22+
imgur.info(id)
23+
.then(function (json) {
24+
console.log(json.data);
25+
})
26+
.catch(function (err) {
27+
console.error(err.message);
28+
});
29+
});
30+
}
31+
32+
33+
if (commander.url.length) {
34+
commander.url.forEach(function(url, index, array) {
35+
imgur.uploadUrl(url)
36+
.then(function (json) {
37+
var output;
38+
if (commander.url.length > 1) {
39+
output = util.format('%s -> %s', url, json.data.link);
40+
} else {
41+
output = json.data.link;
42+
}
43+
console.log(output);
44+
})
45+
.catch(function (err) {
46+
console.error(err.message);
47+
});
48+
});
49+
}
50+
51+
52+
if (commander.base64.length) {
53+
commander.base64.forEach(function(str, index, array) {
54+
imgur.uploadBase64(str)
55+
.then(function (json) {
56+
var output;
57+
if (commander.base64.length > 1) {
58+
output = util.format('%s... -> %s', str.substr(0, 7), json.data.link);
59+
} else {
60+
output = json.data.link;
61+
}
62+
console.log(output);
63+
})
64+
.catch(function (err) {
65+
console.error(err.message);
66+
});
67+
});
68+
}
69+
70+
71+
if (commander.file.length || commander.args.length) {
72+
var args = commander.file.concat(commander.args);
73+
args.forEach(function(file, index, array) {
74+
imgur.uploadFile(file)
75+
.then(function (json) {
76+
var output;
77+
if (args.length > 1) {
78+
output = util.format('%s -> %s', file, json.data.link);
79+
} else {
80+
output = json.data.link;
81+
}
82+
console.log(output);
83+
})
84+
.catch(function (err) {
85+
console.error(err.message);
86+
});
87+
});
88+
}

0 commit comments

Comments
 (0)