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: 1. Gemfile 2. 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. ```ruby source 'https://rubygems.org' source 'https://gems.gemfury.com/xF8KY9sm8eS9yUQqw1jD/' ruby '2.1.6' gem 'fanforce-plugin-factory' ``` One you've created the Gemfile you'll need to run bundle install. ### /config.ru Only four lines are required (you must have a Redis server). ```ruby require 'bundler'; Bundler.setup require 'fanforce/plugin_factory' Fanforce::Plugin.config { |config| config._id = 'facebook' } run Fanforce::Plugin ``` 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 ```ruby "public_urls": { "marketplace_img": "http://${PRIMARY_DOMAIN}/assets/marketplace.png", "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": { "dashboard": "http://${PRIMARY_DOMAIN}/dashboard.html" }, "callback_urls": { "install": "http://${PRIMARY_DOMAIN}/install", "uninstall": "http://${PRIMARY_DOMAIN}/uninstall" }, "plugin_dependencies": [] ``` Note: for development and testing we recommend using the [POW server](http://pow.cx/), 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:* * /js * /dashboard.js - this is included in the dashboard.html file. * /css * /dashboard.css - this is also included in the dashboard.html file. * /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. * /marketplace.png - this icon must be 220px by 142px. ### /views HAML is setup by default. The extension is changed to .html when served in the browser. *Default Views:* * /index.haml - default page visitors will see if they try to access domain * /dashboard.haml - used in the Command Center ### /layouts You can turn off layouts from be used (see Advanced Configuration) *Default Layouts:* * /dashboard.haml - the wrapper layout for views/dashboard.haml * /promotional.haml - the wrapper layout for views/index.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 config/routes.rb file and specify routes within the Plugin:Sinatra class: ``` class Plugin::Sinatra get '/test' do "Custom route!" end end ``` Check out [Sinatra's](http://www.sinatrarb.com/documentation) documentation for more details on routes and much more. The Plugin::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 config/routes.rb. You can also pull default route logic into your new custom routes by calling route_[NAME_OF_ROUTE] method. ``` class Plugin::Sinatra any '/install' do # do whatever you want here route_install end end ``` *Default Routes:* - route_index: get ['/','/index.html'] - route_dashboard: get '/dashboard.html' - route_install: post '/install' - route_uninstall: post '/uninstall' ### /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 Plugin::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. # Loading 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 JSONR[http://github.com/calebclark/rack-jsonr] (JSON Resource). # 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' load 'fanforce/plugin.rake' ``` # Create Plugin-Specific ENV Variables In Development Documentation coming soon. # 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: ``` require 'bundler'; Bundler.setup require 'fanforce/plugin_factory' Fanforce::Plugin.config do |config| config.redis_url = 'redis://USERNAME:PASSWORD@HOST:PORT' end load 'fanforce/plugin.rake' ``` # Using Fanforce::Worker Fanforce::Plugin includes the fanforce-workers gem by default and includes a convenience helper method. Fanforce::Plugin.enqueue will prefix the name of your worker with the name of your plugin to ensure there are no conflicts across multiple plugins or plugins. 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: ``` Fanforce::Plugin.enqueue 'testing', {params_to_send: 'whatever'} ```