Module: CrudService::Api

Defined in:
lib/crud-service/api.rb

Overview

This mixin provides a static methods to configure a sinatra class with the provided resource name, service and api_options

Instance Method Summary collapse

Instance Method Details

#crud_api(resource_name, service_name, primary_key_name, api_options = {}) ⇒ Object

Set up full CRUD API functionality on the given resource



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/crud-service/api.rb', line 6

def crud_api(resource_name, service_name, primary_key_name, api_options = {})
  api_options = get_defaults(api_options)

  crud_options(resource_name, api_options) if api_options[:enable_options]

  if api_options[:enable_write]
    crud_post(resource_name, service_name, primary_key_name, api_options) if api_options[:enable_post]
    crud_put(resource_name, service_name, primary_key_name, api_options)  if api_options[:enable_put]
    crud_delete(resource_name, service_name, primary_key_name, api_options) if api_options[:enable_delete]
  end

  if api_options[:enable_read]
    crud_get(resource_name, service_name, primary_key_name, api_options) if api_options[:enable_get]
    crud_get_all(resource_name, service_name, api_options) if api_options[:enable_get_all]
  end
end

#crud_delete(resource_name, service_name, primary_key_name, api_options = {}) ⇒ Object

Set up DELETE functionality on the given resource



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crud-service/api.rb', line 92

def crud_delete(resource_name, service_name, primary_key_name, api_options = {})
  api_options = get_defaults(api_options)
  delete '/'+resource_name+'/:'+primary_key_name do
    service = settings.send(service_name)

    # Must Exist
    return 404 unless service.exists_by_primary_key?(params[primary_key_name.to_sym])

    # Do Delete
    return 500 unless service.delete_by_primary_key(params[primary_key_name.to_sym])

    204
  end
end

#crud_get(resource_name, service_name, primary_key_name, api_options = {}) ⇒ Object

Set up GET/get_one functionality on the given resource



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/crud-service/api.rb', line 62

def crud_get(resource_name, service_name, primary_key_name, api_options = {})
  api_options = get_defaults(api_options)
  get '/'+resource_name+'/:'+primary_key_name do
    service = settings.send(service_name)

    sanitize_params(params)
    return 400 unless service.valid_query?(params)

    record = service.get_one_by_query(params)
    return 404 if record.nil?
    JSON.fast_generate record
  end
end

#crud_get_all(resource_name, service_name, api_options = {}) ⇒ Object

Set up GET/get_all functionality on the given resource



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/crud-service/api.rb', line 77

def crud_get_all(resource_name, service_name, api_options = {})
  api_options = get_defaults(api_options)
  get '/'+resource_name do
    service = settings.send(service_name)

    sanitize_params(params)
    # Check query validity
    return 400 unless service.valid_query?(params)

    # Return Regions on Query
    JSON.fast_generate service.get_all_by_query(params)
  end
end

#crud_options(resource_name, api_options = {}) ⇒ Object

Set up OPTIONS functionality on the given resource



24
25
26
27
28
29
# File 'lib/crud-service/api.rb', line 24

def crud_options(resource_name, api_options = {})
  api_options = get_defaults(api_options)
  options '/'+resource_name do
    204
  end
end

#crud_post(resource_name, service_name, primary_key_name, api_options = {}) ⇒ Object

Set up POST/insert functionality on the given resource



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
# File 'lib/crud-service/api.rb', line 108

def crud_post(resource_name, service_name, primary_key_name, api_options = {})
  api_options = get_defaults(api_options)
  post '/'+resource_name do
    service = settings.send(service_name)

    # Get The data
    begin
      data = JSON.parse(request.body.read)
    rescue Exception => e
      return 422
    end

    # Valid POST?
    return 422 unless service.valid_insert?(data)

    # Already Exists?
    return 409 if service.exists_by_primary_key?(data[primary_key_name])

    # Do Insert
    record = service.insert(data)

    # Other Error
    return 500 if record.nil?

    # Output new record
    JSON.fast_generate record
  end
end

#crud_put(resource_name, service_name, primary_key_name, api_options = {}) ⇒ Object

Set up UPDATE/put functionality on the given resource



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
# File 'lib/crud-service/api.rb', line 32

def crud_put(resource_name, service_name, primary_key_name, api_options = {})
  api_options = get_defaults(api_options)
  put '/'+resource_name+'/:'+primary_key_name do
    service = settings.send(service_name)

    # Must Exist
    return 404 unless service.exists_by_primary_key?(params[primary_key_name.to_sym])

    # Get The Data
    begin
      data = JSON.parse(request.body.read)
    rescue Exception => e
      return 422
    end

    # Valid Update?
    return 422 unless service.valid_update?(data)

    # Do Update
    record = service.update_by_primary_key(params[primary_key_name.to_sym],data)

    # Other Error
    return 500 if record.nil?

    # Return new Region
    JSON.fast_generate record
  end
end

#get_defaults(api_options) ⇒ Object

Return the given options with defaults set



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/crud-service/api.rb', line 138

def get_defaults(api_options)
  defaults = {
    :enable_read => true,
    :enable_write => true,
    :enable_options => true,
    :enable_get_all => true,
    :enable_get => true,
    :enable_post => true,
    :enable_put => true,
    :enable_delete => true,
  }
  api_options.merge!(defaults) { |key, v1, v2| v1 }
end