Sha256: dfb56e60f4d9a0f478f34f80c5d7d842226b16d0e0e61b3a4200b1c07366d701

Contents?: true

Size: 883 Bytes

Versions: 7

Compression:

Stored size: 883 Bytes

Contents

require 'net/http'
require 'uri'
require 'ahc_helper'

class Ahc

  class << self
    attr_accessor :host, :port, :fake
  end

  def self.root
    @root ||= File.expand_path '../..', __FILE__
    @root
  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
  end

  def self.default_host
    '127.0.0.1'
  end

  def self.default_port
    '8080'
  end
  
end

if defined? Rails
  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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ahc-0.3.6 lib/ahc.rb
ahc-0.3.5 lib/ahc.rb
ahc-0.3.4 lib/ahc.rb
ahc-0.3.3 lib/ahc.rb
ahc-0.3.2 lib/ahc.rb
ahc-0.3.1 lib/ahc.rb
ahc-0.3.0 lib/ahc.rb