Class: Groem::Notification

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Methods included from Marshal::Request

#dump, included

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_nameObject

Returns the value of attribute application_name

Returns:

  • (Object)

    the current value of application_name



6
7
8
# File 'lib/groem/notification.rb', line 6

def application_name
  @application_name
end

#coalescing_idObject

Returns the value of attribute coalescing_id

Returns:

  • (Object)

    the current value of coalescing_id



6
7
8
# File 'lib/groem/notification.rb', line 6

def coalescing_id
  @coalescing_id
end

#display_nameObject

Returns the value of attribute display_name

Returns:

  • (Object)

    the current value of display_name



6
7
8
# File 'lib/groem/notification.rb', line 6

def display_name
  @display_name
end

#enabledObject

Returns the value of attribute enabled

Returns:

  • (Object)

    the current value of enabled



6
7
8
# File 'lib/groem/notification.rb', line 6

def enabled
  @enabled
end

#environmentObject

Returns the value of attribute environment

Returns:

  • (Object)

    the current value of environment



6
7
8
# File 'lib/groem/notification.rb', line 6

def environment
  @environment
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



6
7
8
# File 'lib/groem/notification.rb', line 6

def headers
  @headers
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/groem/notification.rb', line 6

def name
  @name
end

#priorityObject

Returns the value of attribute priority

Returns:

  • (Object)

    the current value of priority



6
7
8
# File 'lib/groem/notification.rb', line 6

def priority
  @priority
end

#stickyObject

Returns the value of attribute sticky

Returns:

  • (Object)

    the current value of sticky



6
7
8
# File 'lib/groem/notification.rb', line 6

def sticky
  @sticky
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



6
7
8
# File 'lib/groem/notification.rb', line 6

def text
  @text
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of 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_contextObject



90
91
92
# File 'lib/groem/notification.rb', line 90

def callback_context
  @callback[GNTP_NOTIFICATION_CALLBACK_CONTEXT_KEY]
end

#callback_targetObject



99
100
101
# File 'lib/groem/notification.rb', line 99

def callback_target
  @callback[GNTP_NOTIFICATION_CALLBACK_TARGET_KEY]
end

#callback_typeObject 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

#dupObject



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_notifyObject



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_registerObject



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_requestObject



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