class Rubapi::Controller

Attributes

params[RW]
request[RW]
response[RW]

Public Class Methods

new(response) click to toggle source
# File lib/rubapi/controller.rb, line 5
def initialize(response)
  self.response = response
end

Public Instance Methods

attach_data(data) click to toggle source
# File lib/rubapi/controller.rb, line 88
def attach_data(data)
  @content ||= { errors: [] }
  @content[:data] = data;

  @content
end
create() click to toggle source
# File lib/rubapi/controller.rb, line 17
def create
  not_defined
end
delete() click to toggle source
# File lib/rubapi/controller.rb, line 25
def delete
  not_defined
end
format_response(response) click to toggle source

If you try to respond with a reql.expr, it ensures that will return with a Hash(Dict) format

# 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() click to toggle source
# File lib/rubapi/controller.rb, line 9
def index
  not_defined
end
not_defined() click to toggle source
# 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) { |to_a| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/rubapi/controller.rb, line 13
def show
  not_defined
end
update() click to toggle source
# File lib/rubapi/controller.rb, line 21
def update
  not_defined
end