Permalink
Browse files

Add code for creating a thing. Also fix problems that occur when Flat…

…tr returns non-json.
  • Loading branch information...
simme committed Feb 12, 2012
1 parent 8629766 commit d90c3530f93bda948af8c0c4beadba4c171e3dbb
Showing with 68 additions and 5 deletions.
  1. +34 −2 flattr.coffee
  2. +34 −3 flattr.js
View
@@ -145,6 +145,32 @@ class root.Flattr
@client.get endpoint, options, callback
+ #
+ # ## _function_ createThing(parameters, callback)
+ #
+ # * `url` the thing's URL.
+ # * `title` the thing's title.
+ # * `description` the thing's description.
+ # * `category` the thing's category.
+ # * `language` the thing's language.
+ # * `tags` an array of tags.
+ # * `hidden` true if this thing is hidden.
+ #
+ # _All the parameters except **URL** is optional._
+ #
+ createThing: (parameters, callback) ->
+ if not @options.access_token
+ callback {error: 'missing_access_token'}
+
+ options =
+ headers:
+ "Authorization": "Bearer #{@options.access_token}"
+
+ if parameters.tags
+ parameters.tags = parameters.tags.join ','
+
+ @client.post "#{@api_endpoint}/things", parameters, options, callback
+
# ---------------------------------------------------------------------------
#
@@ -447,7 +473,10 @@ class NodeHTTP
data += chunk
res.on 'end', () ->
- callback null, JSON.parse data
+ if res.statusCode != 200
+ callback data, null
+ else
+ callback null, JSON.parse data
res.on 'error', (error) ->
callback error, null
@@ -485,7 +514,10 @@ class NodeHTTP
data += chunk
res.on 'end', () ->
- callback null, JSON.parse data
+ if res.statusCode != 200
+ callback data, null
+ else
+ callback null, JSON.parse data
res.on 'error', (error) ->
callback error, null
View
@@ -55,11 +55,17 @@
return this.client.get(endpoint, null, headers, callback);
};
Flattr.prototype.things = function(ids, callback) {
- var endpoint, parameters;
+ var endpoint, headers, parameters;
endpoint = "" + this.api_endpoint + "/things";
parameters = {
id: ids.join(',')
};
+ headers = {};
+ if (this.options.access_token) {
+ headers = {
+ "Authorization": "Bearer " + this.options.access_token
+ };
+ }
return this.client.get(endpoint, parameters, headers, callback);
};
Flattr.prototype.lookup = function(url, callback) {
@@ -81,6 +87,23 @@
}
return this.client.get(endpoint, options, callback);
};
+ Flattr.prototype.createThing = function(parameters, callback) {
+ var options;
+ if (!this.options.access_token) {
+ callback({
+ error: 'missing_access_token'
+ });
+ }
+ options = {
+ headers: {
+ "Authorization": "Bearer " + this.options.access_token
+ }
+ };
+ if (parameters.tags) {
+ parameters.tags = parameters.tags.join(',');
+ }
+ return this.client.post("" + this.api_endpoint + "/things", parameters, options, callback);
+ };
Flattr.prototype.userFlattrs = function(username, count, page, callback) {
var endpoint, parameters;
if (arguments.length === 3) {
@@ -263,7 +286,11 @@
return data += chunk;
});
res.on('end', function() {
- return callback(null, JSON.parse(data));
+ if (res.statusCode !== 200) {
+ return callback(data, null);
+ } else {
+ return callback(null, JSON.parse(data));
+ }
});
return res.on('error', function(error) {
return callback(error, null);
@@ -297,7 +324,11 @@
return data += chunk;
});
res.on('end', function() {
- return callback(null, JSON.parse(data));
+ if (res.statusCode !== 200) {
+ return callback(data, null);
+ } else {
+ return callback(null, JSON.parse(data));
+ }
});
return res.on('error', function(error) {
return callback(error, null);

0 comments on commit d90c353

Please sign in to comment.