npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

robot-spec

v0.1.2

Published

customize robot events hear and response. apply robot and spec.

Downloads

13

Readme

RobotSpec

メッセージ受信イベントの正規表現の省略や、受信したテキストメッセージ内容の取得を容易に行うことができるdaab開発の為のパッケージ

Require

node6+

Usage

・メッセージ内容の簡易取得

scripts/app.js

const RobotSpec = require('robot-spec');


module.exports = (robot) => {

  const robot2 = new RobotSpec(robot);

  robot2.hear(/PING$/i, ({ res, msg }) => {
    res.send('.PONG > ' + msg);
  });

  robot2.hear('text', ({ res, msg }) => {
    res.send('.TEXT > ' + msg);
  });

  robot2.hear('select', ({ res }) => {
    res.send('.SELECT-STAMP ');
  });

  robot2.respond(/ECHO (.*)_(.*)/i, ({ res, msg, msgs }) => {
    res.send('.ECHO INPUT >\n' + msgs.join('\n'));
  });

};

・メソッドのキャッシュと呼び出し

scripts/app.js

const RobotSpec = require('robot-spec');


module.exports = (robot) => {

  const robot2 = new RobotSpec(robot).config();
  const spec = robot2.spec;


  robot2.hear('text', ({ res, msg }) => spec.scenario.textReceive({ res, msg }));
  robot2.hear('select', ({ res }) => spec.scenario.selectReceive({ res }));
  robot2.hear('stamp', ({ res }) => spec.scenario.stampReceive({ res }));

  robot.hear(/PING$/i, (res) => {
    res.send('PONG');
  });

  robot2.respond(/TEST (.*)_(.*)/i, ({ res, msg, msgs }) => {
    res.send('test!\n' + msgs.join('\n'));
  });

  robot2.hear(/TEST (.*)_(.*)/i, ({ res, msg, msgs }) => {
    res.send('test!!\n' + msgs.join('\n'));
    console.log(msg, msgs);
  });

};

・メソッドの初期化を行う(constructorの実行)

initialize

const robot2 = new RobotSpec(robot).config().initialize();
const spec = robot2.spec;

robot2.spec

キャッシュしたメソッドの利用

scripts/app.js

const robot2 = new RobotSpec(robot).config();
const spec = robot2.spec;

spec.scenario.method();
// or
robot2.hear('stamp', ({ res }) => spec.scenario.method());

別のモジュールではもう一度呼び出して使用することも可能

const spec = require('robot-spec').Spec();

spec.scenario.method();

コンポーネント設定

ディレクトリ構造

scripts/app.js
src
├── components.js

コアメソッドのキャッシュ

src/components.js

'use strict';

const components = (robot, config) => ({
  robot,
  config,

  // scenario
  scenario: './scenario/common',
  task:     './scenario/task'
});

module.exports = components;

cache method

メソッドのキャッシュ

ディレクトリ構造

scripts/app.js
src
├── scenario
│   ├── common
│   │   ├── index.js // *
│   │   ├── join-home.js
│   │   ├── select-action-admin.js
│   │   ├── select-receive.js
│   │   ├── stamp-receive.js
│   │   ├── text-action-admin.js
│   │   └── text-receive.js

scenario/common/index.js

'use strict';

class Common {

  constructor(components) {
    components.mixin(this, __dirname, {
      joinHome:             './join-home',
      selectActionAdmin:    './select-action-admin',
      selectReceive:        './select-receive',
      stampReceive:         './stamp-receive',
      textActionAdmin:      './text-action-admin',
      textReceive:          './text-receive',
    });
  }

}

module.exports = Common;

クラス

'use strict';

class Schedules {

  constructor(spec) {
    this.spec = spec;
  }

  add(args) {
    return this.spec.apiSchedule.add(args);
  }
}

module.exports = Schedules;

メソッド

scenario/common/join-home.js

'use strict';

const joinHome = ({ messages }) => {
  return ({ res }) => {

    return messages.joinUser({ res });
  };
};

module.exports = joinHome;

クラス、メソッド共にモジュールから読み込む方法でも可能

(再掲)

const spec = require('robot-spec').Spec();

spec.scenario.method();

クラス、メソッドのキャッシュ

scripts/app.js

メソッドのディレクトリ設定

new RobotSpec(robot).config(opts);

optsの設定を変更することで、各設置ディレクトリの変更が可能です。

opts

デフォルト

opts = {
  root: 'src/',
  config: 'src/config',
  components: 'src/components'
};

config.jsは定数を定義し、グローバル利用できます。
利用しない場合は、falseを設定します。

opts = {
  root: 'src/',
  config: false,
  components: 'src/components'
};

config.jsを利用し、components.js(メソッドキャッシュ)を利用しない場合は、
下記のように、componentsfalseを設定します。

opts = {
  root: 'src/',
  config: 'src/config',
  components: false
};

または、componentsのみ設定します。

opts = {
  components: false
};

グローバル定数

src/config.js

const g = {};

g.ADMIN_USER = 'xxxxxxxx';
g.ADMIN_PASS = 'xxxxxxxx';

module.exports = g;