lib/ahc.rb in ahc-0.2.7 vs lib/ahc.rb in ahc-0.3.0

- old
+ new

@@ -1,60 +1,45 @@ -require 'erubis' -require 'ahcx_template_handler' +require 'net/http' +require 'uri' +require 'ahc_helper' class Ahc - def self.render(template_name, params) - if @@templates[template_name] - css = lambda {|css_class| - @@css_map[css_class] || css_class - } - return @@templates[template_name].result({:params => params, :css => css}).strip - end - return '' + class << self + attr_accessor :host, :port, :fake end - def self.read_templates(paths) - @@templates = {} - @@css_map ||= {} - paths.each do |path| - if File.directory?(path) - Dir.glob(path+"/**/*.css") do |item| - next if File.directory?(item) - provides = css_provides(item) - template = get_template(item) - provides.each do |provide| - @@templates[provide] = template if template - end - end - end - end + def self.root + @root ||= File.expand_path '../..', __FILE__ + @root end - def self.set_css_map(css_map) - @@css_map = css_map - end - - def self.css_provides(stylesheet_path) - provides = [] - File.readlines(stylesheet_path).each do |line| - match = /@provide ['|"](.*)['|"]/.match(line) - if match - provides << match[1] - end + def self.render(template, data) + return '' if @fake + begin + res = Net::HTTP.post_form(URI.parse("http://#{@host || default_host}:#{@port || default_port}/t/" + template), {'params'=> data.to_json}) + return res.body + rescue + return File.read(self.root + '/static/error.html') end - provides end - def self.get_template(stylesheet_path) - template_path = stylesheet_path.sub(%r{\.css$}, ".erb") - return nil unless File.exists?(template_path) - return Erubis::Eruby.new(File.read(template_path)) + def self.default_host + '127.0.0.1' end + def self.default_port + '8080' + end + end -Ahc.read_templates(['components']) if File.directory?('components') - if defined? Rails - ActionView::Template.register_template_handler(:ahcx, AhcxTemplateHandler) + config_path = "#{Rails.root}/config/ahc.yml" + config = {} + if File.exists?(config_path) + config = YAML.load_file(config_path)[Rails.env] || {} + end + Ahc.host = config['host'] + Ahc.port = config['port'] + Ahc.fake = config['fake'] end