module RailsGoogleFonts

Constants

VERSION

Public Class Methods

all() click to toggle source

Get all fonts from the Google Fonts API

Example:

>> RailsGoogleFonts.all
=> [..., {"family"=>"Reenie Beanie", "category"=>"handwriting", "variants"=>["regular"], ...}, ...]
# File lib/rails_google_fonts.rb, line 27
def self.all
  if @key
    @options[:query][:sort] = 'alpha'
    response = HTTParty.get('https://www.googleapis.com/webfonts/v1/webfonts', @options).parsed_response

    if response['error']
      raise "Unexpected error: #{response['error']['errors'][0]['reason']}"
      return response['error']
    else
      return response['items']
    end

  else
    raise "Google API key is not set"
  end
end
all_by(sort='alpha') click to toggle source

Get all fonts sorted by a parameter

Example:

>> RailsGoogleFonts.all_by('trending')
=> [{..., "family"=>"Amita", ...}, ...]

Arguments:

sort: (String)
# File lib/rails_google_fonts.rb, line 53
def self.all_by(sort='alpha')
  if @key
    @options[:query][:sort] = sort
    response = HTTParty.get('https://www.googleapis.com/webfonts/v1/webfonts', @options).parsed_response

    if response['error']
      raise "Unexpected error: #{response['error']['errors'][0]['reason']}"
      return response['error']
    else
      return response['items']
    end

  else
    raise "Google API key is not set"
  end
end
category(category) click to toggle source

Get all fonts from a category

Example:

>> RailsGoogleFonts.category('display')
=> [..., {..., "family"=>"Sniglet", "category"=>"display", ...}, ...]

Arguments:

category: (String)
# File lib/rails_google_fonts.rb, line 79
def self.category(category)
  if @key
    return RailsGoogleFonts.all.select { |a| a['category'] == category }
  else
    raise "Google API key is not set"
  end
end
key(key) click to toggle source

Set Google API key for access to the fonts API

Example:

>> RailsGoogleFonts.key(ENV['GOOGLE_API_KEY'])
=> {:query {:key => "1234567890ASDasdASAsdasdASDASdad}}

Arguments:

key: (String)
# File lib/rails_google_fonts.rb, line 16
def self.key(key)
  @key = key
  @options = { query: {key: @key} }
end