Sha256: fed46e597a8e64cc02059c6a06bc990ff9651608a5901f7417baee8188917e62

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

module LiveQA
  class APIOperation
    ##
    # == Save (create or update) a resource for the API
    #
    module Save
      module ClassMethods
        ##
        # Create an API Resource and validate the params for the API.
        #
        # @param [Hash] params for the request
        # @param [Hash] Additional options for the request
        #
        # @return [LiveQA::Object] response from the API
        def create(params = {}, options = {})
          response = request(:post, resource_path, params, options)
          initialize_from(response)
        end

        ##
        # Update an API Resource and validate params for the API
        #
        # @param [String] id for the request
        # @param [Hash] params for the request
        # @param [Hash] Additional options for the request
        #
        # @return [LiveQA::Object] response from the API
        def update(id, params = {}, options = {})
          response = request(:put, "#{resource_path}/#{id}", params, options)
          initialize_from(response)
        end

      end

      ##
      # Create or Update an API Resource
      #
      # @param [Hash] params for the request
      # @param [Hash] Additional options for the request
      #
      # @return [LiveQA::Object] response from the API
      def save(params = {}, options = {})
        path   = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path
        method = singleton_methods.include?(:id) ? :put : :post

        response = request(method, path, to_hash.merge(params), options)
        update_from(response)
      end
      alias update save

      def self.included(base)
        base.extend(ClassMethods)
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
liveqa-1.9.6 lib/liveqa/api_operation/save.rb
liveqa-1.9.5 lib/liveqa/api_operation/save.rb
liveqa-1.9.4 lib/liveqa/api_operation/save.rb
liveqa-1.9.3 lib/liveqa/api_operation/save.rb
liveqa-1.9.2 lib/liveqa/api_operation/save.rb
liveqa-1.9.1 lib/liveqa/api_operation/save.rb
liveqa-1.9.0 lib/liveqa/api_operation/save.rb
liveqa-1.8.3 lib/liveqa/api_operation/save.rb
liveqa-1.4.6 lib/liveqa/api_operation/save.rb