Passed
Push — master ( 6faae8...2cba04 )
by Dmytro
01:34
created

Telegraph.test   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
import remarkParse from 'remark-parse';
2
import unified from 'unified';
3
import { fill } from 'myrmidon';
4
import remarkTelegraph from 'remark-telegraph';
5
import Api from './TelegraphAPI';
6
7
function dumpAccount(account) {
8
    return account.short_name;
9
}
10
11
function dumpPage(page) {
12
    return {
13
        'telegraph_url'   : page.url,
14
        'telegraph_title' : page.title
15
    };
16
}
17
18
export default class Telegraph {
19
    constructor(token) {
20
        this.api = new Api(token);
21
    }
22
23
    async send(title, message, variables) {
24
        const mdTitle = fill(title, variables);
25
        const mdMessage = fill(message, variables);
26
27
        const content = await unified()
28
            .use(remarkParse)
29
            .use(remarkTelegraph)
30
            .process(mdMessage);
31
32
        const page = await this.api.createPage(mdTitle, String(content));
33
34
        return dumpPage(page);
35
    }
36
37
    async authorize() {
38
        const account = await this.api.createAccount('Semantic Release Telegram');
39
40
        this.api.setToken(account.access_token);
41
42
        return account.access_token;
43
    }
44
45
    async test() {
46
        const account = await this.api.getAccountInfo();
47
48
        return dumpAccount(account);
49
    }
50
}
51
52