Class: Jekyll::InspireHEPCitationsTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/al_citations/inspirehep.rb

Constant Summary collapse

Citations =
{ }

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, params, tokens) ⇒ InspireHEPCitationsTag

Returns a new instance of InspireHEPCitationsTag.



9
10
11
12
# File 'lib/al_citations/inspirehep.rb', line 9

def initialize(tag_name, params, tokens)
  super
  @recid = params.strip
end

Instance Method Details

#render(context) ⇒ Object



14
15
16
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
# File 'lib/al_citations/inspirehep.rb', line 14

def render(context)
  recid = context[@recid.strip]
  api_url = "https://inspirehep.net/api/literature/?fields=citation_count&q=recid:#{recid}"

  begin
    # If the citation count has already been fetched, return it
    if InspireHEPCitationsTag::Citations[recid]
      return InspireHEPCitationsTag::Citations[recid]
    end

    # Fetch the citation count from the API
    uri = URI(api_url)
    response = Net::HTTP.get(uri)
    data = JSON.parse(response)

    # Extract citation count from the JSON data
    citation_count = data["hits"]["hits"][0]["metadata"]["citation_count"].to_i

    # Format the citation count for readability
    citation_count = Helpers.number_to_human(citation_count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })

  rescue Exception => e
    # Handle any errors that may occur during fetching
    citation_count = "N/A"

    # Print the error message including the exception class and message
    puts "Error fetching citation count for #{recid}: #{e.class} - #{e.message}"
  end

  InspireHEPCitationsTag::Citations[recid] = citation_count
  return "#{citation_count}"
end