Class: Groem::App

Inherits:
Struct
  • Object
show all
Includes:
Marshal::Request
Defined in:
lib/groem/app.rb

Constant Summary collapse

DEFAULT_HOST =
'localhost'
DEFAULT_PORT =
23053
DEFAULT_ENV =
{'protocol' => 'GNTP', 'version' => '1.0',
 'request_method' => 'REGISTER', '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, opts = {}) ⇒ App

Returns a new instance of App.



15
16
17
18
19
20
21
22
# File 'lib/groem/app.rb', line 15

def initialize(name, opts = {})
  self.environment, self.headers, self.notifications = {}, {}, {}
  self.environment = DEFAULT_ENV.merge(opts.delete(:environment) || {})
  self.host = opts.delete(:host) || DEFAULT_HOST
  self.port = opts.delete(:port) || DEFAULT_PORT
  self.headers[GNTP_APPLICATION_NAME_KEY] = name
  opts.each_pair {|opt, val| self.headers[growlify_key(opt)] = val }
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment

Returns:

  • (Object)

    the current value of environment



5
6
7
# File 'lib/groem/app.rb', line 5

def environment
  @environment
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



5
6
7
# File 'lib/groem/app.rb', line 5

def headers
  @headers
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host



5
6
7
# File 'lib/groem/app.rb', line 5

def host
  @host
end

#notificationsObject

Returns the value of attribute notifications

Returns:

  • (Object)

    the current value of notifications



5
6
7
# File 'lib/groem/app.rb', line 5

def notifications
  @notifications
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



5
6
7
# File 'lib/groem/app.rb', line 5

def port
  @port
end

Instance Method Details

#[](key) ⇒ Object

used by Marshal::Request#dump



25
26
27
# File 'lib/groem/app.rb', line 25

def [](key)
  to_request[key]
end

#binary(key, value_or_io) ⇒ Object



77
78
79
# File 'lib/groem/app.rb', line 77

def binary(key, value_or_io)
  #TODO
end

#callbacks(&blk) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/groem/app.rb', line 60

def callbacks &blk
  if blk.arity == 1
    blk.call(self)
  else
    instance_eval(&blk)
  end      
end

#header(key, value) ⇒ Object



68
69
70
# File 'lib/groem/app.rb', line 68

def header(key, value)
  self.headers[growlify_key(key)] = value
end

#icon(uri_or_file) ⇒ Object



72
73
74
75
# File 'lib/groem/app.rb', line 72

def icon(uri_or_file)
  # TODO if not uri
  header GNTP_APPLICATION_ICON_KEY, uri_or_file
end

#nameObject



29
# File 'lib/groem/app.rb', line 29

def name; self.headers[GNTP_APPLICATION_NAME_KEY]; end

#notification(name, *args) {|n| ... } ⇒ Object

Yields:

  • (n)


53
54
55
56
57
58
# File 'lib/groem/app.rb', line 53

def notification(name, *args)
  n = Groem::Notification.new(name, *args)
  yield(n) if block_given?
  n.application_name = self.name
  self.notifications[name] = n
end

#notify(name, *args, &blk) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/groem/app.rb', line 40

def notify(name, *args, &blk)
  return unless n = self.notifications[name]
  opts = ((Hash === args.last) ? args.pop : {})
  title = args.shift
  n = n.dup    # copies attributes so not overwritten
  n.title = title ? title : self.name
  if cb = opts.delete(:callback)
    n.callback(cb) 
  end
  opts.each_pair {|k, v| n.__send__ :"#{k}=", v}
  send_notify(n, &blk)
end

#register(&blk) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/groem/app.rb', line 31

def register(&blk)
  if blk.arity == 1
    blk.call(self)
  else
    instance_eval(&blk)
  end
  send_register
end

#to_requestObject



117
118
119
120
121
122
# File 'lib/groem/app.rb', line 117

def to_request
  {'environment' => self.environment, 
   'headers' => self.headers, 
   'notifications' => notifications_to_register
  }
end

#when_callback(action, path = nil, &blk) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/groem/app.rb', line 103

def when_callback action, path=nil, &blk
  action = growlify_action(action)
  path = \
    case path
    when String
      [path, nil]
    when Hash
      [path[:context], path[:type]]
    end
  #puts "App defined callback: #{action} #{path.inspect}"      
  notify_callbacks[Groem::Route.new(action, path)] = blk
end

#when_click(path = nil, &blk) ⇒ Object



91
92
93
# File 'lib/groem/app.rb', line 91

def when_click path=nil, &blk
  when_callback GNTP_CLICK_CALLBACK_RESULT, path, &blk
end

#when_close(path = nil, &blk) ⇒ Object



95
96
97
# File 'lib/groem/app.rb', line 95

def when_close path=nil, &blk
  when_callback GNTP_CLOSE_CALLBACK_RESULT, path, &blk
end

#when_register(&blk) ⇒ Object

—- callback definition methods



83
84
85
# File 'lib/groem/app.rb', line 83

def when_register &blk
  @register_callback = blk
end

#when_register_failed(&blk) ⇒ Object



87
88
89
# File 'lib/groem/app.rb', line 87

def when_register_failed &blk 
  @register_errback = blk
end

#when_timedout(path = nil, &blk) ⇒ Object



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

def when_timedout path=nil, &blk
  when_callback GNTP_TIMEDOUT_CALLBACK_RESULT, path, &blk
end