Skip to content

Instantly share code, notes, and snippets.

@timurvafin
Last active December 14, 2015 11:50
Show Gist options
  • Save timurvafin/5082251 to your computer and use it in GitHub Desktop.
Save timurvafin/5082251 to your computer and use it in GitHub Desktop.
Allow to store api_taster definitions in the app/api_tasters separated by files.
# app/api_tasters/posts.rb
ApiTaster::RouteCollector.route do
get '/posts'
end
# app/api_tasters/comments.rb
ApiTaster::RouteCollector.route do
get '/comments'
end
# config/routes.rb
GrasshopperCore::Application.routes.draw do
resources :posts
resources :comments
if defined? ApiTaster::Engine
mount ApiTaster::Engine => '/api_taster'
end
end
ApiTaster::RouteCollector.collect!
# lib/extensions/api_taster/route_collector.rb
module ApiTaster
class RouteCollector
cattr_accessor :routes
self.routes = []
class << self
def route(&block)
self.routes << block
end
def collect!(path = "#{Rails.root}/app/api_tasters")
self.routes = []
Dir["#{path}/**/*.rb"].each { |file| load(file) }
ApiTaster.routes do
for route in RouteCollector.routes
begin
instance_eval(&route)
rescue Exception => e
Rails.logger.error ['ApiTaster::RouteCollector:', e, e.backtrace].flatten!
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment