Module: Kishu::Utils

Includes:
Bolognese::MetadataUtils
Included in:
CLI, Log, Report, ResolutionEvent, Sushi, LagottoJob
Defined in:
lib/kishu/utils.rb

Instance Method Summary collapse

Instance Method Details

#checksumObject



110
111
112
# File 'lib/kishu/utils.rb', line 110

def checksum
  Digest::SHA256.hexdigest(compress_merged_file)
end

#clean_tmpObject



8
9
10
11
# File 'lib/kishu/utils.rb', line 8

def clean_tmp
  system("rm tmp/datasets-*.json")
  puts "/tmp Files deleted"
end

#encodedObject



106
107
108
# File 'lib/kishu/utils.rb', line 106

def encoded
  Base64.strict_encode64(compress_merged_file)
end

#encoded_fileObject



17
18
19
# File 'lib/kishu/utils.rb', line 17

def encoded_file
  "reports/datacite_resolution_report_#{report_period.strftime("%Y-%m")}_encoded.json"
end

#format_instance(data, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kishu/utils.rb', line 47

def format_instance  data, options={}
  obj = (options[:dataset_id])
  subj = {id:options[:report_id]}
  # subj = "https://api.datacite.org/reports/0cb326d1-e3e7-4cc1-9d86-7c5f3d5ca310"
  relation_type = "#{data[:"metric-type"]}-#{data[:"access-method"]}"
  source_id = "datacite-resolution"
  source_token = SOURCE_TOKEN
  { 
    "data" => {
      "type" => "events",
      "attributes" => {
        "message-action" => "create",
        "subj-id" => options[:report_id],
        "total" => data[:count],
        "obj-id" => options[:dataset_id],
        "relation-type-id" => relation_type.to_s.dasherize,
        "source-id" => source_id,
        "source-token" => source_token,
        "occurred-at" => Time.now.iso8601, # need modify
        "timestamp" => Time.now.iso8601,
        "license" => LICENSE,
        "subj" => subj,
        "obj" => obj } }}
end


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kishu/utils.rb', line 21

def generate_header_footer
  report_header = '{"report-header": '+get_header.to_json.to_s+',"report-datasets": [ '+"\n"
  
  File.open("tmp/datasets-00-report-header.json","w") do |f|
    f.write(report_header)
  end
  report_footer = ']'+"\n"+'}'
  
  File.open("tmp/datasets-zz99-report-footer.json","w") do |f|
    f.write(report_footer)
  end
end

#get_authors(author) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kishu/utils.rb', line 34

def get_authors author
  if (author.key?("given") && author.key?("family"))
    { type: "name",
      value: author.fetch("given",nil)+" "+author.fetch("family",nil) }
    elsif author.key?("literal")
      { type: "name",
        value: author.fetch("literal",nil) }
    else 
      { type: "name",
        value: "" }
  end
end

#get_metadata(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kishu/utils.rb', line 72

def  id
  doi = doi_from_url(id)
  return {} unless doi.present?

  url = API_URL + "/dois/#{doi}"
  response = Maremma.get(url)
  return {} if response.status != 200
  
  attributes = response.body.dig("data", "attributes")
  relationships = response.body.dig("data", "relationships")
  
  resource_type = response.body.dig("data", "relationships")
  resource_type_general = relationships.dig("resource-type", "data", "id")
  type = Bolognese::Utils::CR_TO_SO_TRANSLATIONS[resource_type.to_s.underscore.camelcase] || Bolognese::Utils::DC_TO_SO_TRANSLATIONS[resource_type_general.to_s.underscore.camelcase(first_letter = :upper)] || "CreativeWork"
  author = Array.wrap(attributes["author"]).map do |a| 
    {
      "given_name" => a["givenName"],
      "family_name" => a["familyName"],
      "name" => a["familyName"].present? ? nil : a["name"] }.compact
  end
  client_id = relationships.dig("client", "data", "id")
  
  {
    "id" => id,
    "type" => type.underscore.dasherize,
    "name" => attributes["title"],
    "author" => author,
    "publisher" => attributes["publisher"],
    "version" => attributes["version"],
    "date_published" => attributes["published"],
    "date_modified" => attributes["updated"],
    "registrant_id" => "datacite.#{client_id}" }.compact
end

#merged_fileObject



13
14
15
# File 'lib/kishu/utils.rb', line 13

def merged_file
  "reports/datacite_resolution_report_#{report_period.strftime("%Y-%m")}_2.json"
end