kraken 0.4.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/.jshintrc ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "maxerr": 50, // {int} Maximum error before stopping
3
+ "camelcase": false, // true: Identifiers must be in camelCase
4
+ "curly": true, // true: Require {} for every new block or scope
5
+ "eqeqeq": true, // true: Require triple equals (===) for comparison
6
+ "forin": true, // true: Require filtering for..in loops with obj.hasOwnProperty()
7
+ "freeze": true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
8
+ "indent": 4, // {int} Number of spaces to use for indentation
9
+ "noempty": true, // true: Prohibit use of empty blocks
10
+ "nonbsp": true, // true: Prohibit "non-breaking whitespace" characters.
11
+ "nonew": false, // true: Prohibit use of constructors for side-effects (without assignment)
12
+ "quotmark": "single",
13
+ "undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
14
+ "unused": true, // true: Require all defined variables be used
15
+ "strict": false, // true: Requires all functions run in ES5 Strict Mode
16
+ "devel": false, // Development/debugging (alert, confirm, etc)
17
+ "node": true, // Node.js
18
+ "globals": {
19
+ // additional predefined global variables
20
+ "describe": true,
21
+ "test": true,
22
+ "it": true,
23
+ "expect": true
24
+ },
25
+ "esversion": 10,
26
+ "asi": true // Missing semicolon warning off
27
+ }
@@ -0,0 +1,177 @@
1
+ const Kraken = require('../')
2
+ const fs = require('fs')
3
+ require('dotenv').config();
4
+
5
+ // Environment variables names with API key and secret
6
+ const envKey = 'KRAKEN_API_KEY'
7
+ const envSecret = 'KRAKEN_API_SECRET'
8
+
9
+ const krakenKey = process.env[envKey]
10
+ const krakenSecret = process.env[envSecret]
11
+
12
+ const MAX_TIMEOUT = 20000;
13
+
14
+ // Check key and secret variables
15
+ CheckEnvVars([envKey, envSecret])
16
+
17
+ // Tests extensions
18
+ expect.extend({
19
+ toBeKrakenSuccess(status) {
20
+ const result = {
21
+ message: () => 'Unknown error',
22
+ pass: false
23
+ }
24
+
25
+ if (status == null) {
26
+ result.message = () => 'Empty status'
27
+ } else if (status.success === true) {
28
+ result.message = () => 'Not passed'
29
+ result.pass = true
30
+ } else if (status.error) {
31
+ result.message = () =>
32
+ `Kraken API returned an error: ${status.error}`
33
+ }
34
+
35
+ return result
36
+ }
37
+ })
38
+
39
+ class DevData {
40
+ constructor(data) {
41
+ this.wait = true
42
+ this.dev = true
43
+ Object.assign(this, data)
44
+ }
45
+ }
46
+
47
+ // Main tests
48
+ describe('Kraken.io API', () => {
49
+ // Test variables
50
+ let krakenAuth = null
51
+ let kraken = null
52
+
53
+ // Test prepare
54
+ beforeAll(() => {
55
+ krakenAuth = {
56
+ api_key: krakenKey,
57
+ api_secret: krakenSecret
58
+ }
59
+ kraken = new Kraken(krakenAuth)
60
+ })
61
+
62
+ // auth 1
63
+ it('new Kraken({api_key, api_secret})', () => {
64
+ const kraken = new Kraken(krakenAuth)
65
+ expect(kraken.auth).toEqual(krakenAuth)
66
+ })
67
+
68
+ // auth 2
69
+ it('new Kraken(api_key, api_secret)', () => {
70
+ const kraken = new Kraken(krakenAuth.api_key, krakenAuth.api_secret)
71
+ expect(kraken.auth).toEqual(krakenAuth)
72
+ })
73
+
74
+ // userStatus
75
+ it('kraken.userStatus()', () => {
76
+ return expect(kraken.userStatus()).resolves.toBeKrakenSuccess()
77
+ })
78
+
79
+ // url
80
+ it('kraken.url()', () => {
81
+ const data = {
82
+ url: 'https://kraken-nekkraug.netdna-ssl.com/assets/images/kraken-logotype@2x.png',
83
+ wait: true,
84
+ dev: true
85
+ }
86
+
87
+ return expect(kraken.url(data)).resolves.toBeKrakenSuccess()
88
+ },MAX_TIMEOUT)
89
+
90
+ // url various formats
91
+ describe('kraken.upload({ file })', () => {
92
+ it('file<String> -> catch wrong file path', () => {
93
+ const data = new DevData({
94
+ file: 'test/support/images/wrong-file-path.gif'
95
+ })
96
+
97
+ return expect(kraken.upload(data)).rejects.toHaveProperty(
98
+ 'originalError.code',
99
+ 'ENOENT'
100
+ )
101
+ })
102
+
103
+ it('file<String> -> path', () => {
104
+ const data = new DevData({
105
+ file: 'test/support/images/image.gif'
106
+ })
107
+
108
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
109
+ })
110
+
111
+ it('file<Buffer>', () => {
112
+ const fileData = fs.readFileSync('test/support/images/image.gif')
113
+ const data = new DevData({
114
+ file: fileData
115
+ })
116
+
117
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
118
+ })
119
+
120
+ it('file<Stream>', () => {
121
+ const fileData = fs.createReadStream(
122
+ 'test/support/images/image.gif'
123
+ )
124
+ const data = new DevData({
125
+ file: fileData
126
+ })
127
+
128
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
129
+ })
130
+
131
+ it('file<Object>: path and options', () => {
132
+ const data = new DevData({
133
+ file: {
134
+ path: 'test/support/images/image.gif',
135
+ options: {}
136
+ }
137
+ })
138
+
139
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
140
+ })
141
+
142
+ it('file<Object>: <Buffer> and options', () => {
143
+ const fileData = fs.readFileSync('test/support/images/image.gif')
144
+ const data = new DevData({
145
+ file: {
146
+ value: fileData,
147
+ options: {}
148
+ }
149
+ })
150
+
151
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
152
+ })
153
+
154
+ it('file<Object>: <Stream> and options', () => {
155
+ const fileData = fs.createReadStream(
156
+ 'test/support/images/image.gif'
157
+ )
158
+ const data = new DevData({
159
+ file: {
160
+ value: fileData,
161
+ options: {}
162
+ }
163
+ })
164
+
165
+ return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
166
+ })
167
+ })
168
+ })
169
+
170
+ // Check if environment variable is defined
171
+ function CheckEnvVars(names) {
172
+ for (let name of names) {
173
+ if (!process.env[name]) {
174
+ throw new Error(`Not found environment variable: ${name}`)
175
+ }
176
+ }
177
+ }
Binary file
package/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.6"
4
- - "0.8"
package/lib/kraken.js DELETED
@@ -1,104 +0,0 @@
1
- 'use strict';
2
-
3
- /*!
4
- * Kraken
5
- * Copyright (c) 2013-2016 Kraken.io <support@kraken.io>
6
- * MIT Licensed
7
- */
8
-
9
-
10
- /**
11
- * Module dependencies
12
- */
13
-
14
- var fs = require("fs"),
15
- stream = require("stream"),
16
- request = require("request");
17
-
18
-
19
- /**
20
- * Constructor
21
- */
22
-
23
- var Kraken = module.exports = function (opts) {
24
- this.auth = {
25
- api_key: opts.api_key || "",
26
- api_secret: opts.api_secret || ""
27
- };
28
- };
29
-
30
-
31
- /**
32
- * Creates a http response handler
33
- *
34
- * @param {Function} cb
35
- */
36
- Kraken.prototype._createResponseHandler = function (cb) {
37
- return function (err, res, body) {
38
- if (err) {
39
- return cb(err);
40
- }
41
-
42
- // in case of unsuccessful request with {wait: true}
43
- if (body.success === false) {
44
- return cb(new Error(body.message));
45
- } else {
46
- return cb(undefined, body);
47
- }
48
- }
49
- };
50
-
51
-
52
- /**
53
- * Pass the given `image` URL along with credentials to Kraken API via HTTPS POST
54
- *
55
- * @param {Object} opts
56
- * @param {Function} cb
57
- * @api public
58
- */
59
-
60
- Kraken.prototype.url = function (opts, cb) {
61
- opts = opts || {};
62
-
63
- opts.auth = this.auth;
64
-
65
- request.post({
66
- url: "https://api.kraken.io/v1/url",
67
- json: true,
68
- strictSSL: false,
69
- body: opts
70
- }, this._createResponseHandler(cb));
71
- };
72
-
73
-
74
- /**
75
- * Upload the given `file` along with credentials to Kraken API via HTTPS POST.
76
- *
77
- * @param {Object} opts
78
- * @param {Function} cb
79
- * @api public
80
- */
81
-
82
- Kraken.prototype.upload = function (opts, cb) {
83
- opts = opts || {};
84
-
85
- opts.auth = this.auth;
86
-
87
- var formData = {};
88
-
89
- if (opts.file instanceof stream.Stream) {
90
- formData.file = opts.file;
91
- } else {
92
- formData.file = fs.createReadStream(opts.file);
93
- }
94
- delete opts.file;
95
-
96
- formData.data = JSON.stringify(opts);
97
-
98
- request.post({
99
- url: "https://api.kraken.io/v1/upload",
100
- json: true,
101
- strictSSL: false,
102
- formData: formData
103
- }, this._createResponseHandler(cb));
104
- };