Ruby 将Grape API(非Rails)拆分为不同的文件

Ruby 将Grape API(非Rails)拆分为不同的文件,ruby,api,grape,grape-api,Ruby,Api,Grape,Grape Api,我正在用Grape编写API,但它是独立的,没有Rails、Sinatra或其他任何东西。我想将app.rb文件拆分为单独的文件。我已经看过了,但那是关于Rails的 我不知道如何使它与模块或类一起工作——我确实尝试过将不同的文件子类化到我的大GrapeApp,但这很难看,我甚至不确定它是否正常工作。最好的方法是什么 我目前有按文件夹划分的版本(v1,v2,等等),但仅此而已。您不需要从主应用程序中创建子类,只需在主应用程序中装入单独的Grape::API子类即可。当然,您可以在单独的文件中定义

我正在用Grape编写API,但它是独立的,没有Rails、Sinatra或其他任何东西。我想将
app.rb
文件拆分为单独的文件。我已经看过了,但那是关于Rails的

我不知道如何使它与模块或类一起工作——我确实尝试过将不同的文件子类化到我的大
GrapeApp
,但这很难看,我甚至不确定它是否正常工作。最好的方法是什么


我目前有按文件夹划分的版本(
v1
v2
,等等),但仅此而已。

您不需要从主应用程序中创建子类,只需在主应用程序中装入单独的Grape::API子类即可。当然,您可以在单独的文件中定义这些类,并使用
require
加载应用程序可能需要的所有路由、实体和帮助程序。我发现为每个“域对象”创建一个迷你应用程序很有用,并将它们加载到
app.rb
,如下所示:

  # I put the big list of requires in another file . . 
  require 'base_requires'

  class MyApp < Grape::API
    prefix      'api'
    version     'v2'
    format      :json

    # Helpers are modules which can have their own files of course
    helpers APIAuthorisation

    # Each of these routes deals with a particular sort of API object
    group( :foo ) { mount APIRoutes::Foo }
    group( :bar ) { mount APIRoutes::Bar }
  end
module APIRoutes
  class Foo < Grape::API
    helpers APIFooHelpers

    get :all do
      present get_all_users_foos, :with => APIEntity::Foo
    end

    group "id/:id" do
      before do
        @foo = Model::Foo.first( :id => params[:id] )
        error_if_cannot_access! @foo
      end

      get do
        present @foo, :with => APIEntity::Foo, :type => :full
      end

      put do
        update_foo( @foo, params )
        present @foo, :with => APIEntity::Foo, :type => :full
      end

      delete do
        delete_foo @foo
        true
      end
    end # group "id/:id"
  end # class Foo
end # module APIRoutes
我可能会模拟Rails,并有一个
/models/
文件夹或类似的文件夹来保存ActiveRecord或DataMapper定义,但在我当前的项目中,它以不同的模式提供给我

我的大多数路由看起来都非常基本,它们只调用相关的helper方法,然后基于它呈现一个实体。例如,
/routes/foo.rb
可能如下所示:

  # I put the big list of requires in another file . . 
  require 'base_requires'

  class MyApp < Grape::API
    prefix      'api'
    version     'v2'
    format      :json

    # Helpers are modules which can have their own files of course
    helpers APIAuthorisation

    # Each of these routes deals with a particular sort of API object
    group( :foo ) { mount APIRoutes::Foo }
    group( :bar ) { mount APIRoutes::Bar }
  end
module APIRoutes
  class Foo < Grape::API
    helpers APIFooHelpers

    get :all do
      present get_all_users_foos, :with => APIEntity::Foo
    end

    group "id/:id" do
      before do
        @foo = Model::Foo.first( :id => params[:id] )
        error_if_cannot_access! @foo
      end

      get do
        present @foo, :with => APIEntity::Foo, :type => :full
      end

      put do
        update_foo( @foo, params )
        present @foo, :with => APIEntity::Foo, :type => :full
      end

      delete do
        delete_foo @foo
        true
      end
    end # group "id/:id"
  end # class Foo
end # module APIRoutes
模块路径
类FooAPIEntity::Foo
结束
组“id/:id”do
在做之前
@foo=Model::foo.first(:id=>params[:id])
如果无法访问,则出现错误@福
结束
动手
呈现@foo,:with=>apienty::foo,:type=>:full
结束
付诸行动
更新_foo(@foo,params)
呈现@foo,:with=>apienty::foo,:type=>:full
结束
删除do
删除_foo@foo
真的
结束
结束#组“id/:id”
结束#Foo级
结束#模块APIRoutes

routesfile
require
grape?@tekknolgi:在定义
grape::API
子类之前,您应该
require'grape'
,但是如果你不想在任何地方重复的话,那可能会出现在主
app.rb
中。好吧,它不会因为未定义的变量、类和其他东西而出错吗?@tekknolgi:不应该,但我无法预测你需要做的一切。如果你最终的结构由于某种原因无法正常工作,那么也许值得再问一个这样的问题。大多数需求的问题只是把它们按正确的顺序排列。好的,听起来不错。我要试一试!你对我的红宝石问题无处不在-谢谢!