class OpenAccessReporter::Reporter

Public Class Methods

new(email) click to toggle source

@param email [String] Email for Unpaywall API

# File lib/open_access_reporter/reporter.rb, line 8
def initialize(email)
  raise ArgumentError, 'Unpaywall email not set' unless email && !email.empty?
  @email = email
  @http_client = HTTP::Client.new
end

Public Instance Methods

find(doi) click to toggle source

Find open access data for a research output (makes one Unpaywall API call, see documentation for restrictions) @param doi [String] DOI e.g. 10.1234/abc, doi:10.1234/abc, doi.org/10.1234/abc @return [OpenAccessReporter::Report, nil]

# File lib/open_access_reporter/reporter.rb, line 17
def find(doi)
  unpaywall_object = fetch(doi)
  return nil if unpaywall_object[:error] === true
  report = OpenAccessReporter::Report.new
  report.classification = classification unpaywall_object
  report.open = open? unpaywall_object
  report.license = license unpaywall_object
  report.modified_at = modified_at unpaywall_object
  report.title = title unpaywall_object
  report
end

Private Instance Methods

classification(unpaywall_object) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 58
def classification(unpaywall_object)
  if unpaywall_object[:is_oa] === true
    if unpaywall_object[:best_oa_location][:host_type] === 'repository'
      'green'
    elsif unpaywall_object[:best_oa_location][:host_type] === 'publisher' && unpaywall_object[:best_oa_location][:license].nil?
      'bronze'
    elsif unpaywall_object[:best_oa_location][:host_type] === 'publisher' && unpaywall_object[:best_oa_location][:license]
      'gold'
    end
  end
end
encode_doi(doi) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 75
def encode_doi(doi)
  doi.to_uri.to_s.gsub 'https://doi.org/', ''
end
fetch(doi) click to toggle source

@return [Hash<Symbol>] Verbatim Unpaywall API JSON response

# File lib/open_access_reporter/reporter.rb, line 32
def fetch(doi)
  doi = DOI.parse doi
  encoded_doi = encode_doi doi
  unpaywall_entry encoded_doi
end
license(unpaywall_object) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 50
def license(unpaywall_object)
  if unpaywall_object[:is_oa] === true
    if !unpaywall_object[:best_oa_location].empty?
      unpaywall_object[:best_oa_location][:license]
    end
  end
end
modified_at(unpaywall_object) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 46
def modified_at(unpaywall_object)
  unpaywall_object[:updated]
end
open?(unpaywall_object) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 38
def open?(unpaywall_object)
  unpaywall_object[:is_oa]
end
title(unpaywall_object) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 42
def title(unpaywall_object)
  unpaywall_object[:title]
end
unpaywall_entry(encoded_doi) click to toggle source
# File lib/open_access_reporter/reporter.rb, line 70
def unpaywall_entry(encoded_doi)
  unpaywall_object = @http_client.get "https://api.unpaywall.org/v2/#{encoded_doi}?email=#{@email}"
  JSON.parse unpaywall_object, symbolize_names: true
end