Class: Dbtools::Ckan

Inherits:
Thor
  • Object
show all
Defined in:
lib/tasks/ckan.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ckan

Returns a new instance of Ckan.



20
21
22
23
24
25
26
27
# File 'lib/tasks/ckan.rb', line 20

def initialize(*args)
  super
  load_config
  @gdrive = Dbtools::Google_Drive::Google_drive_api.new
  @service = @gdrive.service
  @rdf_graph = @gdrive.rdf_graph
  @import = Import.new
end

Instance Method Details

#list_databasesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tasks/ckan.rb', line 70

def list_databases
  databases = @rdf_graph.get_available_databases
  databases.each do |index, res|
    puts "#{index}. #{res['dataset_title']}"
  end
  selection = ask("Which data set do you want to load? ").to_i
  unless databases.key?(selection)
    puts 'Data set not found. '
    return
  end
  load_dataset(databases[selection]['dataset'])
  return databases
end

#load_dataset(dataset) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/tasks/ckan.rb', line 30

def load_dataset(dataset)
   = @rdf_graph.(dataset)
  raise "Dataset not found. " if .empty?
  .values.each do ||
    load_dataset_resource()
  end
end

#load_rdf_in_desc(target_database, dataset) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tasks/ckan.rb', line 85

def load_rdf_in_desc(target_database, dataset)
  begin
    # Open the rdf of the file.
    description = open("#{dataset}.ttl").read 

    # Put the rdf in the comments 
    psql = Dbtools::Database::PostgresqlConnection.new(target_database)
    psql.set_description_database(description)
    psql.close
  rescue
    puts "Could not open rdf from dataset: #{dataset}"
  end

end

#load_resource(dataset, resource) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/tasks/ckan.rb', line 39

def load_resource(dataset, resource) 
   = @rdf_graph.(dataset)
  puts .inspect
   = .select { |k, v| v['resource'].to_s.include?(resource) }

  raise "Resource not found." if .empty?
  raise "Multiple resources found." if .length > 1

  load_dataset_resource(.values.first)
end