gnome-campfire-notifications 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee6669e33d2234f27512e5ed8ef72371cbe126e6
4
+ data.tar.gz: 7a629596fa2ab474e4d258f5f8e5c51e757eedc4
5
+ SHA512:
6
+ metadata.gz: 6fc11a16896c4b3668f4bee5fd38235e239f3df3df40fba5becb2257cee2bd90d55877926e54f3dbf9499737f891b8dcb0ed7421605c3cfb3896ec5cc46ffcc4
7
+ data.tar.gz: e8c0d0fc28443c1eeba9d075931f674d2a90d11b4806c686af1787d3ec1d98a4c23930ef931136a53afc4b92c13278507cbfc84e6f5ee40fabee1d8be2f0cbd2
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.swp
2
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,336 @@
1
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., [http://fsf.org/]
2
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3
+ Everyone is permitted to copy and distribute verbatim copies
4
+ of this license document, but changing it is not allowed.
5
+
6
+ Preamble
7
+
8
+ The licenses for most software are designed to take away your
9
+ freedom to share and change it. By contrast, the GNU General Public
10
+ License is intended to guarantee your freedom to share and change free
11
+ software--to make sure the software is free for all its users. This
12
+ General Public License applies to most of the Free Software
13
+ Foundation's software and to any other program whose authors commit to
14
+ using it. (Some other Free Software Foundation software is covered by
15
+ the GNU Lesser General Public License instead.) You can apply it to
16
+ your programs, too.
17
+
18
+ When we speak of free software, we are referring to freedom, not
19
+ price. Our General Public Licenses are designed to make sure that you
20
+ have the freedom to distribute copies of free software (and charge for
21
+ this service if you wish), that you receive source code or can get it
22
+ if you want it, that you can change the software or use pieces of it
23
+ in new free programs; and that you know you can do these things.
24
+
25
+ To protect your rights, we need to make restrictions that forbid
26
+ anyone to deny you these rights or to ask you to surrender the rights.
27
+ These restrictions translate to certain responsibilities for you if you
28
+ distribute copies of the software, or if you modify it.
29
+
30
+ For example, if you distribute copies of such a program, whether
31
+ gratis or for a fee, you must give the recipients all the rights that
32
+ you have. You must make sure that they, too, receive or can get the
33
+ source code. And you must show them these terms so they know their
34
+ rights.
35
+
36
+ We protect your rights with two steps: (1) copyright the software, and
37
+ (2) offer you this license which gives you legal permission to copy,
38
+ distribute and/or modify the software.
39
+
40
+ Also, for each author's protection and ours, we want to make certain
41
+ that everyone understands that there is no warranty for this free
42
+ software. If the software is modified by someone else and passed on, we
43
+ want its recipients to know that what they have is not the original, so
44
+ that any problems introduced by others will not reflect on the original
45
+ authors' reputations.
46
+
47
+ Finally, any free program is threatened constantly by software
48
+ patents. We wish to avoid the danger that redistributors of a free
49
+ program will individually obtain patent licenses, in effect making the
50
+ program proprietary. To prevent this, we have made it clear that any
51
+ patent must be licensed for everyone's free use or not licensed at all.
52
+
53
+ The precise terms and conditions for copying, distribution and
54
+ modification follow.
55
+
56
+ GNU GENERAL PUBLIC LICENSE
57
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
58
+
59
+ 0. This License applies to any program or other work which contains
60
+ a notice placed by the copyright holder saying it may be distributed
61
+ under the terms of this General Public License. The "Program", below,
62
+ refers to any such program or work, and a "work based on the Program"
63
+ means either the Program or any derivative work under copyright law:
64
+ that is to say, a work containing the Program or a portion of it,
65
+ either verbatim or with modifications and/or translated into another
66
+ language. (Hereinafter, translation is included without limitation in
67
+ the term "modification".) Each licensee is addressed as "you".
68
+
69
+ Activities other than copying, distribution and modification are not
70
+ covered by this License; they are outside its scope. The act of
71
+ running the Program is not restricted, and the output from the Program
72
+ is covered only if its contents constitute a work based on the
73
+ Program (independent of having been made by running the Program).
74
+ Whether that is true depends on what the Program does.
75
+
76
+ 1. You may copy and distribute verbatim copies of the Program's
77
+ source code as you receive it, in any medium, provided that you
78
+ conspicuously and appropriately publish on each copy an appropriate
79
+ copyright notice and disclaimer of warranty; keep intact all the
80
+ notices that refer to this License and to the absence of any warranty;
81
+ and give any other recipients of the Program a copy of this License
82
+ along with the Program.
83
+
84
+ You may charge a fee for the physical act of transferring a copy, and
85
+ you may at your option offer warranty protection in exchange for a fee.
86
+
87
+ 2. You may modify your copy or copies of the Program or any portion
88
+ of it, thus forming a work based on the Program, and copy and
89
+ distribute such modifications or work under the terms of Section 1
90
+ above, provided that you also meet all of these conditions:
91
+
92
+ a) You must cause the modified files to carry prominent notices
93
+ stating that you changed the files and the date of any change.
94
+
95
+ b) You must cause any work that you distribute or publish, that in
96
+ whole or in part contains or is derived from the Program or any
97
+ part thereof, to be licensed as a whole at no charge to all third
98
+ parties under the terms of this License.
99
+
100
+ c) If the modified program normally reads commands interactively
101
+ when run, you must cause it, when started running for such
102
+ interactive use in the most ordinary way, to print or display an
103
+ announcement including an appropriate copyright notice and a
104
+ notice that there is no warranty (or else, saying that you provide
105
+ a warranty) and that users may redistribute the program under
106
+ these conditions, and telling the user how to view a copy of this
107
+ License. (Exception: if the Program itself is interactive but
108
+ does not normally print such an announcement, your work based on
109
+ the Program is not required to print an announcement.)
110
+
111
+ These requirements apply to the modified work as a whole. If
112
+ identifiable sections of that work are not derived from the Program,
113
+ and can be reasonably considered independent and separate works in
114
+ themselves, then this License, and its terms, do not apply to those
115
+ sections when you distribute them as separate works. But when you
116
+ distribute the same sections as part of a whole which is a work based
117
+ on the Program, the distribution of the whole must be on the terms of
118
+ this License, whose permissions for other licensees extend to the
119
+ entire whole, and thus to each and every part regardless of who wrote it.
120
+
121
+ Thus, it is not the intent of this section to claim rights or contest
122
+ your rights to work written entirely by you; rather, the intent is to
123
+ exercise the right to control the distribution of derivative or
124
+ collective works based on the Program.
125
+
126
+ In addition, mere aggregation of another work not based on the Program
127
+ with the Program (or with a work based on the Program) on a volume of
128
+ a storage or distribution medium does not bring the other work under
129
+ the scope of this License.
130
+
131
+ 3. You may copy and distribute the Program (or a work based on it,
132
+ under Section 2) in object code or executable form under the terms of
133
+ Sections 1 and 2 above provided that you also do one of the following:
134
+
135
+ a) Accompany it with the complete corresponding machine-readable
136
+ source code, which must be distributed under the terms of Sections
137
+ 1 and 2 above on a medium customarily used for software interchange; or,
138
+
139
+ b) Accompany it with a written offer, valid for at least three
140
+ years, to give any third party, for a charge no more than your
141
+ cost of physically performing source distribution, a complete
142
+ machine-readable copy of the corresponding source code, to be
143
+ distributed under the terms of Sections 1 and 2 above on a medium
144
+ customarily used for software interchange; or,
145
+
146
+ c) Accompany it with the information you received as to the offer
147
+ to distribute corresponding source code. (This alternative is
148
+ allowed only for noncommercial distribution and only if you
149
+ received the program in object code or executable form with such
150
+ an offer, in accord with Subsection b above.)
151
+
152
+ The source code for a work means the preferred form of the work for
153
+ making modifications to it. For an executable work, complete source
154
+ code means all the source code for all modules it contains, plus any
155
+ associated interface definition files, plus the scripts used to
156
+ control compilation and installation of the executable. However, as a
157
+ special exception, the source code distributed need not include
158
+ anything that is normally distributed (in either source or binary
159
+ form) with the major components (compiler, kernel, and so on) of the
160
+ operating system on which the executable runs, unless that component
161
+ itself accompanies the executable.
162
+
163
+ If distribution of executable or object code is made by offering
164
+ access to copy from a designated place, then offering equivalent
165
+ access to copy the source code from the same place counts as
166
+ distribution of the source code, even though third parties are not
167
+ compelled to copy the source along with the object code.
168
+
169
+ 4. You may not copy, modify, sublicense, or distribute the Program
170
+ except as expressly provided under this License. Any attempt
171
+ otherwise to copy, modify, sublicense or distribute the Program is
172
+ void, and will automatically terminate your rights under this License.
173
+ However, parties who have received copies, or rights, from you under
174
+ this License will not have their licenses terminated so long as such
175
+ parties remain in full compliance.
176
+
177
+ 5. You are not required to accept this License, since you have not
178
+ signed it. However, nothing else grants you permission to modify or
179
+ distribute the Program or its derivative works. These actions are
180
+ prohibited by law if you do not accept this License. Therefore, by
181
+ modifying or distributing the Program (or any work based on the
182
+ Program), you indicate your acceptance of this License to do so, and
183
+ all its terms and conditions for copying, distributing or modifying
184
+ the Program or works based on it.
185
+
186
+ 6. Each time you redistribute the Program (or any work based on the
187
+ Program), the recipient automatically receives a license from the
188
+ original licensor to copy, distribute or modify the Program subject to
189
+ these terms and conditions. You may not impose any further
190
+ restrictions on the recipients' exercise of the rights granted herein.
191
+ You are not responsible for enforcing compliance by third parties to
192
+ this License.
193
+
194
+ 7. If, as a consequence of a court judgment or allegation of patent
195
+ infringement or for any other reason (not limited to patent issues),
196
+ conditions are imposed on you (whether by court order, agreement or
197
+ otherwise) that contradict the conditions of this License, they do not
198
+ excuse you from the conditions of this License. If you cannot
199
+ distribute so as to satisfy simultaneously your obligations under this
200
+ License and any other pertinent obligations, then as a consequence you
201
+ may not distribute the Program at all. For example, if a patent
202
+ license would not permit royalty-free redistribution of the Program by
203
+ all those who receive copies directly or indirectly through you, then
204
+ the only way you could satisfy both it and this License would be to
205
+ refrain entirely from distribution of the Program.
206
+
207
+ If any portion of this section is held invalid or unenforceable under
208
+ any particular circumstance, the balance of the section is intended to
209
+ apply and the section as a whole is intended to apply in other
210
+ circumstances.
211
+
212
+ It is not the purpose of this section to induce you to infringe any
213
+ patents or other property right claims or to contest validity of any
214
+ such claims; this section has the sole purpose of protecting the
215
+ integrity of the free software distribution system, which is
216
+ implemented by public license practices. Many people have made
217
+ generous contributions to the wide range of software distributed
218
+ through that system in reliance on consistent application of that
219
+ system; it is up to the author/donor to decide if he or she is willing
220
+ to distribute software through any other system and a licensee cannot
221
+ impose that choice.
222
+
223
+ This section is intended to make thoroughly clear what is believed to
224
+ be a consequence of the rest of this License.
225
+
226
+ 8. If the distribution and/or use of the Program is restricted in
227
+ certain countries either by patents or by copyrighted interfaces, the
228
+ original copyright holder who places the Program under this License
229
+ may add an explicit geographical distribution limitation excluding
230
+ those countries, so that distribution is permitted only in or among
231
+ countries not thus excluded. In such case, this License incorporates
232
+ the limitation as if written in the body of this License.
233
+
234
+ 9. The Free Software Foundation may publish revised and/or new versions
235
+ of the General Public License from time to time. Such new versions will
236
+ be similar in spirit to the present version, but may differ in detail to
237
+ address new problems or concerns.
238
+
239
+ Each version is given a distinguishing version number. If the Program
240
+ specifies a version number of this License which applies to it and "any
241
+ later version", you have the option of following the terms and conditions
242
+ either of that version or of any later version published by the Free
243
+ Software Foundation. If the Program does not specify a version number of
244
+ this License, you may choose any version ever published by the Free Software
245
+ Foundation.
246
+
247
+ 10. If you wish to incorporate parts of the Program into other free
248
+ programs whose distribution conditions are different, write to the author
249
+ to ask for permission. For software which is copyrighted by the Free
250
+ Software Foundation, write to the Free Software Foundation; we sometimes
251
+ make exceptions for this. Our decision will be guided by the two goals
252
+ of preserving the free status of all derivatives of our free software and
253
+ of promoting the sharing and reuse of software generally.
254
+
255
+ NO WARRANTY
256
+
257
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
258
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
259
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
260
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
261
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
262
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
263
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
264
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
265
+ REPAIR OR CORRECTION.
266
+
267
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
268
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
269
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
270
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
271
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
272
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
273
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
274
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
275
+ POSSIBILITY OF SUCH DAMAGES.
276
+
277
+ END OF TERMS AND CONDITIONS
278
+
279
+ How to Apply These Terms to Your New Programs
280
+
281
+ If you develop a new program, and you want it to be of the greatest
282
+ possible use to the public, the best way to achieve this is to make it
283
+ free software which everyone can redistribute and change under these terms.
284
+
285
+ To do so, attach the following notices to the program. It is safest
286
+ to attach them to the start of each source file to most effectively
287
+ convey the exclusion of warranty; and each file should have at least
288
+ the "copyright" line and a pointer to where the full notice is found.
289
+
290
+ {description}
291
+ Copyright (C) {year} {fullname}
292
+
293
+ This program is free software; you can redistribute it and/or modify
294
+ it under the terms of the GNU General Public License as published by
295
+ the Free Software Foundation; either version 2 of the License, or
296
+ (at your option) any later version.
297
+
298
+ This program is distributed in the hope that it will be useful,
299
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
300
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
301
+ GNU General Public License for more details.
302
+
303
+ You should have received a copy of the GNU General Public License along
304
+ with this program; if not, write to the Free Software Foundation, Inc.,
305
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
306
+
307
+ Also add information on how to contact you by electronic and paper mail.
308
+
309
+ If the program is interactive, make it output a short notice like this
310
+ when it starts in an interactive mode:
311
+
312
+ Gnomovision version 69, Copyright (C) year name of author
313
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
314
+ This is free software, and you are welcome to redistribute it
315
+ under certain conditions; type `show c' for details.
316
+
317
+ The hypothetical commands `show w' and `show c' should show the appropriate
318
+ parts of the General Public License. Of course, the commands you use may
319
+ be called something other than `show w' and `show c'; they could even be
320
+ mouse-clicks or menu items--whatever suits your program.
321
+
322
+ You should also get your employer (if you work as a programmer) or your
323
+ school, if any, to sign a "copyright disclaimer" for the program, if
324
+ necessary. Here is a sample; alter the names:
325
+
326
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
327
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
328
+
329
+ {signature of Ty Coon}, 1 April 1989
330
+ Ty Coon, President of Vice
331
+
332
+ This General Public License does not permit incorporating your program into
333
+ proprietary programs. If your program is a subroutine library, you may
334
+ consider it more useful to permit linking proprietary applications with the
335
+ library. If this is what you want to do, use the GNU Lesser General
336
+ Public License instead of this License.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # GNOME Campfire Notifications
2
+ A Ruby script that issues GNOME desktop notifications when something is said in your Campfire chatroom.
3
+
4
+ # Installation
5
+ `gem install gnome-campfire-notifications`
6
+
7
+ In your `~/.campfire.yml`:
8
+
9
+ token: (API access token under My info in the chatroom web app)
10
+ subdomain: ("sample" as in "sample.campfirenow.com/room/123456")
11
+ roomid: ("123456" as in "sample.campfirenow.com/room/123456")
12
+ self_user: 'Michael Nelson' (So that you don't get notified of your own messages)
13
+ filter: !ruby/regexp '/Michael Nelson/' (Send only messages matching this regex. Omit to disable.)
14
+
15
+ # Usage
16
+ Execute `gnome-campfire-notifications`.
17
+
18
+ # Help
19
+ Please feel free to open an issue or PR. Or feel free to contact me via my info on my Github user page.
20
+
21
+ # License
22
+ [LICENSE](https://github.com/mcnelson/gnome-campfire-notifications/blob/master/LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'rake/testtask'
2
+ Rake::TestTask.new(:test)
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "gnome-campfire-notifications"
3
+ s.version = "0.2.0"
4
+ s.summary = "GNOME desktop notifications for Campfire."
5
+ s.description = "A Ruby script that issues GNOME desktop notifications when something is said in your Campfire chatroom."
6
+ s.authors = ["Michael Nelson"]
7
+ s.email = "michael@nelsonware.com"
8
+ s.executables << "gnome-campfire-notifications"
9
+ s.files = `git ls-files`.split
10
+ s.test_files = `git ls-files -- test/*`.split
11
+ s.homepage = "https://github.com/mcnelson/gnome-campfire-notifications"
12
+ s.license = "LGPL-2.0"
13
+
14
+ s.add_dependency 'twitter-stream'
15
+ s.add_development_dependency 'minitest'
16
+ s.add_development_dependency 'vcr'
17
+ s.add_development_dependency 'webmock'
18
+ end
@@ -1,34 +1,65 @@
1
1
  require "twitter/json_stream"
2
2
  require "net/http"
3
3
  require "json"
4
+ require "yaml"
4
5
 
5
6
  class GnomeCampfireNotifications
6
7
  HOST = 'streaming.campfirenow.com'
7
8
  NOTIFICATION_GFX_FILENAME = 'campfire.png'
8
9
  NOTIFICATION_GFX_SYSPATH = "/usr/share/icons/gnome/32x32/apps"
9
10
 
10
- ATTR_MAP = {
11
- room_name: 'GNOME_CAMPFIRE_NOTIFICATIONS_ROOM_NAME',
12
- room_id: 'GNOME_CAMPFIRE_NOTIFICATIONS_ROOM_ID',
13
- token: 'GNOME_CAMPFIRE_NOTIFICATIONS_TOKEN'
14
- }
11
+ CONFIG_SYSPATH = "#{ENV['HOME']}/.campfire.yml"
12
+ CONFIG_ATTRS = %i(token subdomain roomid self_user filter icon_path)
13
+ REQD_CONFIG_ATTRS = %i(token subdomain roomid)
15
14
 
16
- def initialize
17
- if (missing = ATTR_MAP.values.select { |env| ENV[env].empty? }).any?
18
- raise "please set environment variable(s) #{missing.join(', ')}"
19
- end
15
+ attr_reader :config
20
16
 
21
- @options = ATTR_MAP.map.with_object({}) { |(key, env), opts| opts[key] = ENV[env] }
17
+ def initialize
22
18
  @username_cache = []
23
- try_icon
19
+
20
+ load_config
21
+ try_icon unless @config[:icon_path]
24
22
  end
25
23
 
26
24
  def start
27
25
  on_message do |item|
28
- username = get_username(item["user_id"].to_i)
29
- message = "#{item["body"].to_s.gsub(/'/, "\'")}"
26
+ send_notification(item)
27
+ end
28
+ end
29
+
30
+ def send_notification(item)
31
+ username = get_username(item["user_id"].to_i)
32
+
33
+ if should_send?(username, item["body"])
34
+ system("notify-send --hint=int:transient:1 -u low#{icon} \"#{username}\" \"#{escape_double_quotes(item["body"])}\"")
35
+ end
36
+ end
37
+
38
+ def get_username(id)
39
+ return "Unknown" if id.nil?
40
+
41
+ unless @username_cache[id]
42
+ req = Net::HTTP::Get.new("https://#{room_url}/users/#{id}.json")
43
+ req.basic_auth(@config[:token], "x")
44
+ http = Net::HTTP.new(room_url, 443)
45
+ http.use_ssl = true
46
+ resp = http.start { |h| h.request(req) }
47
+
48
+ json = JSON.parse(resp.body)
49
+
50
+ # Get username
51
+ @username_cache[id] = json["user"]["name"]
52
+ end
53
+
54
+ @username_cache[id]
55
+ end
56
+
57
+ def load_config
58
+ raise "please create #{CONFIG_SYSPATH}, see Github page for details" unless File.exists?(CONFIG_SYSPATH)
59
+ @config = YAML.load_file(CONFIG_SYSPATH).each.with_object({}) { |(k, v), h| h[k.to_sym] = v }
30
60
 
31
- system("notify-send --hint=int:transient:1 -u low#{icon} '#{username}' '#{message}'")
61
+ if !(missing = REQD_CONFIG_ATTRS.delete_if { |k| @config[k] }).empty?
62
+ raise "please set config option(s) in #{CONFIG_SYSPATH}: #{missing.join(', ')}"
32
63
  end
33
64
  end
34
65
 
@@ -38,8 +69,8 @@ class GnomeCampfireNotifications
38
69
  EventMachine::run do
39
70
  stream = Twitter::JSONStream.connect(
40
71
  host: HOST,
41
- path: "/room/#{room_id}/live.json",
42
- auth: "#{token}:x",
72
+ path: "/room/#{@config[:roomid]}/live.json",
73
+ auth: "#{@config[:token]}:x",
43
74
  )
44
75
 
45
76
  stream.each_item do |item|
@@ -54,38 +85,26 @@ class GnomeCampfireNotifications
54
85
  end
55
86
  end
56
87
 
57
- def get_username(id)
58
- return "Unknown" if id.nil?
88
+ def should_send?(username, body)
89
+ return false if @config[:self_user] && username == @config[:self_user]
90
+ return !body.match(@config[:filter]).nil? if @config[:filter]
59
91
 
60
- unless @username_cache[id]
61
- req = Net::HTTP::Get.new("https://#{room_url}/users/#{id}.json")
62
- req.basic_auth(@options[:token], "x")
63
- http = Net::HTTP.new(room_url, 443)
64
- http.use_ssl = true
65
- resp = http.start { |h| h.request(req) }
66
-
67
- json = JSON.parse(resp.body)
68
-
69
- # Get username
70
- @username_cache[id] = json["user"]["name"]
71
- end
72
-
73
- @username_cache[id]
92
+ true
74
93
  end
75
94
 
76
95
  def icon
77
- " -i #{@options[:icon_path]}"
96
+ " -i #{@config[:icon_path]}"
78
97
  end
79
98
 
80
99
  def try_icon
81
100
  if path = notification_gfx_paths.detect { |p| File.exists?(p) }
82
- @options[:icon_path] = path
101
+ @config[:icon_path] = path
83
102
  end
84
103
  end
85
104
 
86
105
  def notification_gfx_paths
87
106
  [[NOTIFICATION_GFX_SYSPATH, NOTIFICATION_GFX_FILENAME],
88
- [gem_dir, "assets", NOTIFICATION_GFX_FILENAME]].join('/')
107
+ [gem_dir, "assets", NOTIFICATION_GFX_FILENAME]].map { |p| p.join('/') }
89
108
  end
90
109
 
91
110
  def gem_dir
@@ -93,14 +112,10 @@ class GnomeCampfireNotifications
93
112
  end
94
113
 
95
114
  def room_url
96
- "#{@options[:room_name]}.campfirenow.com"
97
- end
98
-
99
- def room_id
100
- @options[:room_id]
115
+ "#{@config[:subdomain]}.campfirenow.com"
101
116
  end
102
117
 
103
- def token
104
- @options[:token]
118
+ def escape_double_quotes(string)
119
+ string.gsub(/"/, '\"')
105
120
  end
106
121
  end
@@ -0,0 +1,27 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://abcdefg12345678:x@example.campfirenow.com/users/1.json
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Server:
20
+ - nginx
21
+ body:
22
+ encoding: US-ASCII
23
+ string: ! '{"user":{"type":"Member","avatar_url":"http://dge9rmgqjs8m1.cloudfront.net/global/64b5bd6e09f22260d1d2260466dfee98949813aa18d3513ba144086f4f1be319d90518e63e8f0f373ff065346132b66d158e40e4e19e8251f96d96f4326913992aaaaaf702b91432e5e1d250f4575725/avatar.gif?r=3","admin":false,"name":"Derp
24
+ Herpinson","created_at":"2012/06/14 20:04:54 +0000","id":1218200,"email_address":"derp@herpinson.com"}}'
25
+ http_version:
26
+ recorded_at: Mon, 11 Nov 2013 04:20:13 GMT
27
+ recorded_with: VCR 2.7.0
@@ -0,0 +1,27 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://abcdefg12345678:x@example.campfirenow.com/users/1.json
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept:
11
+ - ! '*/*'
12
+ User-Agent:
13
+ - Ruby
14
+ response:
15
+ status:
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ Server:
20
+ - nginx
21
+ body:
22
+ encoding: US-ASCII
23
+ string: ! '{"user":{"type":"Member","avatar_url":"http://dge9rmgqjs8m1.cloudfront.net/global/64b5bd6e09f22260d1d2260466dfee98949813aa18d3513ba144086f4f1be319d90518e63e8f0f373ff065346132b66d158e40e4e19e8251f96d96f4326913992aaaaaf702b91432e5e1d250f4575725/avatar.gif?r=3","admin":false,"name":"Jane
24
+ Doe","created_at":"2012/06/14 20:04:54 +0000","id":1218200,"email_address":"derp@herpinson.com"}}'
25
+ http_version:
26
+ recorded_at: Mon, 11 Nov 2013 04:20:13 GMT
27
+ recorded_with: VCR 2.7.0
data/test/helper.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "minitest/autorun"
2
+ require "gnome-campfire-notifications"
3
+ require "vcr"
4
+ require "webmock"
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'test/cassettes'
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ GnomeCampfireNotifications.class_eval do
12
+ def load_dummy_config(hash = {})
13
+ @config = {
14
+ token: 'abcdefg12345678',
15
+ roomid: 1,
16
+ subdomain: 'example'
17
+ }.merge(hash)
18
+ end
19
+ end
@@ -0,0 +1,101 @@
1
+ require_relative 'helper'
2
+
3
+ describe GnomeCampfireNotifications do
4
+ describe "#load_config" do
5
+ it "loads correct values" do
6
+ gcn = GnomeCampfireNotifications.new
7
+ gcn.load_dummy_config({
8
+ token: 'foo',
9
+ roomid: 'bar',
10
+ room_name: 'baz'
11
+ })
12
+
13
+ assert_equal 'foo', gcn.config[:token]
14
+ assert_equal 'bar', gcn.config[:roomid]
15
+ assert_equal 'baz', gcn.config[:room_name]
16
+ end
17
+ end
18
+
19
+ describe "#send_notification" do
20
+ it "works" do
21
+ gcn = GnomeCampfireNotifications.new
22
+ gcn.load_dummy_config
23
+ input = {"user_id" => '1', "body" => 'hi'}
24
+
25
+ gcn.stub(:get_username, 'Fred') do
26
+ assert_equal true, gcn.send_notification(input)
27
+ end
28
+ end
29
+
30
+ describe "quote escaping" do
31
+ it "returns true" do
32
+ gcn = GnomeCampfireNotifications.new
33
+ gcn.load_dummy_config
34
+ input = {"user_id" => '1', "body" => 'quote"s quote\'s'}
35
+
36
+ gcn.stub(:get_username, 'Fred') do
37
+ assert_equal true, gcn.send_notification(input)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "when self_user is set & message sender is self_user" do
43
+ it "doesn't notify and returns nil" do
44
+ gcn = GnomeCampfireNotifications.new
45
+ gcn.load_dummy_config(self_user: 'Derp Herpinson')
46
+ input = {"user_id" => '1', "body" => 'I should never be sent'}
47
+
48
+ VCR.use_cassette('get_username') do
49
+ assert_equal nil, gcn.send_notification(input)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "when self_user & filter vars are set" do
55
+ before do
56
+ @gcn = GnomeCampfireNotifications.new
57
+ @gcn.load_dummy_config(
58
+ self_user: 'Derp Herpinson',
59
+ filter: /Derp Herpinson/
60
+ )
61
+ end
62
+
63
+ describe "message matches filter" do
64
+ it "sends notification and returns true" do
65
+ input = {"user_id" => '1', "body" => 'Derp Herpinson: yo wassup'}
66
+
67
+ VCR.use_cassette('get_username_alt') do
68
+ assert_equal true, @gcn.send_notification(input)
69
+ end
70
+ end
71
+ end
72
+
73
+ describe "message doesn't match filter" do
74
+ it "doesn't notify and returns nil" do
75
+ input = {"user_id" => '1', "body" => 'I should never be sent'}
76
+
77
+ VCR.use_cassette('get_username_alt') do
78
+ assert_equal nil, @gcn.send_notification(input)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#get_username" do
86
+ it "returns username in json hierarchy user -> name" do
87
+ gcn = GnomeCampfireNotifications.new
88
+ gcn.load_dummy_config
89
+
90
+ VCR.use_cassette('get_username') do
91
+ assert_equal "Derp Herpinson", gcn.get_username(1)
92
+ end
93
+ end
94
+
95
+ it "returns unknown if ID is nil" do
96
+ gcn = GnomeCampfireNotifications.new
97
+ gcn.load_dummy_config
98
+ assert_equal "Unknown", gcn.get_username(nil)
99
+ end
100
+ end
101
+ end
metadata CHANGED
@@ -1,33 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gnome-campfire-notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Michael Nelson
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-10 00:00:00.000000000 Z
11
+ date: 2013-11-15 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: twitter-stream
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
- description: A Ruby script that spawns GNOME desktop notifications when something
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: vcr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A Ruby script that issues GNOME desktop notifications when something
31
70
  is said in your Campfire chatroom.
32
71
  email: michael@nelsonware.com
33
72
  executables:
@@ -35,31 +74,44 @@ executables:
35
74
  extensions: []
36
75
  extra_rdoc_files: []
37
76
  files:
38
- - lib/gnome-campfire-notifications.rb
77
+ - .gitignore
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
39
81
  - assets/campfire.png
40
82
  - bin/gnome-campfire-notifications
83
+ - gnome-campfire-notifications.gemspec
84
+ - lib/gnome-campfire-notifications.rb
85
+ - test/cassettes/get_username.yml
86
+ - test/cassettes/get_username_alt.yml
87
+ - test/helper.rb
88
+ - test/test_gnome_campfire_notifications.rb
41
89
  homepage: https://github.com/mcnelson/gnome-campfire-notifications
42
- licenses: []
90
+ licenses:
91
+ - LGPL-2.0
92
+ metadata: {}
43
93
  post_install_message:
44
94
  rdoc_options: []
45
95
  require_paths:
46
96
  - lib
47
97
  required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
98
  requirements:
50
- - - ! '>='
99
+ - - '>='
51
100
  - !ruby/object:Gem::Version
52
101
  version: '0'
53
102
  required_rubygems_version: !ruby/object:Gem::Requirement
54
- none: false
55
103
  requirements:
56
- - - ! '>='
104
+ - - '>='
57
105
  - !ruby/object:Gem::Version
58
106
  version: '0'
59
107
  requirements: []
60
108
  rubyforge_project:
61
- rubygems_version: 1.8.24
109
+ rubygems_version: 2.0.2
62
110
  signing_key:
63
- specification_version: 3
111
+ specification_version: 4
64
112
  summary: GNOME desktop notifications for Campfire.
65
- test_files: []
113
+ test_files:
114
+ - test/cassettes/get_username.yml
115
+ - test/cassettes/get_username_alt.yml
116
+ - test/helper.rb
117
+ - test/test_gnome_campfire_notifications.rb