presbeus 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/presbeus +118 -29
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a79d36d91ed9bbb866449d4d61a21d6e350e6940
4
- data.tar.gz: 55ed1fe5a961b6b8c69cb6fe2624c720475f6422
3
+ metadata.gz: 5eccbc0625356b786735b43c6a71237c12f761dc
4
+ data.tar.gz: 957e3c0d6604fa0875ac5e6662fcbaa30fe55663
5
5
  SHA512:
6
- metadata.gz: 0f6b209849fb61eedf41e434c10c93238452f81f942218c090130b2e43ea607bea3995aa64e6e01f88aeddc31865ef38ecbde888a0750391c710dafaec866ae0
7
- data.tar.gz: d2c85c35db0bb92cea7b10a6fce426d77bdf47cc92dfe58585cb37c73c4c47e07a1ca72a9747465656788b68c3833cc33a4d0aed9b064189d76cf47174702b1f
6
+ metadata.gz: 644f5372cab7a69065eb71e35b854efb0154c024ddcee54cbd5cd2db3adf4cad1d73c9fed53c3fb0da994c26dec0196d53705fb2c7ccf313c90f85a3d871abf1
7
+ data.tar.gz: ac7c6a6dafbd0d1d40847e0001b0bfaa4700b87c12f8fbb0d078910b7e13c9079e10d546a7076f27c73c685a3cfdecd0ca39110a79089dde1abcc221be42b5d6
data/bin/presbeus CHANGED
@@ -6,14 +6,49 @@ require 'time_ago_in_words'
6
6
  require 'terminal-table'
7
7
  require 'highline'
8
8
  require 'colorize'
9
+ require "readline"
10
+
11
+ class Time
12
+
13
+ alias_method :ago_in_words_no_timezone, :ago_in_words
14
+
15
+ end
9
16
 
10
17
  class Presbeus
11
18
 
19
+ def initialize_arguments
20
+ argument(:help, [], "show this help") { help }
21
+ argument(:shell, [], "run in an interactive shell") { shell }
22
+ argument(:devices, [], "list devices") { puts table devices }
23
+ argument(:pushes, [], "list pushes") { puts table pushes_with_color }
24
+ argument(:threads, [:device_id], "show threads for device") do
25
+ |c| puts table threads c[0]
26
+ end
27
+ argument(:last, [:device_id], "show last thread for device") do |c|
28
+ puts table last_thread c[0]
29
+ end
30
+ argument(:push, [:all], "push note to all devices") do |c|
31
+ push c.join(" ")
32
+ end
33
+ argument(:thread, [:device_id, :thead_id], "show SMS thread") do |c|
34
+ puts table thread_with_two_columns_wrap c[0], c[1]
35
+ end
36
+ argument(:sms, [:device_id, :phone_number, :all], "send SMS") do |c|
37
+ send_sms c[0], c[1], c[2..-1].join(" ")
38
+ end
39
+ end
40
+
12
41
  def initialize
42
+ initialize_arguments
13
43
  configuration_path = "#{ENV['HOME']}/.config/presbeus.yml"
14
44
  configuration = YAML.load_file configuration_path
15
45
  token = `#{configuration['password_command']}`
16
- @headers = {"Access-Token" => token}
46
+ @headers = {"Access-Token" => token, "Content-Type" => "json"}
47
+ end
48
+
49
+ def post_v2 what, payload
50
+ RestClient.post(
51
+ 'https://api.pushbullet.com/v2/' + what, payload.to_json, @headers)
17
52
  end
18
53
 
19
54
  def get_v2 what
@@ -39,31 +74,42 @@ class Presbeus
39
74
  get_v2("permanents/#{device}_thread_#{id}")["thread"]
40
75
  end
41
76
 
77
+ def pushes
78
+ get_v2("pushes")["pushes"].map do |push|
79
+ [Time.at(push["created"].to_i).ago_in_words, push["body"]]
80
+ end
81
+ end
82
+
42
83
  def user_iden
43
84
  get_v2("users/me")["iden"]
44
85
  end
45
86
 
87
+ def push message
88
+ post_v2 "pushes", {
89
+ title: "push from presbeus",
90
+ type: "note",
91
+ body: message,
92
+ }
93
+ end
94
+
46
95
  def send_sms device, conversation_iden, message
