Module: Commonmeta::Readers::JsonFeedReader
- Included in:
- CLI, MetadataUtils
- Defined in:
- lib/commonmeta/readers/json_feed_reader.rb
Instance Method Summary collapse
- #get_doi_prefix_by_blog_id(blog_id) ⇒ Object
- #get_doi_prefix_by_json_feed_item_uuid(uuid) ⇒ Object
- #get_json_feed_by_blog(blog_id) ⇒ Object
- #get_json_feed_item(id: nil, **options) ⇒ Object
- #get_json_feed_not_indexed ⇒ Object
- #get_json_feed_unregistered ⇒ Object
- #get_references(meta) ⇒ Object
- #read_json_feed_item(string: nil, **options) ⇒ Object
Instance Method Details
#get_doi_prefix_by_blog_id(blog_id) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 130 def get_doi_prefix_by_blog_id(blog_id) # for generating a random DOI. url = json_feed_by_blog_url(blog_id) 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_uuid(uuid) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 141 def get_doi_prefix_by_json_feed_item_uuid(uuid) # for generating a random DOI. Prefix is based on the blog id. url = json_feed_item_by_uuid_url(uuid) 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_json_feed_by_blog(blog_id) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 119 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["uuid"] }.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, **) 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_not_indexed ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 108 def get_json_feed_not_indexed # get JSON Feed items not indexed in Crossref since they have been last updated url = json_feed_not_indexed_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_unregistered ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 97 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_references(meta) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 84 def get_references() # check that references resolve Array.wrap(["references"]).reduce([]) do |sum, reference| if reference["doi"] && validate_doi(reference["doi"]) sum << reference if [200, 301, 302].include? HTTP.head(reference["doi"]).status elsif reference["url"] && validate_url(reference["url"]) == "URL" sum << reference if [200, 301, 302].include? HTTP.head(reference["url"]).status 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 |
# File 'lib/commonmeta/readers/json_feed_reader.rb', line 17 def read_json_feed_item(string: nil, **) = ActiveSupport::HashWithIndifferentAccess.new(.except(:doi, :id, :url, :sandbox, :validate, :ra)) = string.present? ? JSON.parse(string) : {} url = normalize_url(.fetch("url", nil)) id = [:doi] ? normalize_doi([:doi]) : normalize_id(.fetch("id", nil)) id = url if id.blank? && url.present? type = "Article" creators = if .fetch("authors", nil).present? (from_json_feed(Array.wrap(.fetch("authors")))) else [{ "type" => "Organization", "name" => ":(unav)" }] end titles = [{ "title" => .fetch("title", nil) }] publisher = { "name" => .dig("blog", "title") } date = {} date["published"] = get_iso8601_date(.dig("date_published")) if .dig("date_published").present? date["updated"] = get_iso8601_date(.dig("date_modified")) if .dig("date_modified").present? license = if .dig("blog", "license").present? hsh_to_spdx("rightsURI" => .dig("blog", "license")) end home_page_url = normalize_url(.dig("blog", "home_page_url")) container = if .dig("blog", "title").present? { "type" => "Periodical", "title" => .dig("blog", "title"), "identifier" => home_page_url, "identifierType" => "URL" } end descriptions = if .fetch("summary", nil).present? [{ "description" => sanitize(.fetch("summary", nil)), "descriptionType" => "Abstract" }] else [] end language = .fetch("language", nil) || .dig("blog", "language") state = id.present? || .present? ? "findable" : "not_found" subjects = Array.wrap(.dig("blog", "category")).reduce([]) do |sum, subject| sum += name_to_fos(subject) sum end references = get_references() alternate_identifiers = [{ "alternateIdentifier" => ["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() end |