Class: Metamatter::Authors

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/metamatter/authors.rb

Constant Summary

Constants included from Helpers

Helpers::MEDIA_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#client

Constructor Details

#initialize(repository) ⇒ Authors

Returns a new instance of Authors.



11
12
13
# File 'lib/metamatter/authors.rb', line 11

def initialize(repository)
  @repository = repository
end

Instance Attribute Details

#repositoryObject

Returns the value of attribute repository.



9
10
11
# File 'lib/metamatter/authors.rb', line 9

def repository
  @repository
end

Instance Method Details

#github_responseObject



72
73
74
# File 'lib/metamatter/authors.rb', line 72

def github_response
  @contributors_response ||= client.contributors(repository.name_with_owner)
end

#listObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/metamatter/authors.rb', line 15

def list
  authors = []
  contribs = github_response
  contribs.each do |c|
    user = client.user(c.)
    orcid = lookup_orcid(user.name)

    authors << {:name => user.name, :orcid => orcid, :email => user.email, :login => user., :contributions => c.contributions}
  end

  return authors.sort_by { |a| a[:contributions] }.reverse
end

#log_orcids(orcids) ⇒ Object



67
68
69
70
# File 'lib/metamatter/authors.rb', line 67

def log_orcids(orcids)
  puts "Warning: More than one ORCID from search returned"
  orcids.each { |orcid| puts orcid }
end

#lookup_orcid(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metamatter/authors.rb', line 28

def lookup_orcid(name)
  return "" unless name
  family_name = name.split(' ').last
  given_names = name.gsub(family_name, '').strip # hackety hack hack
  url = orcid_search_url(given_names, family_name)

  begin
    response = HTTParty.get(url, :headers => { "Accept" =>  "application/json"}).body
  rescue => e
    response = {}
  end

  orcids = orcids_for(response)

  if orcids.empty?
    return nil
  else
    return orcids.first
  end
end

#orcid_search_url(given_names, family_name) ⇒ Object



49
50
51
# File 'lib/metamatter/authors.rb', line 49

def orcid_search_url(given_names, family_name)
  "https://orcid.org/v1.2/search/orcid-bio/?q=given-names%3A#{given_names}%20AND%20family-name%3A#{family_name}&rows=50"
end

#orcids_for(response) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/metamatter/authors.rb', line 53

def orcids_for(response)
  parsed = JSON.parse(response)
  return nil unless parsed["orcid-search-results"]
  results = parsed["orcid-search-results"]["orcid-search-result"]
  return nil if results.empty?

  detected_orcids = Array(results).map do |result|
    orcid = result['orcid-profile']['orcid-identifier'].fetch("path", nil)
  end.compact

  log_orcids(detected_orcids) if detected_orcids.size > 1
  return detected_orcids
end