ruby-notifications 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/notifications.rb +189 -0
- data/lib/ruby-notifications/version.rb +3 -0
- data/ruby-notifications.gemspec +25 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 368417a15f4d68bd03abc9733fc98d9bdf26a318
|
4
|
+
data.tar.gz: 9361fe2762b9412a514e0ada7a267376cef4b67d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08057f59d09db8d813859b0f90b13811301d8184b085c108fe749cecf7335012fcbc3fcaf1d8e8685117400d33155c208c0c41511fe85b748e8358d8dd1ccffb'
|
7
|
+
data.tar.gz: 541ea926b1a380c0c0dc8d93ba6249e6a2227dcda92682eb200b48b5e9e986ca3f352c47cfda915647e193e1b6a7a460a63f5f5590aafd34cd168ee769b853ee
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-notifications (0.1.0)
|
5
|
+
ruby-dbus (~> 0.14.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
rake (10.5.0)
|
12
|
+
rspec (3.7.0)
|
13
|
+
rspec-core (~> 3.7.0)
|
14
|
+
rspec-expectations (~> 3.7.0)
|
15
|
+
rspec-mocks (~> 3.7.0)
|
16
|
+
rspec-core (3.7.1)
|
17
|
+
rspec-support (~> 3.7.0)
|
18
|
+
rspec-expectations (3.7.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-mocks (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-support (3.7.1)
|
25
|
+
ruby-dbus (0.14.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.16.a)
|
32
|
+
rake (~> 10.0)
|
33
|
+
rspec (~> 3.0)
|
34
|
+
ruby-notifications!
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.16.0.pre.1
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Notifications
|
2
|
+
|
3
|
+
A simple interface for the org.freedesktop.Notifications DBus service.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'notifications'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install notifications
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'notifications'
|
25
|
+
# Create the service interface
|
26
|
+
service = Notifications::NotificationService.new
|
27
|
+
|
28
|
+
# Create a notification object
|
29
|
+
# 'appname' is only required named parameter but isn't very
|
30
|
+
# useful on its own.
|
31
|
+
notification = Notifications::Notification.new(appname: "test",
|
32
|
+
summary: "test",
|
33
|
+
body: "A test notification")
|
34
|
+
|
35
|
+
# Retrieve the service object's methods internally.
|
36
|
+
# May refactor as raised initializer exception in the
|
37
|
+
# future.
|
38
|
+
service.try_introspect
|
39
|
+
|
40
|
+
# Send the notification
|
41
|
+
service.send_notification notification
|
42
|
+
|
43
|
+
# Can also short-hand service.close_notification in this instance
|
44
|
+
# as the service interface tracks the last sent notification for
|
45
|
+
# improved usability.
|
46
|
+
service.close_notification notification
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
53
|
+
|
54
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/CrunchwrapSupreme/ruby-notifications.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "notifications"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
#require "notifications/version"
|
2
|
+
require "dbus"
|
3
|
+
require "timeout"
|
4
|
+
|
5
|
+
module Notifications
|
6
|
+
NOTIF_SERVICE = 'org.freedesktop.Notifications'
|
7
|
+
NOTIF_OBJECT = '/org/freedesktop/Notifications'
|
8
|
+
VALID_CAPABILITIES = [ 'action-icons', 'actions', 'body', 'body-hyperlinks', 'body-images',
|
9
|
+
'body-markup', 'icon-multi', 'icon-static', 'persistence', 'sound' ]
|
10
|
+
VALID_CAPABILITIES.sort!
|
11
|
+
NOTIF_SERVICE.freeze
|
12
|
+
NOTIF_OBJECT.freeze
|
13
|
+
VALID_CAPABILITIES.freeze
|
14
|
+
TIMEOUT = 5
|
15
|
+
|
16
|
+
class NotificationDaemonError < RuntimeError
|
17
|
+
def initialize
|
18
|
+
super "Notification daemon not implemented or unavailable."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class CommandNotImplementedError < NoMethodError
|
23
|
+
def initialize(command="Command")
|
24
|
+
super "#{msg} not implemented by the #{Notifications.NOTIF_SERVICE} service."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ObjectIntrospectionError < NoMethodError
|
29
|
+
def initialize
|
30
|
+
super "Object not introspected. Introspect before calling proxy methods."
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class NotificationService
|
35
|
+
attr_reader :bus, :service
|
36
|
+
|
37
|
+
def service_object
|
38
|
+
@service_obj
|
39
|
+
end
|
40
|
+
|
41
|
+
def introspected?
|
42
|
+
@service_obj.introspected
|
43
|
+
end
|
44
|
+
|
45
|
+
def capabilities
|
46
|
+
@capabilities ||= try_to { @service_obj.GetCapabilities }
|
47
|
+
@capabilities
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize
|
51
|
+
@bus = DBus::SessionBus.instance
|
52
|
+
@service = @bus.service Notifications::NOTIF_SERVICE
|
53
|
+
@service_obj = @service.object Notifications::NOTIF_OBJECT
|
54
|
+
|
55
|
+
|
56
|
+
if not @service.exists? then
|
57
|
+
raise NotificationDaemonError.new
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
@service_obj.define_singleton_method(:method_missing) do |name, *args, &block|
|
62
|
+
if not self.introspected then
|
63
|
+
raise ObjectIntrospectionError
|
64
|
+
else
|
65
|
+
raise CommandNotImplementedError name.to_s
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
@supports = {}
|
70
|
+
end
|
71
|
+
|
72
|
+
def try_introspect
|
73
|
+
begin
|
74
|
+
try_to { @service_obj.introspect }
|
75
|
+
true
|
76
|
+
rescue
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#returns values of attributes mapped to @supports
|
82
|
+
def method_missing(name, *args)
|
83
|
+
raise NoMethodError unless args.length == 0 and not block
|
84
|
+
m_data = name.match /^supports_([a-zA-Z]+)\?$/
|
85
|
+
raise NoMethodError unless m_data and m_data[1]
|
86
|
+
( val = @supports[m_data[1]] ) and Notifications::VALID_CAPABILITIES.include?(m_data[1]) ? val : false
|
87
|
+
end
|
88
|
+
|
89
|
+
#returns notification after run
|
90
|
+
def send_notification(notify_temp, replace_previous=false)
|
91
|
+
( replace_previous and notify_temp.id > 0 ) ? r_id = notify_temp.id : r_id = 0
|
92
|
+
|
93
|
+
id = try_to {
|
94
|
+
@service_obj.Notify( notify_temp.appname, r_id, notify_temp.icon, notify_temp.summary,
|
95
|
+
notify_temp.body, notify_temp.actions.pack, notify_temp.hints,
|
96
|
+
notify_temp.timeout )[0]
|
97
|
+
}
|
98
|
+
|
99
|
+
if not replace_previous then
|
100
|
+
notify_temp = notify_temp.clone
|
101
|
+
end
|
102
|
+
|
103
|
+
notify_temp.id = id
|
104
|
+
@last_notify = notify_temp
|
105
|
+
notify_temp
|
106
|
+
end
|
107
|
+
|
108
|
+
def close_notification(notify_temp=nil)
|
109
|
+
if notify_temp then
|
110
|
+
close = notify_temp
|
111
|
+
elsif @last_notify
|
112
|
+
close = @last_notify
|
113
|
+
else
|
114
|
+
return -1
|
115
|
+
end
|
116
|
+
|
117
|
+
try_to { @service_obj.CloseNotification(close.id)[0] }
|
118
|
+
end
|
119
|
+
|
120
|
+
def get_server_info
|
121
|
+
try_to { @service_obj.GetServerInformation }
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
def try_to(&block)
|
126
|
+
Timeout::timeout(TIMEOUT) { block.call }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
class Action
|
131
|
+
attr_accessor :action, :action_string, :callback
|
132
|
+
|
133
|
+
def initialize(action, action_string, callback = nil)
|
134
|
+
@action = action
|
135
|
+
@action_string = action_string
|
136
|
+
@callback = callback
|
137
|
+
end
|
138
|
+
|
139
|
+
def normalize
|
140
|
+
[@action, @action_string]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class ActionList
|
145
|
+
def initialize(action_list = [])
|
146
|
+
@action_list = action_list
|
147
|
+
end
|
148
|
+
|
149
|
+
def <<(action)
|
150
|
+
@action_list << action
|
151
|
+
end
|
152
|
+
|
153
|
+
def [](id)
|
154
|
+
@action_list[id]
|
155
|
+
end
|
156
|
+
|
157
|
+
def length
|
158
|
+
@action_list.length
|
159
|
+
end
|
160
|
+
|
161
|
+
def pack
|
162
|
+
al = @action_list.map do |action|
|
163
|
+
action.normalize
|
164
|
+
end
|
165
|
+
|
166
|
+
al.flatten!
|
167
|
+
al
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
class Notification
|
172
|
+
attr_accessor :actions, :icon, :body, :summary, :hints, :timeout, :id, :appname
|
173
|
+
|
174
|
+
def initialize(icon: '', body: '', summary: '', hints: {}, timeout: -1, appname:)
|
175
|
+
@actions = ActionList.new
|
176
|
+
@icon = icon
|
177
|
+
@body = body
|
178
|
+
@summary = summary
|
179
|
+
@hints = hints
|
180
|
+
@timeout = timeout
|
181
|
+
@id = nil
|
182
|
+
@appname = appname
|
183
|
+
end
|
184
|
+
|
185
|
+
def register_action(action:)
|
186
|
+
@actions << action
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ruby-notifications/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ruby-notifications"
|
8
|
+
spec.version = Notifications::VERSION
|
9
|
+
spec.authors = ["David Young"]
|
10
|
+
spec.email = ["david@davidyoung.space"]
|
11
|
+
|
12
|
+
spec.summary = "Simple interface for the org.freedesktop.Notifications service."
|
13
|
+
spec.homepage = "https://github.com/CrunchwrapSupreme/ruby-notifications.git"
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
15
|
+
f.match(%r{^(test|spec|features)/})
|
16
|
+
end
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_runtime_dependency "ruby-dbus", "~> 0.14.1"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-notifications
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Young
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.16.a
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.16.a
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ruby-dbus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.14.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.14.1
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- david@davidyoung.space
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/notifications.rb
|
86
|
+
- lib/ruby-notifications/version.rb
|
87
|
+
- ruby-notifications.gemspec
|
88
|
+
homepage: https://github.com/CrunchwrapSupreme/ruby-notifications.git
|
89
|
+
licenses: []
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.6.13
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Simple interface for the org.freedesktop.Notifications service.
|
111
|
+
test_files: []
|