module OpenAccess::APC

Methods for managing open access article publishing charges (APCs)

Constants

VERSION

Public Class Methods

configure() { |config| ... } click to toggle source

Sets the module configuration @return [void] @yield [config] Passes the configuration hash to the block @yieldparam config [Hash] the configuration hash

# File lib/openaccess/apc.rb, line 160
def self.configure
  # Get user-supplied configuration from the block
  yield(@config)

  # LDAP client for person resolution
  @ldap = configure_ldap

  # Pure clients
  @person_extractor = Puree::Extractor::Person.new(@config[:pure])
  @person_query = Puree::Query::Person.new(@config[:pure])
end
person(username = nil, employee_id: nil) click to toggle source

Returns the Pure person UUID given a network username @param username [String] the network username @param employee_id [Integer, String] the Pure employee ID @return [String] the Pure UUID of the corresponding person

# File lib/openaccess/apc.rb, line 176
def self.person(username = nil, employee_id: nil)
  employee_id ||= person_employee_id(username)
  person = @person_extractor.find_by_id(employee_id: employee_id)
  return person.uuid unless person.nil?
  raise UserNotFound, 'No user for employee ID: ' \
                      "#{employee_id} (username: #{username})"
end
publications_for_person(username = nil, **opts) click to toggle source

Returns an array of publication summary details for the N most recent funding-eligible publications @param username [String] the network username @param opts [Hash] the options hash @option opts [Date, DateTime, String, Time] :created_end include

publications deposited on or up to this date

@option opts [Date, DateTime, String, Time] :created_start include

publications deposited on or after this date

@option opts [Integer, String] the Pure employee ID @return [Array<Hash>] the publications

# File lib/openaccess/apc.rb, line 194
def self.publications_for_person(username = nil, **opts)
  person_uuid = person(username, employee_id: opts[:employee_id])
  publications_criteria(opts)
  pubs = @person_query.publications_extended(uuid: person_uuid, **opts)
  publications_summary(pubs)
end

Private Class Methods

configure_ldap() click to toggle source

Returns a configured Net::LDAP instance @return [Net::LDAP] the LDAP instance

# File lib/openaccess/apc.rb, line 205
def self.configure_ldap
  conf = @config[:ldap]
  return nil unless conf
  Net::LDAP.new(host: conf[:host], port: conf[:port], auth: conf[:auth])
end
person_employee_id(username = nil) click to toggle source

Returns the Pure employee ID given a network username @param username [String] the network username @return [String] the Pure employee ID

# File lib/openaccess/apc.rb, line 215
def self.person_employee_id(username = nil)
  # Get the LDAP record for the username
  c = @config[:ldap]
  filter = Net::LDAP::Filter.eq(c[:attribute_username], username)
  # Return the value of the LDAP attribute containing the Pure employee ID
  @ldap.search(base: c[:base], filter: filter) do |entry|
    pure_id = entry[c[:attribute_pure_id]]
    return pure_id[0] unless pure_id.nil? || pure_id.empty?
  end
  raise UserNotFound, "No user for username: #{username}"
end
publication_summary(pub) click to toggle source

Returns a hash of publication summary details ready for JSON formatting @param pub [Puree::Model::Publication] the publication @return [Hash] the publication summary rubocop:disable Metrics/MethodLength

# File lib/openaccess/apc.rb, line 232
def self.publication_summary(pub)
  {
    created: pub.created,
    description: pub.description,
    dois: pub.dois,
    subtitle: pub.subtitle,
    title: pub.title,
    translated_subtitle: pub.translated_subtitle,
    translated_title: pub.translated_title,
    uuid: pub.uuid,
    workflow_state: pub.workflow_state
  }
end
publications_criteria(opts) click to toggle source

Sets the Puree person query criteria in an options hash @param opts [Hash] the options hash @return [Hash] the options hash

# File lib/openaccess/apc.rb, line 251
def self.publications_criteria(opts)
  opts[:limit] ||= 5
  opts[:order_by] = ['-created']
  end_date = to_date(opts[:created_date_end])
  start_date = to_date(opts[:created_date_start])
  opts[:created_end] = end_date.to_s if end_date # YYYY-MM-DD
  opts[:created_start] = start_date.to_s if start_date
  opts
end
publications_summary(pubs) click to toggle source

Returns a list of publication summaries ready for JSON formatting @param pubs [Array<Puree::Model::Publication>] the publications @return [Array<Hash>] the publication summaries

# File lib/openaccess/apc.rb, line 265
def self.publications_summary(pubs)
  pubs.map { |p| publication_summary(p) }
end
to_date(date = nil, default = nil) click to toggle source

Converts a date representation to a Date instance. Date instances are returned as-is. DateTime and Time instances are converted to Date instances. All other types are converted to a string and parsed. @param date [Object] the date representation @param default [Date, nil] the default value if no date is supplied or

string parsing fails

@return [Date] the date instance

# File lib/openaccess/apc.rb, line 278
def self.to_date(date = nil, default = nil)
  return default if date.nil? || date.empty?
  return date if date.is_a?(Date)
  return date.to_date if date.respond_to?(:to_date)
  Date.parse(date.to_s)
rescue ArgumentError
  default
end