Sha256: 2ccfe25708cb39de9d2af66262a788a3fac77cd8da24698d26ce19287b9339d5

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'dmao/api/entity'
require 'dmao/api/errors/invalid_dataset_file'
require 'dmao/api/errors/invalid_dataset_file_id'
require 'dmao/api/errors/dataset_file_not_found'
require 'dmao/api/errors/invalid_dataset_id'

module DMAO
  module API

    class DatasetFile < Entity

      ENDPOINT = 'dataset_files'
      NOT_FOUND_ERROR = DMAO::API::Errors::DatasetFileNotFound
      INVALID_ID_ERROR = DMAO::API::Errors::InvalidDatasetFileID
      INVALID_ENTITY_CLASS = DMAO::API::Errors::InvalidDatasetFile
      INVALID_ENTITY_ERROR_MESSAGE = "Invalid dataset file details, please see errors."

      VALID_ATTRIBUTES = [:id, :institution_id, :name, :size, :url, :system_uuid, :system_modified_at, :dataset_id]

      attr_reader(*VALID_ATTRIBUTES)

      def initialize(attributes)

        @id = attributes[:id]
        @institution_id = attributes[:institution_id]
        @name = attributes[:name]
        @size = attributes[:size]
        @url = attributes[:url]
        @system_uuid = attributes[:system_uuid]
        @system_modified_at = attributes[:system_modified_at]
        @dataset_id= attributes[:dataset_id]

      end

      def self.instance_from_api_data data

        attributes = {
            id: data["id"],
            institution_id: data["relationships"]["institution"]["data"]["id"],
            name: data["attributes"]["name"],
            size: data["attributes"]["size"],
            url: data["attributes"]["url"],
            system_uuid: data["attributes"]["system-uuid"],
            system_modified_at: data["attributes"]["system-modified-at"],
            dataset_id: data["relationships"]["dataset"]["data"]["id"]
        }

        new(attributes)

      end

      def self.handle_unprocessable_entity error_response

        errors = parse_error_response error_response

        raise_error_if_key DMAO::API::Errors::InvalidDatasetID, errors, "dataset"

        raise DMAO::API::Errors::InvalidDatasetFile.new("Invalid dataset file details, please see errors.", errors)

      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dmao_api-0.6.0 lib/dmao/api/dataset_file.rb
dmao_api-0.5.0 lib/dmao/api/dataset_file.rb
dmao_api-0.4.0 lib/dmao/api/dataset_file.rb