Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iCite sample Ruby script
require 'open-uri'
require 'json'
require 'csv'
ICITE_URL = "https://icite.od.nih.gov/api/pubs"
ICITE_FIELDS = ["pmid", "authors", "citation_count", "citations_per_year", "expected citations_per_year", "field_citation_rate", "is_research_article", "journal", "nih_percentile", "relative_citation_ratio", "title", "year"]
SAMPLE_PMIDS = %w(
18541772 18542051 18542297 18543598 18544040
18544041 18544607 18545699 18546057 18546185
18546205 18546420 18547521 18548086 18548484
18549456 18549480 18549781 18549792 18550261
18550263 18550523 18550696 18550750 18550751
18551693 18552127 18552280 18552825 18552842
18552857 18553513 18553518 18554181 18554302
18554410 18554535 18554536 18554537 18555770
18555774 18555784 18556473 18556515 18556658
18556659 18556758 18557633 18558353 18558649
18558668 18559476 18559481 18560474 18560567
18562535 18562536 18562598 18562627 18563087
18563180 18564833 18565991 18565998 18566000
18566192 18566540 18566663 18567627 18567814
18567817 18567894 18568017 18568018 18568028
18568348 18570315 18570424 18570871 18570876
18571096 18571144 18571452 18572927 18573084
18573091 18573213 18573652 18573869 18573918
18574040 18574138 18574145 18576324 18577586
18577756 18579608 18579657 18579682 18579731
18579743 18579776 18579784 18579798 18579813
18579869 18579870 18583346 18583599 18583936
18585098 18585354 18585357 18585358 18585528
18585699 18585870 18586090 18586670 18586882
18586938 18586943 18587121 18587384 18587389
18587397 18587609 18588334 18588487 18588488
18588692 18588970 18590693 18590699 18590736
18590806 18590813 18590818 18591412 18591467
18591963 18591966 18593557 18593559 18593637
18593662 18595705 18595771 18596033 18596266
18596815 18596819 18597424 18597511 18597807
18598267 18598681 18598704 18599436 18599455
18599626 18599825 18602395 18602471 18602567
18602999 18603132 18603593 18604202 18604217
18604272 18606549 18606653 18606733 18606857
18611380 18612297 18612378 18612386 18612436
18613727 18613951 18614008 18614010 18614014
18614015 18614016 18614030 18614046 18614049
18614631 18614669 18614673 18615016 18615733
18616282 18616427 18617267 18617992 18618939
)
# Single PMID
def get_single_pmid(pmid)
url = ICITE_URL + "/" + pmid
response = open(url)
icite_json = JSON.parse(response.read)
pub = icite_json
end
# Multiple PMIDs
def get_multiple_pmids(pmids)
pmids = pmids.join(',')
url = ICITE_URL + "?pmids=" + pmids
response = open(url)
icite_json = JSON.parse(response.read)
pubs = icite_json["data"]
end
# Multiple PMIDs, with option of specifying CSV
def get_multiple_pmids_csv_option(pmids, accept_csv: false)
pmids = pmids.join(',')
url = ICITE_URL + "?pmids=" + pmids + (accept_csv ? "&format=csv" : "")
response = open(url)
if accept_csv
return CSV.parse(response.read)
else
return JSON.parse(response.read)["data"]
end
end
puts "Testing single pmid request.\nResults:"
pmid = SAMPLE_PMIDS.sample
puts get_single_pmid(pmid)
puts ""
puts "Testing multiple pmid simple request.\nResults:"
pmids = SAMPLE_PMIDS.sample(5)
puts get_multiple_pmids(pmids)
puts ""
puts "Testing multiple pmid request, csv accepted.\nResults:"
pmids = SAMPLE_PMIDS.sample(5)
print get_multiple_pmids_csv_option(pmids, accept_csv: true)
puts ""
puts "Testing single pmid request, csv not accepted.\nResults:"
pmids = SAMPLE_PMIDS.sample(5)
puts get_multiple_pmids_csv_option(pmids, accept_csv: false)
puts ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment