Sha256: b5314f76a1d73633d454d1307ab757ae0d8316323ea94438b3da0a2332fbe986

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cytoplasm-0.0.7 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.6 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.5 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.4 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.3 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.2 app/controllers/cytoplasm/fonts_controller.rb
cytoplasm-0.0.1 app/controllers/cytoplasm/fonts_controller.rb