Class: JsonCrudApi::API

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/json-crud-api/api.rb

Instance Method Summary collapse

Instance Method Details

#add_error(code, message, reference = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/json-crud-api/api.rb', line 58

def add_error(code, message, reference = nil)
  @errors = [] if @errors.nil?

  error = {
    :code => code,
    :message => message,
  }
  error[:reference] = reference unless reference.nil?
  @errors.push error
end

#fail_forbiddenObject



89
90
91
# File 'lib/json-crud-api/api.rb', line 89

def fail_forbidden
  fail_with_error 403, 'FORBIDDEN','The user is not allowed to perform this operation on the resource.'
end

#fail_not_foundObject



81
82
83
# File 'lib/json-crud-api/api.rb', line 81

def fail_not_found
  fail_with_error 404, 'NOT_FOUND','The resource cannot be found.'
end

#fail_unauthorizedObject



85
86
87
# File 'lib/json-crud-api/api.rb', line 85

def fail_unauthorized
  fail_with_error 401, 'UNAUTHORIZED','Authorization is required to perform this operation on the resource.'
end

#fail_with_error(status, code, message, reference = nil) ⇒ Object



69
70
71
72
# File 'lib/json-crud-api/api.rb', line 69

def fail_with_error(status, code, message, reference = nil)
  add_error code,message,reference
  fail_with_errors status
end

#fail_with_errors(status = 422) ⇒ Object



74
75
76
77
78
79
# File 'lib/json-crud-api/api.rb', line 74

def fail_with_errors(status = 422)
  halt status, JSON.fast_generate({
    :success => false,
    :errors => @errors
  })
end

#logged_in?Boolean



54
55
56
# File 'lib/json-crud-api/api.rb', line 54

def logged_in?
  @logged_in
end