Module: EasyApiOperations

Defined in:
lib/easy_api_operations.rb,
lib/easy_api_operations/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.set_up_api(api, ressource_name, operations) ⇒ Object



4
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/easy_api_operations.rb', line 4

def self.set_up_api(api, ressource_name, operations)
  operations.downcase
  ressource_name.capitalize

  #ressources are singular and plural
  #methods are primary_key, get, put, delete and post
  #option all is the easiest

  api_version = api.parent_name

  http_codes_hash = {
      200 => 'Ok',
      400 => 'Invalid parameter entry',
      403 => 'Not allowed',
      404 => 'No appointment found'
    }
  http_codes_hash_extra_creates = {500 => 'unable to create/update'}


  #for understanding:
    #(api_version + "::Entities::" + ressource_name.pluralize).constantize ==  API::V1::Entities::Appointments
    #(api_version + "::Entities::" + ressource_name + "UpdateBody").constantize ==  API::V1::Entities::AppointmentUpdateBody
    #(api_version + "::Entities::" + ressource_name + "CreateBody").constantize ==  API::V1::Entities::AppointmentCreateBody
    #ressource_name.constantize == Appointment
    
    
  #___________ressource plural start
    if operations.include?("plural") or operations.include?("all")
      api.resource ressource_name.downcase.pluralize.to_sym do

    #___________get_______________start__________________   
        #!!!description + params that can be requested -> currently the same as the once you get back, this can be adjusted (see other possibilities at the end)
        if operations.include?("plural_get") or operations.include?("all")
          desc "Returns a list of #{ressource_name}. Limited by 1000", params: (api_version + "::Entities::" + ressource_name.pluralize).constantize.documentation#, entity: API::V1::Entities::Appointments
          get do
            if params.empty?
              present "Choose something dude"
            else
              results = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params)).order(ressource_name.constantize.primary_key).limit(1000)
              present results, with: (api_version + "::Entities::" + ressource_name.pluralize).constantize
            end
          end
        else
          #no plural_get
        end 
      end
    else
      #no plural
    end

  #___________ressource singulare start
    if operations.include?("singular") or operations.include?("all")
      api.resource ressource_name.downcase.to_sym do

    #___________get_primary_key_______________start__________________ 
        if operations.include?("singular_primary_key") or operations.include?("all")
          desc "Returns the primary key from #{ressource_name.pluralize}"
          #!!! get then the path addition
          get "primary_key" do
            ressource_name.constantize.primary_key.to_sym
          end
        else
        #no singular_primary_key
        end

    #___________get_______________start__________________       
        if operations.include?("singular_get") or operations.include?("all")   
          desc "Returns #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
          params do
            requires ressource_name.constantize.primary_key, type: Integer, desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
          end
          get "#{ressource_name.constantize.primary_key.to_sym}", http_codes: http_codes_hash do
            instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
            if instance_variable.nil?
              error! http_codes_hash[404], 404
            end
            present instance_variable, with: (api_version + "::Entities::" + ressource_name).constantize
          end
        else
          #no singular_get
        end
    #___________put_______________start__________________
        if operations.include?("singular_put") or operations.include?("all")
          desc "Updates #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
          params do
            #!!!no need to require the primary_key as it is requested within the body already, change the body, change this part
            #requires Appointment.primary_key, type: String,  desc: "Appointment #{Appointment.primary_key}"
            optional :body, type: (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize,  desc: 'Body to update params'
          end
          put ressource_name.constantize.primary_key, http_codes: http_codes_hash.merge(http_codes_hash_extra_creates) do

            instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
            if instance_variable.nil?
              error! http_codes_hash[404], 404
            end
    
            transformed_params = (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize.represent(params, serializable: true)
            transformed_params.keys.each do |key|
              if transformed_params[key].nil?
                transformed_params.delete(key)
              end
            end
  
            if instance_variable.update!(transformed_params)
              status 204
            else
              error! http_codes_hash_extra_creates[500], 500
            end
          end
        else
          #no singular_put
        end

    #___________delete_______________start__________________
        if operations.include?("singular_delete") or operations.include?("all")
          desc "Deletes #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
  
          params do
            requires ressource_name.constantize.primary_key, type: String,  desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
          end
          delete ressource_name.constantize.primary_key, http_codes: http_codes_hash do
            instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
    
            if instance_variable.nil?
              error! http_codes_hash[404], 404
            else
              instance_variable.destroy
              status 204
            end
    
          end
        else
          #no singular pluaral
        end
  
    #___________create_______________start__________________
        if operations.include?("singular_post") or operations.include?("all")
          desc "Creates #{ressource_name.indefinitize}"#, entity: API::V1::Entities::Appointments
          params do
            optional :body, type: (api_version + "::Entities::" + ressource_name + "CreateBody").constantize,  desc: "Body to create #{"Appointment".indefinitize}"
          end
          post http_codes: http_codes_hash.merge(http_codes_hash_extra_creates) do


            transformed_params = (api_version + "::Entities::" + ressource_name + "CreateBody").constantize.represent(params, serializable: true)

            transformed_params.keys.each do |key|
              if transformed_params[key].nil?
                transformed_params.delete(key)
              end
            end

            instance_variable = ressource_name.constantize.create(transformed_params)
            if instance_variable.save
              #status 204
              body instance_variable.send("#{ressource_name.constantize.primary_key}")
            else
              error! http_codes_hash_extra_creates[500], 500
            end
          end
        else
          #no singular_post
        end
      end
    else
      #no singular ressource
    end
  
    #!!!other possibilities for more flexible param / respond structure
  
        # paginate :per_page => 100
   
        #!!!just show the attributes that are allowed by the modul
          #prohibited_attributes = Appointment.get_prohibited_attributes
        #!!! create params out of this
#         params do
#           optional :modified_since, documentation: { type: :string, desc: "Appointments created or modified since a certain date. Format: '2015-04-03T15:53:33.428Z'"}
#           Appointment.get_prohibited_attributes_with_types.each do |column, type|
#             optional column, documentation: { type: type, desc: "Optional Appointment property: #{column}"} # unless type.methods.include? :delimiter
#            # optional column, type: :array, desc: "Optional Appointment property: #{column}" if type.methods.include? :delimiter
#           end
#         end
end