Five Gems

Guilherme Simões

Five Gems

Brought to you by Guilherme Simões

jquery-rails-cdn

Adds CDN support to the jquery-rails gem.

jquery-rails-cdn

# app/views/layouts/application.html.erb
<%= javascript_include_tag "//ajax.googleapis.com/jquery.min.js" %>
<%= javascript_include_tag "application" %>

jquery-rails-cdn

# app/views/layouts/application.html.erb
<%= jquery_include_tag :google %>
<%= javascript_include_tag "application" %>

jquery-rails-cdn

http_accept_language

Detects the user's preferred language sent in the "Accept-Language" HTTP header.

http_accept_language

request.env["HTTP_ACCEPT_LANGUAGE"]
# => en-GB,en;q=0.8,en-US;q=0.6,pt-PT;q=0.4,pt;q=0.2
http_accept_language.user_preferred_languages
# => ["en-GB", "en", "en-US", "pt-PT", "pt"]
available = I18n.available_locales
# => [:en, :pt]
http_accept_language.preferred_language_from(available)
# => "en"

fastimage

Finds the size or type of an image given its URL by fetching as little as needed.

fastimage

FastImage.size("http://fake.url/funny.gif")
# => [300, 50] # width, height
FastImage.type("http://fake.url/png-image-with-no-extension")
# => :png
FastImage.size("http://fake.url/large-image-that-causes-DOS.jpg",
  raise_on_failure: true, timeout: 0.5)
# => FastImage::ImageFetchFailure: FastImage::ImageFetchFailure

fastimage

omniauth

Provides a standardized interface to many different authentication providers such as Facebook and OpenID. Also supports a traditional strategy using username and password.

omniauth

use OmniAuth::Builder do
  provider :github, "GITHUB_KEY", "GITHUB_SECRET"
end

omniauth

# some_view.html.erb
<%= link_to "Login with GitHub", "/auth/github" %>

omniauth

get "/auth/:provider/callback" do
  omniauth = env["omniauth.auth"]
  raise omniauth.to_yaml
end

omniauth

provider: "github"
uid: "531168"
info:
  nickname: "GuilhermeSimoes"
  email: "guilherme.rdems@gmail.com"
  name: "Guilherme Simões"
  image: "https://secure.gravatar.com/avatar/480fec4ec9bf207c739b4bd38ea98aec?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png"
  urls:
    GitHub: "https://github.com/GuilhermeSimoes"
extra:

omniauth

get "/auth/failure" do
  flash[:notice] = params[:message]
  redirect_to "/you-suck-for-not-logging-in-with-GitHub"
end

figaro

Helps configure Rails apps, especially open source Rails apps.

figaro

$ rails generate figaro:install
# Creates a YAML config file and adds it to .gitignore

figaro

# config/application.yml
HELLO: "world"
development:
  HELLO: "developers"
production:
  HELLO: "users"

figaro

# config/environments/development.rb
config.action_mailer.default_url_options = {
  host: "localhost:3000"
}

# config/environments/production.rb
config.action_mailer.default_url_options = {
  host: "example.com"
}

figaro

# config/application.rb
config.action_mailer.default_url_options = {
  host: ENV["MAILER_HOST"]
}

figaro

# config/environments/production.rb
config.action_mailer.smtp_settings = {
  user_name: ENV["EMAIL_USER_NAME"],
  password: ENV["EMAIL_PASSWORD"]
}

figaro

# config/application.yml
MAILER_HOST: "localhost:3000"
production:
  MAILER_HOST: "example.com"

  EMAIL_PASSWORD: "super-difficult-password"
  EMAIL_USER_NAME: "admin"

figaro

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"]
  provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"]
  provider :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"]
end

figaro

# config/initializers/secret_token.rb
YourApp::Application.config.secret_token = ENV["SECRET_TOKEN"]

figaro

$ heroku config:set S3_KEY=totallyrandomstring
$ heroku config:set S3_SECRET=classified
$ heroku config:set PUSHER_KEY=nopenopenopenopenope
$ heroku config:set PUSHER_SECRET=topsecret
$ heroku config:set PUSHER_APP_ID=imoutoffunnyideas
...

figaro

$ rake figaro:heroku
# One command to push ENV variables to Heroku

figaro alternatives

Other Resources

Thanks!

Fork me on Github