Class: MaisOrcidClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mais_orcid_client.rb,
lib/mais_orcid_client/version.rb,
lib/mais_orcid_client/authenticator.rb,
lib/mais_orcid_client/token_wrapper.rb,
lib/mais_orcid_client/unexpected_response.rb

Overview

Client for interacting with MAIS’s ORCID API

Defined Under Namespace

Classes: Authenticator, OrcidUser, TokenWrapper, UnexpectedResponse

Constant Summary collapse

VERSION =
"0.3.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



44
45
46
# File 'lib/mais_orcid_client.rb', line 44

def config
  @config
end

Class Method Details

.configure(client_id:, client_secret:, base_url:) ⇒ Object

Parameters:

  • client_id (String)

    the client identifier registered with MAIS

  • client_secret (String)

    the client secret to authenticate with MAIS

  • base_url (String)

    the base URL for the API



30
31
32
33
34
35
36
37
38
39
# File 'lib/mais_orcid_client.rb', line 30

def configure(client_id:, client_secret:, base_url:)
  instance.config = OpenStruct.new(
    token: Authenticator.token(client_id, client_secret, base_url),
    client_id:,
    client_secret:,
    base_url:
  )

  self
end

Instance Method Details

#fetch_orcid_user(sunetid: nil, orcidid: nil) ⇒ <OrcidUser>?

Fetch a user details, including scope and token, given either a SUNetID or ORCIDID

Parameters:

  • sunetid (string) (defaults to: nil)

    of user to fetch

  • orcidid (orcid) (defaults to: nil)

    of user to fetch (ignored if sunetid is also provided)

Returns:

  • (<OrcidUser>, nil)

    orcid user or nil if not found



68
69
70
71
72
# File 'lib/mais_orcid_client.rb', line 68

def fetch_orcid_user(sunetid: nil, orcidid: nil)
  raise "must provide either a sunetid or orcidid" unless sunetid || orcidid

  sunetid ? fetch_by_sunetid(sunetid) : fetch_by_orcidid(orcidid)
end

#fetch_orcid_users(limit: nil, page_size: nil) ⇒ Array<OrcidUser>

Returns orcid users.

Parameters:

  • limit (int) (defaults to: nil)

    number of users requested

  • page_size (int) (defaults to: nil)

    number of users per page

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mais_orcid_client.rb', line 49

def fetch_orcid_users(limit: nil, page_size: nil)
  orcid_users = []
  next_page = first_page(page_size)
  loop do
    response = get_response(next_page)
    response[:results].each do |result|
      orcid_users << OrcidUser.new(result[:sunet_id], result[:orcid_id], result[:scope], result[:access_token], result[:last_updated])
      return orcid_users if limit && limit == orcid_users.size
    end
    # Currently next is always present, even on last page. (This may be changed in future.)
    next_page = response.dig(:links, :next)
    return orcid_users if last_page?(response[:links])
  end
end