RailsGoogleFonts

Rails Google Fonts is a gem/wrapper to ease the use of Google Fonts with your Rails app. It's more of an API client sort of thing.

Requirements

Your Google Developer API key. You can get one at https://console.developers.google.com/ (you will need to create a project, if you haven't already!).

Installation

Add this line to your application's Gemfile:

gem 'rails_google_fonts'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_google_fonts

Usage

Setup

Include your Google Developer API key. Tip: Store your API key in the environment variable for extra security!

RailsGoogleFonts.key(ENV['GOOGLE_API_KEY'])

or (unsafe!)

RailsGoogleFonts.key('YOUR_GOOGLE_API_KEY')

Get all the fonts

Simply:

RailsGoogleFonts.all # Returns a list of object containing all the fonts

This will raise an error if the API key is not set

Get all fonts sorted by a parameter

Get a sorted list of all the fonts, sorted by a parameter. Possible parameters are (shamelessly copied from https://developers.google.com/fonts/docs/developer_api):

  • alpha: Sort the list alphabetically
  • date: Sort the list by date added (most recent font added or updated first)
  • popularity: Sort the list by popularity (most popular family first)
  • style: Sort the list by number of styles available (family with most styles first)
  • trending: Sort the list by families seeing growth in usage (family seeing the most growth first)

Example:

RailsGoogleFonts.all_by('trending')

Setting an invalid parameter will raise an error

Filter by font category

Filter the fonts using the category function. Known font categories are:

  • serif
  • sans-serif
  • monospace
  • handwriting
  • display

Example:

RailsGoogleFonts.category('sans-serif') # Returns a list of objects in the sans-serif category

Returns an empty array for invalid categories

Testing

Test the build by running rake test

Contributing

  1. Fork it ( https://github.com/[my-github-username]/rails_google_fonts/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request