Sha256: 6f8c4e90997b797c0aa06becf0e746159bb7ed75677fb4129c46a90f030fe3e9

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

require 'json'
require 'fileutils'

module WebFont
  module Data

    class << self
      attr_accessor :path
    end

    # Download fonts data from Google and index it
    #
    # Returns nothing
    def self.download(destination = nil)
      raise ArgumentError, "ENV['GOOGLE_API_KEY'] is nil" unless ENV['GOOGLE_API_KEY']

      destination ||= path
      FileUtils.mkdir_p(destination) unless Dir.exist?(destination)

      url = "https://www.googleapis.com/webfonts/v1/webfonts?key=#{ENV['GOOGLE_API_KEY']}"
      system("wget -O #{destination}/fonts.json #{url}")

      index(destination)
    end

    private

    # Index fonts data so it will speed things up when search
    #
    # Returns nothing
      def self.index(destination)
        indices           = {}
        alphabet          = 'a'
        indices[alphabet] = { 'start' => 0 }

        hash = JSON.parse(File.open("#{destination}/fonts.json").read)
        hash['items'].each_with_index do |item, index|
          family = item['family']

          if family[0].downcase != alphabet
            indices[alphabet]['end'] = index - 1

            alphabet = family[0].downcase
            indices[alphabet] = { 'start' => index }
          end
        end
        indices[alphabet]['end'] = hash['items'].size - 1

        File.open("#{destination}/index.json", 'w') { |file| file.write(indices.to_json) }
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
web_font-0.0.3 lib/web_font/data.rb
web_font-0.0.2 lib/web_font/data.rb
web_font-0.0.1 lib/web_font/data.rb