fanforce-plugin-factory
The fanforce-plugin-factory is a gem that makes it really easy to create new Fanforce Plugins using convention over configuration.
Easy Setup
Only two files are required to setup a new project:
- Gemfile
- config.ru
/Gemfile
Since the fanforce-plugin-factory gem is hosted on a private gem server so you'll need to specify the source for both rubygems and the private server.
source 'https://rubygems.org'
source 'https://gems.gemfury.com/xF8KY9sm8eS9yUQqw1jD/'
ruby '1.9.3'
gem 'fanforce-plugin-factory'
One you've created the Gemfile you'll need to run bundler install.
/config.ru
Only seven lines are required (in addition to having a Redis server).
require 'bundler'; Bundler.setup
require 'fanforce/plugin'
FanforcePlugin.config do |config|
config._id = 'watch-youtube'
config.type = :behavior # :data_connector, :data_processor, :broadcaster, :identifier, :behavior
end
run FanforcePlugin
You can replace config._id (as in the example above) with an environment variable: ENV['FANFORCE_PLUGIN_ID']
Ready for the Fanforce Marketplace!
Your plugin can now be added to the Fanforce Marketplace. Use the follow config settings when adding your new plugin into Fanforce
"public_urls": {
"icon_16": "http://${PRIMARY_DOMAIN}/assets/icon-16.png",
"icon_32": "http://${PRIMARY_DOMAIN}/assets/icon-32.png",
"icon_42": "http://${PRIMARY_DOMAIN}/assets/icon-42.png"
},
"staffer_ui_urls": {
"add_initiative": "http://${PRIMARY_DOMAIN}/add_initiative.html",
"edit_initiative": "http://${PRIMARY_DOMAIN}/edit_initiative.html"
},
"callback_urls": {
"install": "http://${PRIMARY_DOMAIN}/install",
"uninstall": "http://${PRIMARY_DOMAIN}/uninstall"
},
"widget_dependencies": [
{
"widget-name-here": "http://${PRIMARY_DOMAIN}/widget_tmpls"
}
]
Note: for development and testing we recommend using the POW server, but you can use rackup or any other server to run your new plugin.
====================
Customizing/Building Your Plugin
There are lots of ways you can start building out and customizing your plugin...
/assets
Use the assets folder to store all your images, css, and javascript files. There are six asset files that are required. If any are missing, the fanforce-plugin-factory sends default versions when requested.
Default Assets:
- /css
- /add_edit_initiative.css - this is loaded from the add_edit_initiative.haml layout.
- /add_source.css - this is loaded from the add_source.haml layout.
- /engage.css - this is loaded from the engage.haml layout.
- /new_message.css - this is loaded from the new_message.haml layout.
- /source_details.css - this is loaded from the source_details.haml layout.
- /js
- /add_edit_initiative.js - this is loaded from the add_edit_initiative.haml layout.
- /add_source.js - this is loaded from the add_source.haml layout.
- /add_source_popup.js - this is loaded from the add_source_popup.haml layout.
- /engage.js - this is loaded from the engage.haml layout.
- /new_message.js - this is loaded from the new_message.haml layout.
- /source_details.js - this is loaded from the source_details.haml layout.
- /img
- /icon-16.png - this icon must be 16px by 16px.
- /icon-32.png - this icon must be 32px by 32px.
- /icon-42.png - this icon must be 42px by 42px.
/views
HAML is the default layout engine. The .haml extension is converted to .html when served in the browser.
Default Views for Behavior Plugins:
- /add_initiative.haml - used in the Command Center.
- /edit_initiative.haml - used in the Command Center.
- /engage.haml - used in the Command Center.
Default Views for Broadcaster Plugins:
- /new_message.haml - used in the Command Center.
/layouts
You can turn off layouts from be used (see Advanced Configuration)
Default Layouts for Behavior Plugins:
- /add_edit_initiative.haml - the wrapper layout for views/add_initiative.haml and views/edit_initiative.haml
- /engage.haml - the wrapper layout for views/engage.haml
Default Layouts for Broadcaster Plugins:
- /new_message.haml - the wrapper layout for views/new_message.haml
/public
Anything you put in here will be publicly available. The only folder you should not use is /assets, since it is used for precompiling assets (see below).
Default Files:
- /favicon.ico
/config/routes.rb
The fanforce-plugin-factory sets up all the required routes. If you want to customize existing routes or add new ones, create a routes.rb file in your home directory with a FanforcePlugin:Sinatra class:
class FanforcePlugin::Sinatra
get '/test' do
"Custom route!"
end
end
Check out Sinatra's documentation for more details on routes and much more. The FanforcePlugin::Sinatra inherits from the Sinatra::Base, which gives you full control to customize the fanforce-plugin-factory's sinatra implementation as much as you want.
Overwrite any existing routes by specifying them in your routes.rb. You can also pull default route logic into your new custom routes by calling route_[NAME_OF_ROUTE] method.
class FanforcePlugin::Sinatra
any '/install' do
# do whatever you want here
route_install
end
end
Default Routes:
- route_index: get ['/','/index.html']
- route_install: post '/install'
- route_uninstall: post '/uninstall'
Default Routes for Behavior Plugins:
- route_add_initiative: get '/add_initiative.html'
- route_edit_initiative: get '/add_initiative.html'
- route_widget_tmpls: get '/widget_tmpls.html'
- route_engage: get '/:fanforce_slug/:initiative_slug/engage.html'
Default Routes for Broadcaster Plugins:
- route_new_message: get '/new_message.html'
Default Routes for Data Connector Plugins:
- route_add_source: get '/add_source.html'
- route_source_details: get '/source_details.html'
/config/initializer.rb
If you need to run Sinatra middleware or other non-route code, you'll want to create a config/initializer.rb file. This file is required before config/routes.rb. As an example, if you want to use the OmniAuth Facebook Strategy, you would include the following in config/initializer.rb:
require 'omniauth'
require 'omniauth-constantcontact2'
class FanforcePlugin::Sinatra
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :constantcontact, ENV['CONSTANTCONTACT_API_KEY'], ENV['CONSTANTCONTACT_API_SECRET']
end
end
/lib/*
If you create a lib/ folder in your plugin's home directory, it will be automatically added to your plugin's ruby path. This allows you to require 'any_file' that has been put in this folder.
Using Pages/Layouts In Controllers
Fanforce-plugin-factory adds a helper method on top of the Sinatra code base to load pages and layouts. It's called page:
get '/custom.html' do
page :custom, :layout => :custom
end
Other Examples
- :custom_page
- :custom_page, :layout => :false
Rendering JSON, JSONP, and JSONR
Fanforce-plugin-factory adds a "json" method you use in your routes to convert the object to json and set the correct application/json header:
get '/custom.html' do
json first_name: 'Caleb', last_name: 'Clark'
end
If a callback param is sent in the request then your json is automatically converted to JSONRhttp://github.com/calebclark/rack-jsonr.
Asset Helpers
Fanforce-plugin-factory comes with a few asset helpers you can use in your haml pages:
- stylesheet(absolute_or_relative_path) - creates style tag
- javascript(absolute_or_relative_path) - creates script tag
- asset_path(absolute_or_relative_path) - prints path to asset
Precompile Your Assets for Faster Delivery
Heroku tries to run rake assets:precompile
when pushing new code changes. Fanforce-plugin-factory has this rake task built-in. To activate, just create a new Rakefile in your plugin directory with the following:
require 'bundler'; Bundler.setup
require 'fanforce/plugin'
FanforcePlugin.config do |config|
config.type = :behavior # :data_connector, :data_processor, :broadcaster, :identifier, :behavior
end
load 'fanforce/plugin.rake'
Configure Redis
By default fanforce-plugin-factory looks for ENV['REDIS_URL'] and if that isn't found, it falls back to redis://localhost:6379. You can specify a custom redis url with config.redis_url =
in config.ru:
FanforcePlugin.config do |config|
config.redis_url = 'redis://USERNAME:PASSWORD@HOST:PORT'
end
Using Fanforce::Workers
FanforcePlugin includes the fanforce-workers gem by default and includes a convenience helper method. FanforcePlugin.enqueue will prefix the name of your worker with the name of your plugin to ensure there are no conflicts across multiple plugins or apps. This is the naming convention used by Fanforce Factory when creating and uploading Iron.io workers.
As an example, if you have a testing.worker file, you would use the following command:
FanforcePlugin.enqueue 'testing', {params_to_send: 'whatever'}