Sha256: 15a5ac4fc5a1d5d241ee054be7d1274814a25cceabeabd141da88d0404d8504a

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'active_model'
require 'json'

module ECMBlockchain
  class AssetModel
    include ActiveModel::Validations

    attr_accessor :uuid, :txId, :groupId, :title, :summary, :createdBy, :owner,
                  :file, :content, :created_at, :access, :lastInteraction, :events

    validates :uuid, :title, presence: true
    validate :data_file_or_content?
 
    class << self
      def verify(data={})
        asset = self.new(data)
        raise ECMBlockchain::Error.raise_error(
          OpenStruct.new(
            code: 422, 
            body: { 
              error: { 
                message: asset.errors.full_messages.first,
                details: ''
              }
            }.to_json 
          )
        ) unless asset.valid?
        asset
      end
    end

    def initialize(data={})
      @uuid = data.fetch(:uuid) { nil }
      @txId = data.fetch(:txId) { nil }
      @groupId = data.fetch(:groupId) { nil }
      @title = data.fetch(:title) { nil }
      @summary = data.fetch(:summary) { nil }
      @createdBy = data.fetch(:createdBy) { nil }
      @owner = data.fetch(:owner) { nil }
      @file = ECMBlockchain::DataFile.new(data.fetch(:file) { })
      @content = ECMBlockchain::DataContent.new(data.fetch(:content) { })
    end

    def success?
      true
    end

    def error
      nil
    end

    private

    def data_file_or_content?
      unless file.valid? || content.valid?
        errors.add(:base, 
          "file or content error: #{file.errors.full_messages.first || content.errors.full_messages.first ||
          "Please supply either a file object or content object"}"
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecm-blockchain-api-1.1.2 lib/ecm-blockchain-api/models/asset_model.rb
ecm-blockchain-api-1.1.1 lib/ecm-blockchain-api/models/asset_model.rb