47
- RestClient.post(
48
- 'https://api.pushbullet.com/v2/ephemerals',
49
- {
50
- push: {
51
- conversation_iden: conversation_iden,
52
- message: message,
53
- package_name: "com.pushbullet.android",
54
- source_user_iden: user_iden,
55
- target_device_iden: device,
56
- type: "messaging_extension_reply"
57
- },
58
- type: "push"
59
- }.to_json,
60
- @headers)
96
+ post_v2 "ephemerals", {
97
+ push: {
98
+ conversation_iden: conversation_iden,
99
+ message: message,
100
+ package_name: "com.pushbullet.android",
101
+ source_user_iden: user_iden,
102
+ target_device_iden: device,
103
+ type: "messaging_extension_reply"
104
+ },
105
+ type: "push"
106
+ }
61
107
  end
62
108
 
63
109
  def table rows, config = {}
64
110
  t = Terminal::Table.new config.merge({:rows => rows})
65
111
  t.style = { border_top: false, border_bottom: false, border_y: '' }
66
- puts t
112
+ t
67
113
  end
68
114
 
69
115
  def wrap s, width
@@ -75,18 +121,29 @@ class Presbeus
75
121
  lines.map { |line| line.ljust(width).colorize(color: :black, background: background)}.join "\n"
76
122
  end
77
123
 
124
+ def width
125
+ HighLine::SystemExtensions.terminal_size[0]
126
+ end
127
+
128
+ def half_width
129
+ width / 2 - 4
130
+ end
131
+
132
+ def width_2
133
+ width - 4
134
+ end
135
+
78
136
  def thread_with_two_columns_wrap device, id
79
- width = HighLine::SystemExtensions.terminal_size[0] / 2 - 4
80
137
  res = []
81
138
  thread(device, id).reverse.each do |message|
82
- text = [wrap(message["body"], width), ""]
139
+ text = [wrap(message["body"], half_width), ""]
83
140
  date = ["\n#{Time.at(message["timestamp"]).ago_in_words}", ""]
84
141
  if message["direction"] != "incoming"
85
- text[0] = with_color text[0], width, :white
142
+ text[0] = with_color text[0], half_width, :white
86
143
  text.reverse!
87
144
  date.reverse!
88
145
  else
89
- text[0] = with_color text[0], width, :green
146
+ text[0] = with_color text[0], half_width, :green
90
147
  end
91
148
  res << date
92
149
  res << text
@@ -94,17 +151,49 @@ class Presbeus
94
151
  res
95
152
  end
96
153
 
154
+ def pushes_with_color
155
+ pushes.reverse.map do |push|
156
+ ["\n" + push[0] + "\n" + with_color(wrap(push[1], width_2), width_2, :green)]
157
+ end
158
+ end
159
+
160
+ def last_thread device
161
+ thread_with_two_columns_wrap device, threads(device).last[0]
162
+ end
163
+
164
+ def argument name, args, description
165
+ @arguments ||= {}
166
+ @arguments[name] = {args: args, description: description, block: Proc.new { |c| yield c } }
167
+ end
168
+
169
+ def help
170
+ a = @arguments.to_a.map do |_|
171
+ [_[0].to_s, _[1][:args].map { |x| x.to_s }.join(" "), _[1][:description]]
172
+ end
173
+ puts table a
174
+ end
175
+
176
+ def shell
177
+ while buf = Readline.readline("> ", true)
178
+ run buf.split
179
+ end
180
+ end
181
+
182
+ def good_syntax? name, command
183
+ args = @arguments[name][:args]
184
+ count = args.size + 1
185
+ args.include?(:all) ? command.size >= count : command.size == count
186
+ end
187
+
97
188
  def run command
98
- case command[0]
99
- when "devices"
100
- puts table devices
101
- when "threads"
102
- puts table threads command[1]
103
- when "thread"
104
- puts table thread_with_two_columns_wrap command[1], command[2]
105
- when "sms"
106
- send_sms command[1], command[2], command[3..-1].join(" ")
189
+ if command.size > 0
190
+ name = command[0].to_sym
191
+ if @arguments.keys.include?(name) and good_syntax?(name, command)
192
+ @arguments[name][:block].call command[1..-1]
193
+ return
194
+ end
107
195
  end
196
+ help
108
197
  end
109
198
 
110
199
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presbeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olivier Abdesselam