Class: Rubapi::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/rubapi/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/rubapi/controller.rb', line 3

def params
  @params
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/rubapi/controller.rb', line 3

def request
  @request
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/rubapi/controller.rb', line 3

def response
  @response
end

Instance Method Details

#createObject



17
18
19
# File 'lib/rubapi/controller.rb', line 17

def create
  not_defined
end

#deleteObject



25
26
27
# File 'lib/rubapi/controller.rb', line 25

def delete
  not_defined
end

#indexObject



9
10
11
# File 'lib/rubapi/controller.rb', line 9

def index
  not_defined
end

#not_definedObject



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

#run(route) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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.params = self.params
  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(content)
    rescue
      r.content = "{\"errors\":[\"can't generate a json object\"]}"
      r.status = 417 #Execution failed
    ensure
      r.send_response
    end
  end
end

#showObject



13
14
15
# File 'lib/rubapi/controller.rb', line 13

def show
  not_defined
end

#updateObject



21
22
23
# File 'lib/rubapi/controller.rb', line 21

def update
  not_defined
end