Skip to content

Commit ac7cf2a

Browse files
author
Anthony Nowell
committed
add exists method; split out File-specific operations
1 parent f8666f4 commit ac7cf2a

File tree

7 files changed

+114
-53
lines changed

7 files changed

+114
-53
lines changed

examples/string-write.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ var client = algorithmia.client(process.env.ALGORITHMIA_API_KEY);
1313

1414
// === Create/Update file
1515
var content = "Hello this is a test";
16-
client.file("data://.my/TestCollection/foo.txt").putString(content, function(response) {
17-
console.log(response);
16+
17+
var f = client.file("data://.my/TestCollection/foo.txt");
18+
19+
f.exists(function(exists) {
20+
if(exists) {
21+
console.log("File already existed. Overwriting.");
22+
}
23+
f.putString(content, function(response) {
24+
console.log(response);
25+
});
1826
});
1927

28+

lib/algorithmia.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Algorithm, AlgorithmiaClient, Data, algorithmia, defaultApiAddress, exports, http, https, packageJson, url;
1+
var Algorithm, AlgorithmiaClient, File, algorithmia, defaultApiAddress, exports, http, https, packageJson, url;
22

33
https = require('https');
44

@@ -10,7 +10,7 @@ packageJson = require('../package.json');
1010

1111
Algorithm = require('./algorithm.js');
1212

13-
Data = require('./data.js');
13+
File = require('./file.js');
1414

1515
defaultApiAddress = 'https://api.algorithmia.com';
1616

@@ -34,7 +34,7 @@ AlgorithmiaClient = (function() {
3434
};
3535

3636
AlgorithmiaClient.prototype.file = function(path) {
37-
return new Data(this, path);
37+
return new File(this, path);
3838
};
3939

4040
AlgorithmiaClient.prototype.req = function(path, method, data, cheaders, callback) {

lib/data.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,19 @@ Data = (function() {
99
this.data_path = path.replace(/data\:\/\//, '');
1010
}
1111

12-
Data.prototype.putString = function(content, callback) {
13-
var headers;
14-
headers = {
15-
'Content-Type': 'text/plain'
16-
};
17-
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
18-
};
19-
20-
Data.prototype.putJson = function(content, callback) {
21-
var headers;
22-
headers = {
23-
'Content-Type': 'application/JSON'
24-
};
25-
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
26-
};
27-
28-
Data.prototype.getString = function(callback) {
29-
var headers;
30-
headers = {
31-
'Accept': 'text/plain'
32-
};
33-
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
34-
};
35-
36-
Data.prototype.getJson = function(callback) {
12+
Data.prototype.exists = function(callback) {
3713
var headers;
3814
headers = {
15+
'Content-Type': 'text/plain',
3916
'Accept': 'text/plain'
4017
};
41-
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
18+
return this.client.req('/v1/data/' + this.data_path, 'HEAD', '', headers, function(response, status) {
19+
if (status === 200) {
20+
return callback(true);
21+
} else {
22+
return callback(false);
23+
}
24+
});
4225
};
4326

4427
return Data;

lib/file.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var Data, File, exports,
2+
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
3+
hasProp = {}.hasOwnProperty;
4+
5+
Data = require('./data.js');
6+
7+
File = (function(superClass) {
8+
extend(File, superClass);
9+
10+
function File() {
11+
return File.__super__.constructor.apply(this, arguments);
12+
}
13+
14+
File.prototype.putString = function(content, callback) {
15+
var headers;
16+
headers = {
17+
'Content-Type': 'text/plain'
18+
};
19+
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
20+
};
21+
22+
File.prototype.putJson = function(content, callback) {
23+
var headers;
24+
headers = {
25+
'Content-Type': 'application/JSON'
26+
};
27+
return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback);
28+
};
29+
30+
File.prototype.getString = function(callback) {
31+
var headers;
32+
headers = {
33+
'Accept': 'text/plain'
34+
};
35+
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
36+
};
37+
38+
File.prototype.getJson = function(callback) {
39+
var headers;
40+
headers = {
41+
'Accept': 'application/JSON'
42+
};
43+
return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback);
44+
};
45+
46+
return File;
47+
48+
})(Data);
49+
50+
module.exports = exports = File;

src/algorithmia.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ url = require('url')
44
packageJson = require('../package.json')
55

66
Algorithm = require('./algorithm.js')
7-
Data = require('./data.js')
7+
File = require('./file.js')
88

99
defaultApiAddress = 'https://api.algorithmia.com'
1010

@@ -27,7 +27,7 @@ class AlgorithmiaClient
2727

2828
# file
2929
file: (path) ->
30-
new Data(this, path)
30+
new File(this, path)
3131

3232
# internal http-helper
3333
req: (path, method, data, cheaders, callback) ->

src/data.coffee

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,13 @@ class Data
1111

1212
@data_path = path.replace /data\:\/\//, ''
1313

14-
# put string
15-
putString: (content, callback) ->
14+
# This is similar to node's fs.exists function
15+
exists: (callback) ->
1616
headers =
1717
'Content-Type': 'text/plain'
18-
@client.req('/v1/data/' + @data_path, 'PUT', content, headers, callback)
19-
20-
# put json
21-
putJson: (content, callback) ->
22-
headers =
23-
'Content-Type': 'application/JSON'
24-
@client.req('/v1/data/' + @data_path, 'PUT', content, headers, callback)
25-
26-
# get string
27-
getString: (callback) ->
28-
headers =
29-
'Accept': 'text/plain'
30-
@client.req('/v1/data/' + @data_path, 'GET', '', headers, callback)
31-
32-
# get json
33-
getJson: (callback) ->
34-
headers =
3518
'Accept': 'text/plain'
36-
@client.req('/v1/data/' + @data_path, 'GET', '', headers, callback)
19+
@client.req('/v1/data/' + @data_path, 'HEAD', '', headers, (response, status) ->
20+
if status == 200 then callback(true) else callback(false)
21+
)
3722

3823
module.exports = exports = Data

src/file.coffee

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Data.coffee
3+
#
4+
5+
Data = require('./data.js')
6+
7+
8+
class File extends Data
9+
# put string
10+
putString: (content, callback) ->
11+
headers =
12+
'Content-Type': 'text/plain'
13+
@client.req('/v1/data/' + @data_path, 'PUT', content, headers, callback)
14+
15+
# put json
16+
putJson: (content, callback) ->
17+
headers =
18+
'Content-Type': 'application/JSON'
19+
@client.req('/v1/data/' + @data_path, 'PUT', content, headers, callback)
20+
21+
# get string
22+
getString: (callback) ->
23+
headers =
24+
'Accept': 'text/plain'
25+
@client.req('/v1/data/' + @data_path, 'GET', '', headers, callback)
26+
27+
# get json
28+
getJson: (callback) ->
29+
headers =
30+
'Accept': 'application/JSON'
31+
@client.req('/v1/data/' + @data_path, 'GET', '', headers, callback)
32+
33+
34+
module.exports = exports = File

0 commit comments

Comments
 (0)