Class: Restapi::RestapisController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/restapi/restapis_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/restapi/restapis_controller.rb', line 5

def index
  respond_to do |format|

    if Restapi.configuration.use_cache?
      path = Restapi.configuration.doc_base_url.dup
      if [:resource, :method, :format].any? { |p| params[p].to_s =~ /\W/ }
        head :bad_request and return
      end

      path << "/" << params[:resource] if params[:resource].present?
      path << "/" << params[:method] if params[:method].present?
      if params[:format].present?
        path << ".#{params[:format]}"
      else
        path << ".html"
      end
      cache_file = File.join(Restapi.configuration.cache_dir, path)
      if File.exists?(cache_file)
        content_type = case params[:format]
                       when "json" then "application/json"
                       else "text/html"
                       end
        send_file cache_file, :type => content_type, :disposition => "inline"
      else
        Rails.logger.error("API doc cache not found for '#{path}'. Perhaps you have forgot to run `rake restapi:cache`")
        head :not_found
      end
      return
    end

    Restapi.reload_documentation if Restapi.configuration.reload_controllers?
    @doc = Restapi.to_json(params[:resource], params[:method])

    format.json do
      render :json => @doc
    end

    format.html do

      @doc = @doc[:docs]
      if params[:resource].present? && params[:method].present?
        @resource = @doc[:resources].first
        @method = @resource[:methods].first
        render 'method'
      elsif params[:resource].present?
        @resource = @doc[:resources].first
        render 'resource'
      else
        render 'index'
      end
    end
  end
end