Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Open the ./lib/index.js file, uncomment the line containing Capabilities.CanRetrieve, and save the file. The log output tells us to update four new files that API Builder just generated.

    Code Block
    The "CanRetrieve" capability has been enabled, so we need to make a couple of changes:
     - Created `lib/methods/distinct.js` (contains 3 TODOs)
     - Created `lib/methods/findAll.js` (contains 3 TODOs)
     - Created `lib/methods/findById.js` (contains 4 TODOs)
     - Created `lib/methods/query.js` (contains 5 TODOs)
    (Hint: If you only want to support some of these methods, feel free to delete the others.)
    Please go take a look at the TODOs in these new files, then do an `appc run` or `npm test` to try out the new capabilities.
  2. Remove the distinct.js and query.js files. We will not be implementing these for the example.

    Code Block
    title./lib/lifecycle/connect.js
    var Twitter = require('twitter');
    
    exports.connect = function (next) {
      // Initialize the client
      // Use this.config to get values from the configuration file
      this.client = new Twitter({
        consumer_key: this.config.consumer_key,
        consumer_secret: this.config.consumer_secret,
        access_token_key: this.config.access_token_key,
        access_token_secret: this.config.access_token_secret
      });    
      next();
    };
  3. Open ./lib/methods/findAll.js and replace the contents of the file with the following. The findAll.js file implements the connector's findAll() method, which retrieves all tweets from the user's Twitter account, or at least, as many tweets as Twitter will allow us to fetch.

    Code Block
    title./lib/methods/findAll.js
    var Arrow = require('arrow'),
      Collection = Arrow.Collection,
      ORMError = Arrow.ORMError;
    
    exports.findAll = function findAll(Model, callback) {
        var params = {screen_name: this.config.account};
        // this Twitter API only returns the last twenty tweets
        this.client.get('statuses/user_timeline', params, function(error, tweets, response) {
        if (!error) {
          var results = [];
          for (var i = 0; i < tweets.length; i++) {
            results.push(Model.connector.createInstance(Model, tweets[i]));
          }
          callback(null, new Collection(Model, results));
        } else {
          this.logger.error(error);
          callback(new ORMError('ERROR: Could not fetch tweets!'));
        }
      });
    };
  4. Open ./lib/methods/findById.js and replace the contents of the file with the following. The findById.js file implements the connector's findById() method, which retrieves one specific tweet, identified by its ID, from the datasource.

    Code Block
    title./lib/methods/findById.js
    var Arrow = require('arrow'),
      ORMError = Arrow.ORMError;
    
    exports.findById = function (Model, id, callback) {
      this.client.get('statuses/show/' + id, {}, function(error, tweet, response) {
        if (!error) {
          callback(null, Model.connector.createInstance(Model, tweet));
        } else {
          this.logger.error(error);
          callback(new ORMError('ERROR: Could not fetch tweet!'));
        }
      });
    };

...