module EasyApiOperations

Constants

VERSION

Public Class Methods

set_up_api(api, ressource_name, operations, options = {}) click to toggle source
# File lib/easy_api_operations.rb, line 7
    def self.set_up_api(api, ressource_name, operations, options = {})
      if options.include?(:db)
        #db option gets through to easy_model_select gem
      else
        options[:db] = :normal
      end
      if options.include?(:primary_key)
        @@primary_key = (defined? ressource_name.constantize.send(options[:primary_key])) == nil ? "id" : ressource_name.constantize.send(options[:primary_key])
      else 
        @@primary_key = (defined? ressource_name.constantize.primary_key == nil) ? "id" : ressource_name.constantize.primary_key
      end
      
      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_version + "::Entities::" + ressource_name.pluralize).constantize#, entity: API::V1::Entities::Appointments
              get do
                if params.empty?
                  present "Choose something dudesadf"
                else
                  results = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params, db: options[:db])).order(@@primary_key => :desc).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
                @@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 #{@@primary_key}", entity: (api_version + "::Entities::" + ressource_name).constantize
              params do
                requires @@primary_key, type: String, desc: "#{ressource_name} #{@@primary_key}"
              end
              get "#{@@primary_key.to_sym}", http_codes: http_codes_hash do
                instance_variable = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params, db: options[:db])).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 #{@@primary_key}", entity: (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize
              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 @@primary_key, http_codes: http_codes_hash.merge(http_codes_hash_extra_creates) do

                instance_variable = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params, db: options[:db])).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 #{@@primary_key}"
      
              params do
                requires @@primary_key, type: String,  desc: "#{ressource_name} #{@@primary_key}"
              end
              delete @@primary_key, http_codes: http_codes_hash do
                instance_variable = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params, db: options[:db])).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_version + "::Entities::" + ressource_name + "CreateBody").constantize#, 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("#{@@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