Writing a Plugin
Plugins are essentially a thin wrapper around the Reflection API packaged up for others to use.
Let's take a look at an example:
plugin = Rpush.plugin(:my_plugin)
plugin.url = 'https://github.com/me/my_plugin'
plugin.description = "My example plugin, it's awesome!"
These are the basic requirements of our plugin, all plugins must have this!
Next lets add some configuration options to our plugin:
plugin.configure do |config|
config.awesome_mode = true
end
Here config is an instance of an OpenStruct
, so you can assign anything you like to it. This allows you to expose configuration options to users of your plugin. If someone didn't want your plugin to run in awesome mode, they'd disable the option in their rpush.rb
like so:
Rpush.configure do |config|
config.my_plugin.awesome_mode = false
end
Note that my_plugin
is the same as the symbol we passed to Rpush.plugin(:my_plugin)
.
Now we need to make our plugin actually do something:
plugin.reflect do |on|
on.notification_delivered do |notification|
do_something_with(notification)
end
end
This is where we setup some hooks using the Reflection API. This is functionally equivalent to Rpush.reflect
like you already have in your rpush.rb
.
Finally, your plugin may need to perform some kind of setup when Rpush boots up:
plugin.init do
@socket = connect_to_some_service(awesome: config.my_plugin.awesome_mode)
end