Class: Dbtools::Converter::GoogleDrive2RDFConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/dbtools/converter/google_drive2_rdf_converter.rb

Instance Method Summary collapse

Constructor Details

#initializeGoogleDrive2RDFConverter

Returns a new instance of GoogleDrive2RDFConverter.



7
8
9
# File 'lib/dbtools/converter/google_drive2_rdf_converter.rb', line 7

def initialize
  Spira.repository = RDF::Repository.new
end

Instance Method Details

#drivefile2rdf(google_drive_file) ⇒ String

Converts a google drive file instance to RDF statements

Parameters:

  • google_drive_file (Google::Apis::DriveV3::File)

    Google drive file that will be converted.

Returns:

  • (String)

    RDF ntriples in string format.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dbtools/converter/google_drive2_rdf_converter.rb', line 16

def drivefile2rdf(google_drive_file)
  file = google_drive_file
  result = []
  Spira.repository ||= RDF::Repository.new

  if file.mime_type == 'application/vnd.google-apps.folder'
    # Google Folder specific attributes
    uri = RDF::URI.new("https://drive.google.com/drive/folders/#{file.id}")
    drive_file = uri.as(Dbtools::Google_Drive::GoogleDriveFolder)
  else
    # Google File specific attributes
    uri = RDF::URI.new(Dbtools::Google_Drive::Google_drive_api.get_url_from_id(file.id))
    drive_file = uri.as(Dbtools::Google_Drive::GoogleDriveFile)
    drive_file.file_extension = file.file_extension
    drive_file.web_content_link = file.web_content_link
  end
  # Shared attributes
  drive_file.name = file.name
  drive_file.identifier = file.id
  drive_file.created_time = file.created_time
  drive_file.mime_type = file.mime_type
  drive_file.size = file.size
  drive_file.modified_time = file.modified_time
  drive_file.icon_link = file.icon_link
  drive_file.description = file.description
  drive_file.web_view_link = file.web_view_link
  drive_file.trashed = file.trashed

  # Assign all key-value pairs from properties attribute to the Spira resource.
  # Could probably be done for all attributes above..
  # Untested so commented to prevent errors
  # file.properties.each do |key, value|
  #   m = "#{key}="
  #   drive_file.send(m, value) if drive_file.respond_to?(m)
  # end if file.properties

  # Add bi-directional relation for parents-children.
  drive_file.parents = file.parents.map do |parent_id|
    parent_uri = RDF::URI.new("https://drive.google.com/drive/folders/#{parent_id}")
    parent_drive_folder = parent_uri.as(Dbtools::Google_Drive::GoogleDriveFolder)
    parent_drive_folder.children << drive_file
    result << parent_drive_folder
    parent_drive_folder
  end if file.parents

  result << drive_file
  return result
end

#serialize_as_rdf(files, verbose: true) ⇒ Object

Serializes a list of files to RDF statements. Yields rdf ntriples for every file.

Parameters:

  • files

    List of files to be converted. Default is all files.

  • verbose (defaults to: true)

    Prints progress if true



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dbtools/converter/google_drive2_rdf_converter.rb', line 71

def serialize_as_rdf(files, verbose: true)
  if verbose
    total = files.size
    count = 0
  end
  files.each do |file|
    if verbose
      count += 1
      STDERR.puts("Converting file to rdf: #{count}/#{total}\t\r")
    end
    # get tree method returns a hash with [id, file].
    file = file[1] if files.is_a?(Hash)
    yield drivefile2rdf(file).map(&:to_ntriples).join("\n")
  end
end