Sha256: 875ea0773a254fb44c6d2484250f1e3c37d647b4c22617bbb7aff5e975b420d6

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

module FellowshipOne

  # This adapter is the standard for all saving objects.
  class ApiWriter
    attr_reader :error_messages, :response_code

    # Saves this object.
    #
    # @return True or ID on success, otherwise false.
    def save_object
      @url_data_params ||= {}
      success = true

      if @url_data_path.nil?
        @error_messages = ["#{@url_action.to_s.upcase} not implemented for #{self.class.to_s}"]
        return false
      end

      if @updatable_fields and !@updatable_fields.empty?
        # fields_to_remove = @url_data_params.keys - @updatable_fields  
        # fields_to_remove.each { |ftr| @url_data_params.delete(ftr) }
      end

      begin
        response = FellowshipOne::api_request(@url_action, @url_data_path, [], @url_data_params.to_json)
        @response_code = response.code
        # No content but is a success
        success = response.code == 204 ? {'success' => true} : JSON.parse(response.body)
      rescue Exception => e  
        @error_messages = e.message.split(',')
        success = false
      end 

      return success
    end


    # Deletes this object.
    #
    # @return True or ID on success, otherwise false.
    def delete_object
      success = true

      if @url_data_delete_path.nil?
        @error_messages = ["DELETE not implemented for #{self.class.to_s}"]
        return false
      end

      begin
        # @url_data_path should be the same as :put if this object is already
        # setup and mapped to an object that exists
        response = FellowshipOne::api_request(:delete, @url_data_delete_path)
        success = response.code == 204 ? true : false # No content but is a success
      rescue Exception => e  
        @error_messages = e.message.split(',')
        success = false
      end 

      return success
    end    

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fellowshipone-api-0.9.0 lib/writers/api_writer.rb
fellowshipone-api-0.8.0 lib/writers/api_writer.rb
fellowshipone-api-0.7.0 lib/writers/api_writer.rb
fellowshipone-api-0.6.4 lib/writers/api_writer.rb
fellowshipone-api-0.6.3 lib/writers/api_writer.rb
fellowshipone-api-0.6.2 lib/writers/api_writer.rb
fellowshipone-api-0.6.1 lib/writers/api_writer.rb
fellowshipone-api-0.6.0 lib/writers/api_writer.rb