Sha256: 1cb9edb6eb8d23ed20f8346ef0852a7af79a6866e9f0db20c231149842ea6e71

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'net/http'
require 'uri'

module AhcHelper

  def render_ahc(template)
    if defined? Rails && @data
      @data ||= {}
      @data['authenticity_token'] = form_authenticity_token
    end
    if params[:format] == 'json'
      render json: @data
    else
      render text: Ahc.render(template, @data)
    end
  end

end

module AhcInheritedResourcesHelper
  include AhcHelper

  def respond_with(*resources, &block)
    controller = params[:controller].split('/').last
    action = params[:action]
    @data ||= {}
    case params[:action]
    when 'show'
      @data['resource'] = resource
    when 'index'
      @data['resources'] = resources[0]
    when 'create', 'update'
      params[:format] = 'json'
      if resource.valid?
        @data['resource'] = resource
      else
        @data['errors'] = resource.errors
      end
    end
    render_ahc("#{controller}/#{action}")
  end

end

class AhcProxy

  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    path = request.fullpath
    if path.start_with?('/s/') || path.start_with?('/static/') || path.start_with?('/plovr/')
      begin
        response = Net::HTTP.get(URI.parse("http://#{Ahc.host || Ahc.default_host}:#{Ahc.port || Ahc.default_port}" + path))
        return [200, {}, [response]]
      rescue
        return [200, {}, ['Ahc server is not running']]
      end
    end
    @app.call(env)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ahc-0.3.4 lib/ahc_helper.rb