forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopibar.15m.js
executable file
·191 lines (160 loc) · 5.46 KB
/
Shopibar.15m.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env /usr/local/bin/node
/*
* <xbar.title>Shopibar</xbar.title>
* <xbar.version>v1.0.1</xbar.version>
* <xbar.author>Etka Özdemir</xbar.author>
* <xbar.author.github>m-etka</xbar.author.github>
* <xbar.image>http://i.imgur.com/Dt499Nh.png</xbar.image>
* <xbar.desc>Shopify Admin Plugin for BitBar</xbar.desc>
* <xbar.dependencies>node</xbar.dependencies>
*
*/
/*
* Visit http://shopibar.etka.org for installation and details
*/
/* Shopibar configuration */
var admin = {
myShopifyAccountName: 'example.myshopify.com',
apiKey: 'example.com_api_key',
password: 'example.com_password'
};
showCustomerCount = true;
showPendingOrders = true;
showUnshippedOrders = true;
/* End of Shopibar configuration */
var verbose = true;
// Start plug-in
console.log('🛒');
console.log('---');
printBitBarLine('Shopibar', ['color=black', 'font=Phosphate', 'size=20']);
// Get data using Shopify Admin API
get(admin, 'shop', function(data) {
// Show shop info
printBitBarLine(data.shop.name, ['color=black', 'font=Calibri', 'size=18']);
if (showCustomerCount) {
// Get customer count
get(admin, 'customerCount', function(data) {
// Show customer count
printBitBarLine('Customer Count:', ['color=gray', 'font=Calibri', 'size=14']);
printBitBarLine(data.count, ['color=black', 'font=Calibri', 'size=16']);
});
}
if (showPendingOrders) {
// Get pending orders
get(admin, 'pendingOrders', function(data) {
printOrders(data, 'Pending Orders');
});
}
if (showUnshippedOrders) {
// Get unshipped orders
get(admin, 'unshippedOrders', function(data) {
printOrders(data, 'Unshipped Orders');
});
}
});
// Print order details
function printOrders(data, title) {
// Show orders
printBitBarLine(' ');
printBitBarLine(title + ':', ['color=gray', 'font=Calibri', 'size=14']);
printBitBarLine(data.orders.length, ['color=black', 'font=Calibri', 'size=16']);
for (var i = 0; i < data.orders.length; i++) {
// Show order price and user info
printBitBarLine('--$' + data.orders[i].total_line_items_price, ['color=black', 'font=Calibri', 'size=16']);
printBitBarLine('--' + data.orders[i].email, ['font=Calibri', 'size=14']);
// Show items ordered
for (var j = 0; j < data.orders[i].line_items.length; j++) {
if (j !== 0) {
printBitBarLine('----');
}
printBitBarLine('----' + data.orders[i].line_items[j].title, ['font=Calibri', 'size=16', 'color=black']);
printBitBarLine('----');
printBitBarLine('----Name : ' + data.orders[i].line_items[j].name, ['font=Monaco', 'size=11']);
printBitBarLine('----SKU : ' + data.orders[i].line_items[j].sku, ['font=Monaco', 'size=11']);
printBitBarLine('----Price : $' + data.orders[i].line_items[j].price, ['font=Monaco', 'size=11']);
printBitBarLine('----Quantity : ' + data.orders[i].line_items[j].quantity, ['font=Monaco', 'size=11']);
}
printBitBarLine('-----');
}
}
// Get
function get(admin, requestName, callback) {
var endpoint = {
url: '',
requestType: 'GET'
};
switch (requestName) {
case 'shop':
endpoint.url = '/admin/shop.json';
endpoint.requestType = 'GET';
break;
case 'customerCount':
endpoint.url = '/admin/customers/count.json';
endpoint.requestType = 'GET';
break;
case 'pendingOrders':
endpoint.url = '/admin/orders.json?status=pending';
endpoint.requestType = 'GET';
break;
case 'unshippedOrders':
endpoint.url = '/admin/orders.json?fulfillment_status=unshipped';
endpoint.requestType = 'GET';
break;
default:
break;
}
performRequest(admin, endpoint.url, endpoint.requestType, function(data) {
if (data.errors && verbose) {
console.log('Errors: ' + data.errors);
} else {
callback(data);
}
});
}
// HTTPS request
function performRequest(admin, endpoint, method, success) {
var https = require('https');
var auth = new Buffer(admin.apiKey + ':' + admin.password).toString('base64');
var headers = {};
headers = {
'Authorization': 'Basic ' + auth,
'Content-Type': 'application/json'
};
var options = {
host: admin.myShopifyAccountName,
path: endpoint,
method: method,
headers: headers
};
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var responseObject = JSON.parse(responseString);
success(responseObject);
});
});
req.end();
req.on('error', function(err) {
console.log('Error: Could not connect! Check your credientials and connection.');
if (verbose) {
console.log(err);
}
});
}
// Plug-in output
function printBitBarLine(title, args) {
var lineArgs = [];
if (title !== '-----') {
for (var i in args) {
lineArgs.push(args[i]);
}
if (lineArgs.length !== 0) {
title = title + '|';
}
}
console.log(title + lineArgs.join(' '));
}