1.0.1 • Published 5 years ago

twurlrc v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

twurlrc

Package Version

Parse .twurlrc files.

twurl stores Twitter credentials in ~/.twurlrc (in YAML format). This package will parse a .twurlrc file and return credentials suitable for use with oauth-1.0a.

Install

npm install twurlrc

Quick Start

The got package provides an excellent example of how to send a signed OAuth request with oauth-1.0a. A modified version of that example is included below to illustrate how to incorporate twurlrc:

const got = require('got');
const crypto  = require('crypto');
const OAuth = require('oauth-1.0a');
const {consumer, access_token} = require('twurlrc').fromFileSync().defaultCredentials();

const oauth = OAuth({
    consumer,
    signature_method: 'HMAC-SHA1',
    hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
});

const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';

got(url, {
    headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, access_token)),
    json: true
});

The Twurlrc class is used to get credentials from parsed data.

Call Twurlrc.fromFile or Twurlrc.fromFileSync to create a Twurlrc object and load the contents of the specified file path. If no path is specified ~/.twurlrc is loaded by default.

Call Twurlrc.fromYAMLSync to create a Twurlrc object from a YAML string.

API

Table of Contents

Twurlrc

Get credentials from parsed data.

Parameters

  • data Object object returned by YAML parser
    • data.configuration Object
      • data.configuration.default_profile Array 0: screen_name, 1: consumer_key
    • data.profiles Object credentials grouped by screen_name then consumer_key

data

object returned by YAML parser

Type: Object

credentials

Get credentials for the specified profile.

Returns grouped key/secret pairs by default. Disable grouping by passing true for the raw parameter value.

If the consumer_key parameter is omitted or falsy, the "first" consumer_key defined for provided screen_name is used. This fallback is provided for convenience when parsing profiles with a single consumer_key defined. To load credentials from profiles with multiple consumer_keys defined, specifying which consumer_key to load is strongly recommended (as opposed to relying on definition order).

Parameters
  • screen_name string Twitter user screen_name
  • consumer_key string Twitter app consumer_key
  • raw boolean return unmodified credentials object instead of grouping key/secret pairs (optional, default false)
Examples
// get credentials with grouped key/secret pairs
const {consumer, access_token, screen_name} = twurlrc.credentials('my_screen_name', 'my_consumer_key');
// consumer === {key: consumer_key, secret: consumer_secret}
// access_token === {key: token, secret: secret}
// screen_name === 'my_screen_name'
// disable grouping
const {consumer_key, consumer_secret, token, secret, username} = twurlrc.credentials('my_screen_name', 'my_consumer_key', true);

Returns Object

defaultCredentials

Get credentials for the default profile.

Returns grouped key/secret pairs by default. Disable grouping by passing true for the raw parameter value.

Parameters
  • raw boolean return unmodified credentials object instead of grouping key/secret pairs (optional, default false)
Examples
// get default credentials with grouped key/secret pairs
const {consumer, access_token, screen_name} = twurlrc.defaultCredentials();
// consumer === {key: consumer_key, secret: consumer_secret}
// access_token === {key: token, secret: secret}
// screen_name === 'my_screen_name'
// disable grouping
const {consumer_key, consumer_secret, token, secret, username} = twurlrc.defaultCredentials(true);

Returns Object

fromYAMLSync

Create a Twurlrc object from a YAML string.

Parameters
Examples
try {
    const twurlrc = require('twurlrc').fromYAMLSync('YAML STRING');
    // use twurlrc
}
 catch(error) {
    console.error(error.message);
}

Returns Twurlrc

fromFileSync

Create a Twurlrc object from a file path.

Parameters
  • filePath string path to .twurlrc file
Examples
try {
    const twurlrc = require('twurlrc').fromFileSync('/path/to/.twurlrc');
    // use twurlrc
}
 catch(error) {
    console.error(error.message);
}

Returns Twurlrc

fromFile

Asynchronously create a Twurlrc object from a file path.

Parameters
  • filePath string path to .twurlrc file
Examples
const Twurlrc = require('twurlrc');
Twurlrc.fromFile('/path/to/.twurlrc')
     .then((twurlrc) => {
         // use twurlrc
     })
     .catch((error) => console.error(error.message));

Returns Promise

Development

Clone the repository and cd into the resulting directory.

Install Dependencies

npm install

Run Tests

npm test

Note: Tests expect a ~/.twurlrc file to exist. The easiest way to create one is with twurl.

Built With

Contributing

Fork the repo and submit a pull request.

Versioning

SemVer is used for versioning. For the versions available, see the tags on this repository.

Author

Adam Jarret

License

This project is licensed under the MIT License. See the LICENSE.txt file for details.