Class: Rubapi::Controller
- Inherits:
-
Object
- Object
- Rubapi::Controller
- Defined in:
- lib/rubapi/controller.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #attach_data(data) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
-
#format_response(response) ⇒ Object
If you try to respond with a reql.expr, it ensures that will return with a Hash(Dict) format.
- #index ⇒ Object
-
#initialize(response) ⇒ Controller
constructor
A new instance of Controller.
- #not_defined ⇒ Object
- #process_defer(reql_expression) ⇒ Object
- #run(route) ⇒ Object
- #send_response(content) ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(response) ⇒ Controller
Returns a new instance of Controller.
5 6 7 |
# File 'lib/rubapi/controller.rb', line 5 def initialize(response) self.response = response end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/rubapi/controller.rb', line 3 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
3 4 5 |
# File 'lib/rubapi/controller.rb', line 3 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
3 4 5 |
# File 'lib/rubapi/controller.rb', line 3 def response @response end |
Instance Method Details
#attach_data(data) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/rubapi/controller.rb', line 88 def attach_data(data) @content ||= { errors: [] } @content[:data] = data; @content end |
#create ⇒ Object
17 18 19 |
# File 'lib/rubapi/controller.rb', line 17 def create not_defined end |
#delete ⇒ Object
25 26 27 |
# File 'lib/rubapi/controller.rb', line 25 def delete not_defined end |
#format_response(response) ⇒ Object
If you try to respond with a reql.expr, it ensures that will return with a Hash(Dict) format
82 83 84 85 86 |
# File 'lib/rubapi/controller.rb', line 82 def format_response(response) response.map {|re| { :"#{re.first}" => re.last } }.inject({}) {|sum, re| sum.merge!(re)} end |
#index ⇒ Object
9 10 11 |
# File 'lib/rubapi/controller.rb', line 9 def index not_defined end |
#not_defined ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/rubapi/controller.rb', line 29 def not_defined self.response.tap do |r| r.status = 417 r.content = "{\"errors\":[\"not defined\"]}" r.send_response end end |
#process_defer(reql_expression) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubapi/controller.rb', line 67 def process_defer(reql_expression) get_conn = proc{ ReconnectionPool.get } callback = proc{|conn| reql_expression.run(conn) do |row| yield(row.to_a) if block_given? end ReconnectionPool.return conn send_response(nil) unless block_given? } EM.defer get_conn, callback end |
#run(route) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubapi/controller.rb', line 52 def run(route) if route.nil? response.status = 404 response.send_response return response end route[:controller] ||= self.class controller = route[:controller].new(self.response) controller.request = self.request controller.request.pagination = route[:pagination] unless route[:pagination].nil? controller.send(route[:method]) end |
#send_response(content) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rubapi/controller.rb', line 37 def send_response(content) self.response.tap do |r| r.status = 200 begin r.content = JSON.generate(attach_data(content)) rescue r.content = "{\"errors\":[\"can't generate a json object\"]}" r.status = 417 #Execution failed ensure r.send_response end end end |
#show ⇒ Object
13 14 15 |
# File 'lib/rubapi/controller.rb', line 13 def show not_defined end |
#update ⇒ Object
21 22 23 |
# File 'lib/rubapi/controller.rb', line 21 def update not_defined end |