Module: CrudService::Api

Defined in:
lib/api.rb

Instance Method Summary collapse

Instance Method Details

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

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/api.rb', line 5

def crud_api(resource_name, service_name, primary_key_name, 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 }

  if api_options[:enable_options]
    options '/'+resource_name do
      204
    end
  end

  if api_options[:enable_write]

    if api_options[:enable_post]
      post '/'+resource_name do
        service = settings.send(service_name)

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

        # Valid POST?
        return 400 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 == false

        # Output new record
        JSON.fast_generate record
      end
    end

    if api_options[:enable_put]
      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 400
        end

        # Valid Update?
        return 400 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

    if api_options[:enable_delete]
      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 400 unless service.delete_by_primary_key(params[primary_key_name.to_sym])

        204
      end
    end

  end

  if api_options[:enable_read]

    if api_options[:enable_get]
      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

    if api_options[:enable_get_all]
      get '/'+resource_name+'/:'+primary_key_name do
        service = self.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
  end
end