Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…tr returns non-json.
  • Loading branch information...
commit d90c3530f93bda948af8c0c4beadba4c171e3dbb 1 parent 8629766
Simon Ljungberg authored February 12, 2012

Showing 2 changed files with 68 additions and 5 deletions. Show diff stats Hide diff stats

  1. 36  flattr.coffee
  2. 37  flattr.js
36  flattr.coffee
@@ -145,6 +145,32 @@ class root.Flattr
145 145
 
146 146
     @client.get endpoint, options, callback
147 147
 
  148
+  #
  149
+  # ## _function_ createThing(parameters, callback)
  150
+  #
  151
+  # * `url` the thing's URL.
  152
+  # * `title` the thing's title.
  153
+  # * `description` the thing's description.
  154
+  # * `category` the thing's category.
  155
+  # * `language` the thing's language.
  156
+  # * `tags` an array of tags.
  157
+  # * `hidden` true if this thing is hidden.
  158
+  #
  159
+  # _All the parameters except **URL** is optional._
  160
+  #
  161
+  createThing: (parameters, callback) ->
  162
+    if not @options.access_token
  163
+      callback {error: 'missing_access_token'}
  164
+
  165
+    options =
  166
+      headers:
  167
+        "Authorization": "Bearer #{@options.access_token}"
  168
+
  169
+    if parameters.tags
  170
+      parameters.tags = parameters.tags.join ','
  171
+
  172
+    @client.post "#{@api_endpoint}/things", parameters, options, callback
  173
+
148 174
 # ---------------------------------------------------------------------------
149 175
 
150 176
   #
@@ -447,7 +473,10 @@ class NodeHTTP
447 473
         data += chunk
448 474
 
449 475
       res.on 'end', () ->
450  
-        callback null, JSON.parse data
  476
+        if res.statusCode != 200
  477
+          callback data, null
  478
+        else
  479
+          callback null, JSON.parse data
451 480
 
452 481
       res.on 'error', (error) ->
453 482
         callback error, null
@@ -485,7 +514,10 @@ class NodeHTTP
485 514
         data += chunk
486 515
 
487 516
       res.on 'end', () ->
488  
-        callback null, JSON.parse data
  517
+        if res.statusCode != 200
  518
+          callback data, null
  519
+        else
  520
+          callback null, JSON.parse data
489 521
 
490 522
       res.on 'error', (error) ->
491 523
         callback error, null
37  flattr.js
@@ -55,11 +55,17 @@
55 55
       return this.client.get(endpoint, null, headers, callback);
56 56
     };
57 57
     Flattr.prototype.things = function(ids, callback) {
58  
-      var endpoint, parameters;
  58
+      var endpoint, headers, parameters;
59 59
       endpoint = "" + this.api_endpoint + "/things";
60 60
       parameters = {
61 61
         id: ids.join(',')
62 62
       };
  63
+      headers = {};
  64
+      if (this.options.access_token) {
  65
+        headers = {
  66
+          "Authorization": "Bearer " + this.options.access_token
  67
+        };
  68
+      }
63 69
       return this.client.get(endpoint, parameters, headers, callback);
64 70
     };
65 71
     Flattr.prototype.lookup = function(url, callback) {
@@ -81,6 +87,23 @@
81 87
       }
82 88
       return this.client.get(endpoint, options, callback);
83 89
     };
  90
+    Flattr.prototype.createThing = function(parameters, callback) {
  91
+      var options;
  92
+      if (!this.options.access_token) {
  93
+        callback({
  94
+          error: 'missing_access_token'
  95
+        });
  96
+      }
  97
+      options = {
  98
+        headers: {
  99
+          "Authorization": "Bearer " + this.options.access_token
  100
+        }
  101
+      };
  102
+      if (parameters.tags) {
  103
+        parameters.tags = parameters.tags.join(',');
  104
+      }
  105
+      return this.client.post("" + this.api_endpoint + "/things", parameters, options, callback);
  106
+    };
84 107
     Flattr.prototype.userFlattrs = function(username, count, page, callback) {
85 108
       var endpoint, parameters;
86 109
       if (arguments.length === 3) {
@@ -263,7 +286,11 @@
263 286
           return data += chunk;
264 287
         });
265 288
         res.on('end', function() {
266  
-          return callback(null, JSON.parse(data));
  289
+          if (res.statusCode !== 200) {
  290
+            return callback(data, null);
  291
+          } else {
  292
+            return callback(null, JSON.parse(data));
  293
+          }
267 294
         });
268 295
         return res.on('error', function(error) {
269 296
           return callback(error, null);
@@ -297,7 +324,11 @@
297 324
           return data += chunk;
298 325
         });
299 326
         res.on('end', function() {
300  
-          return callback(null, JSON.parse(data));
  327
+          if (res.statusCode !== 200) {
  328
+            return callback(data, null);
  329
+          } else {
  330
+            return callback(null, JSON.parse(data));
  331
+          }
301 332
         });
302 333
         return res.on('error', function(error) {
303 334
           return callback(error, null);

0 notes on commit d90c353

Please sign in to comment.
Something went wrong with that request. Please try again.