Module: FReCon::Routes

Included in:
Server
Defined in:
lib/frecon/routes.rb

Overview

Public: A module containing all of the routes.

Class Method Summary collapse

Class Method Details

.attribute_routes(base, name, controller) ⇒ Object

Public: Set up basic attribute route handlers.

base - Sinatra::Application to register the routes under. name - String containing the model name. controller - Controller-like object.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/frecon/routes.rb', line 68

def self.attribute_routes(base, name, controller)
	model = controller.model

	model_attribute_methods = model.class_variable_get(:@@attributes)

	model_attribute_methods.each do |model_attribute_method|
		base.get "/#{name}/:id/#{model_attribute_method[:attribute]}" do
			begin
				@model = controller.find_model(params)

				params.delete('id')

				result = @model.method(model_attribute_method[:method]).call

				if result.is_a? Mongoid::Criteria
					params.delete('splat')
					params.delete('captures')

					result.psv_filter(params).to_json
				else
					result.to_json
				end
			rescue RequestError => e
				e.return_value
			end
		end
	end
end

.included(base) ⇒ Object

Public: Bootstrap the routes into inclusors of this module.

base - The child that included this module (should be a Sinatra App)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/frecon/routes.rb', line 100

def self.included(base)
	resource_routes base, 'teams', TeamsController
	resource_routes base, 'competitions', CompetitionsController
	resource_routes base, 'matches', MatchesController
	resource_routes base, 'records', RecordsController
	resource_routes base, 'robots', RobotsController
	resource_routes base, 'participations', ParticipationsController

	attribute_routes base, 'teams', TeamsController
	attribute_routes base, 'competitions', CompetitionsController
	attribute_routes base, 'matches', MatchesController
	attribute_routes base, 'records', RecordsController
	attribute_routes base, 'robots', RobotsController
	attribute_routes base, 'participations', ParticipationsController

	base.before do
		params.delete('_')
	end

	base.get '/dump' do
		begin
			DumpController.full params
		rescue RequestError => e
			e.return_value
		end
	end

	if ENV['PRINT_ROUTES']
		base.routes.each do |verb, routes|
			puts "#{verb}:"

			routes.each do |route|
				puts "  #{route[0]}"
			end
		end
	end
end

.resource_routes(base, name, controller) ⇒ Object

Public: Set up basic resource route handlers.

base - Sinatra::Application to register the routes under. name - String containing the model name. controller - Controller-like object that contains key methods.



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
58
59
60
61
# File 'lib/frecon/routes.rb', line 21

def self.resource_routes(base, name, controller)
	base.post "/#{name}" do
		begin
			controller.create request, params
		rescue RequestError => e
			e.return_value
		end
	end

	base.put "/#{name}/:id" do
		begin
			controller.update request, params
		rescue RequestError => e
			e.return_value
		end
	end

	base.delete "/#{name}/:id" do
		begin
			controller.delete params
		rescue RequestError => e
			e.return_value
		end
	end

	base.get "/#{name}/:id" do
		begin
			controller.show params
		rescue RequestError => e
			e.return_value
		end
	end

	base.get "/#{name}" do
		begin
			controller.index params
		rescue RequestError => e
			e.return_value
		end
	end
end