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_doi_prefix_by_blog_slug(blog_slug) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 237

def get_doi_prefix_by_blog_slug(blog_slug)
  # for generating a random DOI.

  url = json_feed_by_blog_url(blog_slug)
  response = HTTP.get(url)
  return nil unless response.status.success?

  post = JSON.parse(response.body.to_s)
  post.to_h.dig("prefix")
end

#get_doi_prefix_by_json_feed_item_id(id) ⇒ Object



248
249
250
251
252
253
254
255
256
257
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 248

def get_doi_prefix_by_json_feed_item_id(id)
  # for generating a random DOI. Prefix is based on the blog id.

  url = json_feed_item_by_id_url(id)
  response = HTTP.get(url)
  return nil unless response.status.success?

  post = JSON.parse(response.body.to_s)
  post.to_h.dig("blog", "prefix")
end

#get_files(id) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 107

def get_files(id)
  doi = doi_from_url(id)
  return nil unless is_rogue_scholar_doi?(doi)
    
  [{ "mimeType" => "text/markdown", "url" => "https://api.rogue-scholar.org/posts/#{doi}.md" },
   { "mimeType" => "application/pdf", "url" => "https://api.rogue-scholar.org/posts/#{doi}.pdf" },
   { "mimeType" => "application/epub+zip", "url" => "https://api.rogue-scholar.org/posts/#{doi}.epub" },
   { "mimeType" => "application/xml", "url" => "https://api.rogue-scholar.org/posts/#{doi}.xml" }]
end

#get_funding_references(meta) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 156

def get_funding_references(meta)
  # check that relationships resolve and have type "HasAward" or funding is provided by blog metadata
  if funding = meta.dig("blog", "funding")
    fundref = { 
      "funderIdentifier" => funding["funder_id"],
      "funderIdentifierType" => "Crossref Funder ID",
      "funderName" => funding["funder_name"], 
      "awardNumber" => funding["award_number"]
    }

  else
    fundref = nil
  end
  Array.wrap(fundref) + Array.wrap(meta["relationships"]).reduce([]) do |sum, relationship|
    begin
      # funder is European Commission
      if validate_prefix(relationship["url"]) == "10.3030" || URI.parse(relationship["url"]).host == "cordis.europa.eu"
        relationship["funderIdentifier"] = "http://doi.org/10.13039/501100000780"
        relationship["funderName"] = "European Commission"
        relationship["awardNumber"] = relationship["url"].split("/").last
      end
      if relationship["type"] == "HasAward" && relationship["funderName"]
        sum << {
          "funderIdentifier" => relationship["funderIdentifier"],
          "funderName" => relationship["funderName"],
          "awardNumber" => relationship["awardNumber"],
        }
      end
    rescue => error
      # puts "Error: #{error.message}"
      # puts "Error: #{reference}"
    end

    sum
  end
end

#get_json_feed_blog_slug(id) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 226

def get_json_feed_blog_slug(id)
  # get JSON Feed item by id and return blog slug

  url = json_feed_item_by_id_url(id)
  response = HTTP.get(url)
  return nil unless response.status.success?

  post = JSON.parse(response.body.to_s)
  post.to_h.dig("blog", "slug")
end

#get_json_feed_by_blog(blog_id) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 215

def get_json_feed_by_blog(blog_id)
  # get all JSON Feed items from a particular blog

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

  blog = JSON.parse(response.body.to_s)
  blog["items"].map { |item| item["id"] }.first
end

#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_unregisteredObject



193
194
195
196
197
198
199
200
201
202
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 193

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["id"] }.first
end

#get_json_feed_updatedObject



204
205
206
207
208
209
210
211
212
213
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 204

def get_json_feed_updated
  # get JSON Feed items not indexed in Crossref since they have been last updated

  url = json_feed_updated_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["id"] }.first
end

#get_references(meta) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 117

def get_references(meta)
  # check that references resolve
  Array.wrap(meta["reference"]).reduce([]) do |sum, reference|
    begin
      if reference["doi"] && validate_doi(reference["doi"])
        response = HTTP.follow
          .headers(:accept => "application/vnd.citationstyles.csl+json")
          .get(reference["doi"])
        csl = JSON.parse(response.body.to_s)
        sum << reference.merge("title" => csl["title"], "publicationYear" => csl.dig("issued", "date-parts", 0, 0).to_s) if [200, 301, 302].include? response.status
      elsif reference["url"] && validate_url(reference["url"]) == "URL"
        sum << reference if [200, 301, 302].include? HTTP.head(reference["url"]).status
      end
    rescue => error
      # puts "Error: #{error.message}"
      # puts "Error: #{reference}"
    end

    sum
  end
end


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 139

def get_related_identifiers(meta)
  # check that relationships resolve and has a supported type
  supported_types = %w[IsIdenticalTo IsPreprintOf IsTranslationOf]
  Array.wrap(meta["relationships"]).reduce([]) do |sum, relationship|
    begin
      if supported_types.include?(relationship["type"]) && [200, 301, 302].include?(HTTP.head(relationship["url"]).status)
        sum << { "id" => relationship["url"], "type" => relationship["type"] }
      end
    rescue => error
      # puts "Error: #{error.message}"
      # puts "Error: #{reference}"
    end

    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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) : {}

  if (meta.dig("blog", "status") == "archived")
    url = normalize_url(meta.fetch("archive_url", nil))
  else
    url = normalize_url(meta.fetch("url", nil))
  end
  id = options[:doi] ? normalize_doi(options[:doi]) : normalize_id(meta.fetch("doi", nil))
  id = normalize_url(meta.fetch("url", nil)) if id.blank?

  type = "Article"
  contributors = 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_date_from_unix_timestamp(meta.dig("published_at")) if meta.dig("published_at").present?
  date["updated"] = get_date_from_unix_timestamp(meta.dig("updated_at")) if meta.dig("updated_at").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.dig("blog", "category")).reduce([]) do |sum, subject|
    sum += name_to_fos(subject.underscore.humanize)

    sum
  end
  references = get_references(meta)
  funding_references = get_funding_references(meta)
  related_identifiers = get_related_identifiers(meta)
  alternate_identifiers = [{ "alternateIdentifier" => meta["id"], "alternateIdentifierType" => "UUID" }]
  files = get_files(id)
  archive_url = meta.fetch("archive_url", nil)

  if archive_url
    host = URI.parse(archive_url).host
    if ["web.archive.org", "wayback.archive-it.org"].include?(host)
      archive_locations = ["Internet Archive"]
    else
      archive_locations = nil
    end
  else
    archive_locations = nil
  end

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