4

Hi I am developing a simple api in ruby using intridea's grape. Let's say we have this:

class API_v1 < Grape::API
  resource :foo do
  end

  resource :bar do
  end

end

How could I make it so that the declaration for :foo and :bar are in separate files? Basically, I wanted to know if it is possible to have something similar to rails controllers where there are multiple files to organize the code.

I hope someone can give me an insight on how to achieve this.

lightswitch05
  • 9,058
  • 7
  • 52
  • 75
Lester Celestial
  • 1,454
  • 1
  • 16
  • 26

2 Answers2

8

Ruby has open classes, so you should be able to simply move those to separate files.

# foo.rb
class API_v1 < Grape::API
  resource :foo do
  end
end

# bar.rb
class API_v1 < Grape::API
  resource :bar do
  end
end
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
8

The README recommends you use mount:

class Foo < Grape::API
  resource :foo ... 
end

class Bar < Grape::API
  resource :bar ... 
end

class API < Grape::API
  mount Foo
  mount Bar
end
dB.
  • 4,700
  • 2
  • 46
  • 51