Module: Commonmeta::Readers::JsonFeedReader

Included in:
CLI, MetadataUtils
Defined in:
lib/commonmeta/readers/json_feed_reader.rb

Instance Method Summary collapse

Instance Method Details

#get_json_feed_item(id: nil, **_options) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 7

def get_json_feed_item(id: nil, **_options)
  return { "string" => nil, "state" => "not_found" } unless id.present?

  url = normalize_id(id)
  response = HTTP.get(url)
  return { "string" => nil, "state" => "not_found" } unless response.status.success?

  { "string" => response.body.to_s }
end

#get_json_feed_unregistered ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 91

def get_json_feed_unregistered
  # get JSON Feed items not registered as DOIs

  url = json_feed_unregistered_url
  response = HTTP.get(url)
  return { "string" => nil, "state" => "not_found" } unless response.status.success?

  posts = JSON.parse(response.body.to_s)
  posts.map { |post| post["uuid"] }.first
end

#get_json_feed_updated ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 102

def get_json_feed_updated
  # get JSON Feed items updated since last check

  url = json_feed_unregistered_url
  response = HTTP.get(url)
  return { "string" => nil, "state" => "not_found" } unless response.status.success?

  posts = JSON.parse(response.body.to_s)
  posts.map { |post| post["uuid"] }.first
end

#get_references(meta) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 82

def get_references(meta)
  # check that references resolve
  Array.wrap(meta["references"]).reduce([]) do |sum, reference|
    sum << reference if [200, 301, 302].include? HTTP.head(reference["doi"] || reference["url"]).status

    sum
  end
end

#read_json_feed_item(string: nil, **options) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 17

def read_json_feed_item(string: nil, **options)
  read_options = ActiveSupport::HashWithIndifferentAccess.new(options.except(:doi, :id, :url,
                                                                             :sandbox, :validate, :ra))

  meta = string.present? ? JSON.parse(string) : {}

  id = options[:doi] ? normalize_doi(options[:doi]) : normalize_id(meta.fetch("id", nil))
  url = normalize_url(meta.fetch("url", nil))
  type = "Article"
  creators = if meta.fetch("authors", nil).present?
      get_authors(from_json_feed(Array.wrap(meta.fetch("authors"))))
    else
      [{ "type" => "Organization", "name" => ":(unav)" }]
    end
  titles = [{ "title" => meta.fetch("title", nil) }]
  publisher = { "name" => meta.dig("blog", "title") }

  date = {}
  date["published"] = get_iso8601_date(meta.dig("date_published")) if meta.dig("date_published").present?
  date["updated"] = get_iso8601_date(meta.dig("date_modified")) if meta.dig("date_modified").present?

  license = if meta.dig("blog", "license").present?
      hsh_to_spdx("rightsURI" => meta.dig("blog", "license"))
    end
  home_page_url = normalize_url(meta.dig("blog", "home_page_url"))
  container = if meta.dig("blog", "title").present?
      { "type" => "Periodical",
        "title" => meta.dig("blog", "title"),
        "identifier" => home_page_url,
        "identifierType" => "URL" }
    end

  descriptions = if meta.fetch("summary", nil).present?
      [{ "description" => sanitize(meta.fetch("summary", nil)),
         "descriptionType" => "Abstract" }]
    else
      []
    end
  language = meta.fetch("language", nil) || meta.dig("blog", "language")
  state = id.present? || read_options.present? ? "findable" : "not_found"
  subjects = Array.wrap(meta.fetch("tags", nil)).reduce([]) do |sum, subject|
    sum += name_to_fos(subject)

    sum
  end
  references = get_references(meta)
  alternate_identifiers = [{ "alternateIdentifier" => meta["uuid"], "alternateIdentifierType" => "UUID" }]

  { "id" => id,
    "type" => type,
    "url" => url,
    "titles" => titles,
    "creators" => creators,
    "publisher" => publisher,
    "container" => container,
    "date" => date,
    "language" => language,
    "descriptions" => descriptions,
    "license" => license,
    "subjects" => subjects.presence,
    "references" => references.presence,
    "alternate_identifiers" => alternate_identifiers,
    "state" => state }.compact.merge(read_options)
end