Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Middleman: The missing front end of the Rails A...

Middleman: The missing front end of the Rails API stack

There’s a lot of talk around using Rails as a JSON API back-end, but less on how to use the same great tools we’ve been using in Rails to build front end single page JavaScript apps.

Learn how Middleman, a Ruby static website generator, is used by Poll Everywhere to build massively scalable and responsive front end JavaScript apps that talk to a Rails API server. I discuss topics including dependency management between several front ends and an API, env targeting with Foreman, deployments using rsync and nginx, and more.

Brad Gessler

April 23, 2014
Tweet

More Decks by Brad Gessler

Other Decks in Programming

Transcript

  1. Getting Started with Middleman $ gem install middleman ! $

    middleman init my-app create my-app/.gitignore create my-app/config.rb create my-app/source/index.html.erb create my-app/source/layouts/layout.erb create my-app/source/stylesheets create my-app/source/javascripts ! $ cd my-app ; middleman server == The Middleman is standing watch at http://0.0.0.0:4567
  2. Has all of the same front-end goodies from Rails, so

    you’ll feel right at home gem 'haml' gem 'sass' gem 'sprockets' gem 'coffeescript' gem 'compass' gem 'activesupport' gem 'susy'
  3. Middleman is multi-environment aware # ./config.rb # Local dev server

    settings configure :development do activate :livereload end ! # Production settings configure :build do activate :minify_css activate :minify_javascript activate :asset_hash end
  4. A simple two step deploy process task :build do sh

    'bundle exec middleman build' end ! task :upload do sh 'rsync -avz ./build/ [email protected]:/www/' end ! task deploy: %w[build upload]
  5. Static Dynamic Rails! More caching Middleman! HTML content Middleman! JS

    MVC Library Rails! Less caching Realtime visualizations! Poll Ev graphs GUI-oriented applications! Google Spreadsheets Document-oriented web apps! Invoicing application Blogging platform! Svbtle, Posthaven Personal blog! bradgessler.com Informational website! Steve’s Plumbin’ Services
  6. A Flex app was created within the rails project for

    PowerPoint visualizations Rails App polleverywhere.com Flex App
  7. Rails App polleverywhere.com XML API Flex App We broke the

    Flex app into its own project so that our Flex contractor could deploy independently from the Rails app team
  8. Rails App polleverywhere.com XML API Flex App Smart phones started

    taking off, so we built .mobi extension views in Rails with jQuery mobile jQm/.mobi
  9. Rails App polleverywhere.com XML API Flex App jQuery mobile and

    .mobi was a big disaster. Framework got us 80% there, the other 20% was painful and error prone jQm/.mobi
  10. Our team made the decision to hand pick libraries to

    have exactly what we need, when we need it. ! Framework agnostic Middleman would be there to manage all of the assets
  11. Rails App! polleverywhere.com XML API Flex App Our first single

    page HTML application was built in Middleman at PollEv.com JSON API Mobile App
  12. Rails App! polleverywhere.com Visualization App Flash was on its deathbed,

    so we set out to replace the Flex app with another single page Middleman app JSON API Mobile Web App
  13. To reduce latency, a Stream API was deployed to production

    Rails App! polleverywhere.com Visualization App Mobile Web App Stream API JSON API
  14. Stream API was isolated to its own host and server

    fleet for isolation and stability purposes. Client-side SOA allowed us to start using the Stream API, stabilize in production, gain confidence, and ensure a smooth rollout.
  15. Rails App JSON API Mobile Web App Stream API More

    apps emerge on the scene, how do we keep it all DRY? Visualization App PowerPoint App Mobile Web App Keynote App
  16. Rails App JSON API Visualization App PowerPoint App Mobile Web

    App Stream API Extract common Coffeescript, Sass, assets into a sprockets asset gem Keynote App Sprockets Asset Gem
  17. Create a sprockets asset gem to share assets between apps

    # The ./lib/pollev_assets.rb file require 'pollev_assets/version' ! if defined? Sprockets Sprockets.env.append_path 'lib/assets/javascripts' Sprockets.env.append_path 'lib/assets/stylesheets' Sprockets.env.append_path 'vendor/assets/javascripts' end
  18. Manage asset gem dependencies and branches with Bundler # App

    ./Gemfile if path = ENV['ASSETS_GEM_PATH'] gem "pollev_assets", path: path else gem "pollev_assets", branch: 'new-feature' end
  19. Rails App JSON API Visualization App … Mobile Web App

    Stream API We reused mobile app assets to build PowerPoint 2013 app in 2 weeks PowerPoint 2013 App Sprockets Asset Gem …
  20. Rails App JSON API … PowerPoint App Mobile Web App

    Stream API Extract content from Rails app so marketing team can move faster Keynote App Sprockets Asset Gem Form API Content Site! Middleman App
  21. Some dynamic components are still required Render login state via

    JavaScript snippet Integration between Middleman content site and Rails app should have well defined and tested integration points Stripe.js, GA, Optimizely are all dynamic tools that can be integrated into a static website
  22. Why bother with a static content website? Don’t let “TechCrunched”

    or HN’ed or whatever the cool kids call it these days bring down your site Introduce completely different workflows into content production without a heavy CMS Easier deployments, can you upload a file?
  23. …a few things I didn’t cover How to not use

    hash bang URLs with pushState 12-factor middleman apps Validate test, staging, and any other environment with acceptance tests The state of exception monitoring in production environments Building an API side-by-side with client code deployments