require 'net/http' require 'open-uri' module Cytoplasm class FontsController < ApplicationController def fetch_imported imported = {} fontsdir = "public/fonts" # Find fonts directory, create one if it doesn't exist if File.exists?(fontsdir) fontsdir = Dir.open(fontsdir) else Dir.chdir("public") fontsdir = Dir.mkdir("fonts") Dir.chdir("../") end fontsdir.each do |file| imported[file] = fetch_fs_family(file) if File.directory?(file) and file!="." and file!=".." end return imported end def fetch_fs_family(font) return fetch_json("http://www.fontsquirrel.com/api/familyinfo/"+font) end def fetch_all success = {"fontsquirrel" => {}, "googlewebfonts" => {},"imported" => {}} success["fontsquirrel"] = fetch_json("http://www.fontsquirrel.com/api/fontlist/all") fetch_json("https://www.googleapis.com/webfonts/v1/webfonts?key="+Cytoplasm.conf("fontloader.googlewebfonts_apikey"))["items"].each {|f| success["googlewebfonts"][f["family"]] = f} success["imported"] = fetch_imported() render :text => Cytoplasm::Ajax.ajax_success(success) end private def fetch_json(url) return ActiveSupport::JSON.decode(open(url).read) end end end