Class: Groem::Notification
- Inherits:
-
Struct
- Object
- Struct
- Groem::Notification
- Includes:
- Marshal::Request
- Defined in:
- lib/groem/notification.rb
Constant Summary collapse
- DEFAULT_ENV =
{'protocol' => 'GNTP', 'version' => '1.0', 'request_method' => 'NOTIFY', 'encryption_id' => 'NONE' }
Constants included from Constants
Constants::ENVIRONMENT_KEY, Constants::GNTP_APPLICATION_ICON_KEY, Constants::GNTP_APPLICATION_NAME_KEY, Constants::GNTP_CALLBACK_RESPONSE, Constants::GNTP_CLICK_CALLBACK_RESULT, Constants::GNTP_CLOSE_CALLBACK_RESULT, Constants::GNTP_DEFAULT_ENVIRONMENT, Constants::GNTP_ENCRYPTION_ID_KEY, Constants::GNTP_ERROR_CODE_KEY, Constants::GNTP_ERROR_CODE_OK, Constants::GNTP_ERROR_RESPONSE, Constants::GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY, Constants::GNTP_NOTIFICATION_CALLBACK_CONTEXT_TYPE_KEY, Constants::GNTP_NOTIFICATION_CALLBACK_RESULT_KEY, Constants::GNTP_NOTIFICATION_CALLBACK_TARGET_KEY, Constants::GNTP_NOTIFICATION_CALLBACK_TIMESTAMP_KEY, Constants::GNTP_NOTIFICATION_COUNT_KEY, Constants::GNTP_NOTIFICATION_ICON_KEY, Constants::GNTP_NOTIFICATION_ID_KEY, Constants::GNTP_NOTIFICATION_NAME_KEY, Constants::GNTP_NOTIFY_METHOD, Constants::GNTP_OK_RESPONSE, Constants::GNTP_PROTOCOL_KEY, Constants::GNTP_REGISTER_METHOD, Constants::GNTP_REQUEST_METHOD_KEY, Constants::GNTP_RESPONSE_ACTION_KEY, Constants::GNTP_RESPONSE_METHOD_KEY, Constants::GNTP_SUBSCRIBE_METHOD, Constants::GNTP_TIMEDOUT_CALLBACK_RESULT, Constants::GNTP_VERSION_KEY, Constants::HEADERS_KEY, Constants::NOTIFICATIONS_KEY
Instance Attribute Summary collapse
-
#application_name ⇒ Object
Returns the value of attribute application_name.
-
#coalescing_id ⇒ Object
Returns the value of attribute coalescing_id.
-
#display_name ⇒ Object
Returns the value of attribute display_name.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#name ⇒ Object
Returns the value of attribute name.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sticky ⇒ Object
Returns the value of attribute sticky.
-
#text ⇒ Object
Returns the value of attribute text.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#callback(*args) ⇒ Object
Note defaults name and type to notification name.
- #callback_context ⇒ Object
- #callback_target ⇒ Object
- #callback_type ⇒ Object (also: #callback_context_type)
- #dup ⇒ Object
- #header(key, value) ⇒ Object
- #icon(uri_or_file) ⇒ Object
-
#initialize(name, *args) ⇒ Notification
constructor
A new instance of Notification.
- #reset! ⇒ Object
- #reset_callback! ⇒ Object
- #to_notify ⇒ Object
- #to_register ⇒ Object
- #to_request ⇒ Object
Methods included from Marshal::Request
Methods included from Constants
#growlify_action, #growlify_key, included
Constructor Details
#initialize(name, *args) ⇒ Notification
Returns a new instance of Notification.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/groem/notification.rb', line 24 def initialize(name, *args) opts = args.last.is_a?(Hash) ? args.pop : {} title = args.shift self.environment, self.headers, @callback = {}, {}, {} self.environment = DEFAULT_ENV.merge(opts.delete(:environment) || {}) self.name = name.to_s if name self.title = title.to_s if title self.enabled = 'True' self.headers = opts.delete(:headers) || {} opts.each_pair do |opt, val| if self.respond_to?(:"#{opt}=") self.__send__ :"#{opt}=", val.to_s else header(opt, val.to_s) end end reset! end |
Instance Attribute Details
#application_name ⇒ Object
Returns the value of attribute application_name
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def application_name @application_name end |
#coalescing_id ⇒ Object
Returns the value of attribute coalescing_id
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def coalescing_id @coalescing_id end |
#display_name ⇒ Object
Returns the value of attribute display_name
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def display_name @display_name end |
#enabled ⇒ Object
Returns the value of attribute enabled
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def enabled @enabled end |
#environment ⇒ Object
Returns the value of attribute environment
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def environment @environment end |
#headers ⇒ Object
Returns the value of attribute headers
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def headers @headers end |
#name ⇒ Object
Returns the value of attribute name
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def name @name end |
#priority ⇒ Object
Returns the value of attribute priority
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def priority @priority end |
#sticky ⇒ Object
Returns the value of attribute sticky
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def sticky @sticky end |
#text ⇒ Object
Returns the value of attribute text
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def text @text end |
#title ⇒ Object
Returns the value of attribute title
6 7 8 |
# File 'lib/groem/notification.rb', line 6 def title @title end |
Instance Method Details
#[](key) ⇒ Object
43 44 45 |
# File 'lib/groem/notification.rb', line 43 def [](key) to_request[key] end |
#callback(*args) ⇒ Object
Note defaults name and type to notification name
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/groem/notification.rb', line 77 def callback *args opts = ((Hash === args.last) ? args.pop : {}) name = args.shift || opts[:context] || self.name type = opts[:type] || self.name target = opts[:target] reset! reset_callback! @callback[GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY] = name if name @callback[GNTP_NOTIFICATION_CALLBACK_CONTEXT_TYPE_KEY] = type if type @callback[GNTP_NOTIFICATION_CALLBACK_TARGET_KEY] = target if target @callback end |
#callback_context ⇒ Object
90 91 92 |
# File 'lib/groem/notification.rb', line 90 def callback_context @callback[GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY] end |
#callback_target ⇒ Object
99 100 101 |
# File 'lib/groem/notification.rb', line 99 def callback_target @callback[GNTP_NOTIFICATION_CALLBACK_TARGET_KEY] end |
#callback_type ⇒ Object Also known as: callback_context_type
94 95 96 |
# File 'lib/groem/notification.rb', line 94 def callback_type @callback[GNTP_NOTIFICATION_CALLBACK_CONTEXT_TYPE_KEY] end |
#dup ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/groem/notification.rb', line 56 def dup attrs = {}; self.each_pair {|k, v| attrs[k] = v.dup if v} n = self.class.new(self.name, attrs) n.callback(:context => callback_context, :type => callback_type, :target => callback_target) if @callback n end |
#header(key, value) ⇒ Object
65 66 67 68 |
# File 'lib/groem/notification.rb', line 65 def header key, value reset! self.headers[growlify_key(key)] = value end |
#icon(uri_or_file) ⇒ Object
70 71 72 73 74 |
# File 'lib/groem/notification.rb', line 70 def icon(uri_or_file) # TODO if not uri reset! header GNTP_NOTIFICATION_ICON_KEY, uri_or_file end |
#reset! ⇒ Object
47 48 49 50 |
# File 'lib/groem/notification.rb', line 47 def reset! @to_request = nil self end |
#reset_callback! ⇒ Object
52 53 54 |
# File 'lib/groem/notification.rb', line 52 def reset_callback! @callback = {} end |
#to_notify ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/groem/notification.rb', line 112 def to_notify %w{name title text sticky priority coalescing_id}.inject({}) do |memo, attr| if val = self.__send__(:"#{attr}") memo["Notification-#{growlify_key(attr)}"] = val end memo end.merge({GNTP_APPLICATION_NAME_KEY => self.application_name}). merge({GNTP_NOTIFICATION_ID_KEY => unique_id}). merge(@callback). merge(self.headers) end |
#to_register ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/groem/notification.rb', line 103 def to_register %w{display_name enabled}.inject({}) do |memo, attr| if val = self.__send__(:"#{attr}") memo["Notification-#{growlify_key(attr)}"] = val end memo end.merge(self.headers) end |
#to_request ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/groem/notification.rb', line 124 def to_request @to_request ||= \ {'environment' => environment, 'headers' => to_notify, 'notifications' => {} } end |