class ApiDocGeneration::ViewHelper

Public Class Methods

new(args) click to toggle source

{:a => 1, :b => 2}

# File lib/api_doc_generation/view_helper.rb, line 15
def initialize(args)
  @args = args
  
  args.each do |key, val|
    self.instance_variable_set("@#{key}", val)
  end
end
render(template, args = {}, file_path = '') click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 4
def self.render(template, args = {}, file_path = '')
  template.result(self.new(args).obj_binding)

rescue => e
  e.backtrace[0].gsub!(/^\(erb\)/, file_path)
  
  raise e
end

Public Instance Methods

action_identifer(controller_name, action_name) click to toggle source

Helper #######################

# File lib/api_doc_generation/view_helper.rb, line 46
def action_identifer(controller_name, action_name)
  "action-#{Digest::MD5.hexdigest(controller_name)}-#{action_name}"
end
controller_identifer(controller_name) click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 51
def controller_identifer(controller_name)
  "controller-#{Digest::MD5.hexdigest(controller_name)}"
end
format_other_param(param) click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 102
def format_other_param(param)
  
end
format_param(param) click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 56
def format_param(param)
  return unless param['val'].to_s =~ /^\s*$/

  case param['name']
  when 'app_uname'
    param['type'] = 'String'
    param['val'] = 'app的唯一标识名,表明调用此api是在哪个app下'
  when 'access_token'
    param['type'] = 'String'
    param['val'] = '请求的唯一标识,用于识别用户,用户登陆返回或是使用refresh_token换取'
  when 'refresh_token'
    param['type'] = 'String'
    param['val'] = '用户登陆时返回,使用此token来取得新的access_token'
  when 'page'
    param['type'] = 'Integer'
    param['val'] = '返回所有数据中的第几页'
  when 'perpage'
    param['type'] = 'Integer'
    param['val'] = '返回数据每页多少条'
  end
end
format_response_param(param) click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 79
def format_response_param(param)
  return unless param['val'].to_s =~ /^\s*$/

  case param['name']
  when 'current_page'
    param['type'] = 'Integer'
    param['val'] = '当前返回数据是第几页'
  when 'perpage'
    param['type'] = 'Integer'
    param['val'] = '每页返回的数据量'
  when 'count'
    param['type'] = 'Integer'
    param['val'] = '数据总数量'
  when 'items'
    param['type'] = 'Array'
    param['val'] = '数组中存放当前页的数据'
  when 'total_pages'
    param['type'] = 'Integer'
    param['val'] = '数据的总页数'
  end
end
obj_binding() click to toggle source
# File lib/api_doc_generation/view_helper.rb, line 24
def obj_binding
  binding
end
render(path, opts = {}) click to toggle source

View Helper ###################

# File lib/api_doc_generation/view_helper.rb, line 33
def render(path, opts = {})
  path = "templates/_#{path}.*.erb"
  file_path = Dir[File.expand_path(path, ROOT_PATH)].first
  raise "no found file '#{path}'" unless file_path

  template = ERB.new(File.read(file_path))

  self.class.render(template, @args.merge(opts), file_path)
end