module WebTools::Support::ServiceHelper

Public Class Methods

included(base) click to toggle source
# File lib/web_tools/support/service_helper.rb, line 5
def self.included(base)
  base.set :show_exceptions, false
  base.set :raise_errors, false
  base.set :method_override, true
  base.set :logging, true
  base.use Rack::JSONP

  base.error do
    excep = request.env['sinatra.error']
    puts
    p excep.message
    puts excep.backtrace
    json('_stack' => excep.backtrace.join("<br>"),
         '_error' => excep.message)
  end

  base.before do
    @ts = Time.now
  end

  base.helpers do
    def non_meta_name(str)
      if str =~ /^#<Class:.*>$/
        str["#<Class:".length..-2]
      else
        str
      end
    end

    def json(obj)
      content_type :json
      obj.to_hash.tap do |o|
        o["_time"] = ((Time.now - @ts) * 1000).to_i
      end.to_json
    end

    def reflect(obj)
      system.reflect(obj)
    end

    def system
      @reflection ||= MirrorAPI::Reflection.new
    end
  end
end

Public Instance Methods

json(obj) click to toggle source
# File lib/web_tools/support/service_helper.rb, line 34
def json(obj)
  content_type :json
  obj.to_hash.tap do |o|
    o["_time"] = ((Time.now - @ts) * 1000).to_i
  end.to_json
end
non_meta_name(str) click to toggle source
# File lib/web_tools/support/service_helper.rb, line 26
def non_meta_name(str)
  if str =~ /^#<Class:.*>$/
    str["#<Class:".length..-2]
  else
    str
  end
end
reflect(obj) click to toggle source
# File lib/web_tools/support/service_helper.rb, line 41
def reflect(obj)
  system.reflect(obj)
end
system() click to toggle source
# File lib/web_tools/support/service_helper.rb, line 45
def system
  @reflection ||= MirrorAPI::Reflection.new
end