Module: RailsGoogleFonts

Includes:
HTTParty
Defined in:
lib/rails_google_fonts.rb,
lib/rails_google_fonts/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.allObject

Get all fonts from the Google Fonts API

Example:

>> RailsGoogleFonts.all
=> [..., {"family"=>"Reenie Beanie", "category"=>"handwriting", "variants"=>["regular"], ...}, ...]


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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') ⇒ Object

Get all fonts sorted by a parameter

Example:

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

Arguments:

sort: (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

Get all fonts from a category

Example:

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

Arguments:

category: (String)


79
80
81
82
83
84
85
# 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) ⇒ Object

Set Google API key for access to the fonts API

Example:

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

Arguments:

key: (String)


16
17
18
19
# File 'lib/rails_google_fonts.rb', line 16

def self.key(key)
  @key = key
  @options = { query: {key: @key} }
end