Module: Ref2bibtex
- Defined in:
- lib/ref2bibtex.rb,
lib/ref2bibtex/version.rb
Overview
require ‘serrano’
Constant Summary collapse
- USER_EMAIL_FILE_PATH =
File.("#{ENV['HOME']}/.ref2bibtex_user_email")
- USER_EMAIL_FILE =
key
- USER_EMAIL_ENV =
ENV['REF2BIBTEX_USER_EMAIL']
- USER_EMAIL =
USER_EMAIL_FILE || USER_EMAIL_ENV || nil
- CROSSREF_URI =
By default sorts by score
URI("https://search.crossref.org/links?mailto=#{USER_EMAIL}")
- DEFAULT_CUTOFF =
50
- VERSION =
"0.3.0"
- @@cutoff =
DEFAULT_CUTOFF
Class Method Summary collapse
-
.citation2bibtex(citation) ⇒ Object
(also: get)
Pass a citation, get a String in BibTeX back.
- .cutoff ⇒ Object
- .cutoff=(value) ⇒ Object
- .get_bibtex(doi) ⇒ String, false
-
.get_doi(citation) ⇒ String, false
Pass a String citation, get a DOI back.
-
.get_score(citation) ⇒ Float, -1.0
Pass a String citation, get a score back.
- .has_match?(response) ⇒ Boolean
-
.parse_json(string) ⇒ Object
Parse the response into json.
- .request(url = CROSSREF_URI, payload: nil, headers: {'content-type' => 'application/json' }, protocol: 'POST', process_response_as: 'json', redirect_limit: 10) ⇒ Object
- .reset_cutoff ⇒ Object
- .validate_query(citation) ⇒ Object
Class Method Details
.citation2bibtex(citation) ⇒ Object Also known as: get
Pass a citation, get a String in BibTeX back
103 104 105 |
# File 'lib/ref2bibtex.rb', line 103 def self.citation2bibtex(citation) get_bibtex(get_doi(citation) ) end |
.cutoff ⇒ Object
31 32 33 |
# File 'lib/ref2bibtex.rb', line 31 def self.cutoff @@cutoff end |
.cutoff=(value) ⇒ Object
35 36 37 |
# File 'lib/ref2bibtex.rb', line 35 def self.cutoff=(value) @@cutoff = value end |
.get_bibtex(doi) ⇒ String, false
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ref2bibtex.rb', line 55 def self.get_bibtex(doi) return false if !doi doi = Addressable::URI.parse(doi).normalize.to_s uri = URI(doi) return false if uri.class == URI::Generic # Serrano.content_negotiation(ids: doi).first Ref2bibtex.request(uri, headers: {'Accept' => 'application/x-bibtex' }, protocol: 'GET', process_response_as: 'text') end |
.get_doi(citation) ⇒ String, false
Pass a String citation, get a DOI back
70 71 72 73 74 75 76 |
# File 'lib/ref2bibtex.rb', line 70 def self.get_doi(citation) citation = validate_query(citation) response = Ref2bibtex.request(payload: citation) return false unless has_match?(response) return false if response['results'][0]['score'] < @@cutoff response['results'][0]['doi'] end |
.get_score(citation) ⇒ Float, -1.0
Pass a String citation, get a score back
81 82 83 84 85 86 87 88 89 |
# File 'lib/ref2bibtex.rb', line 81 def self.get_score(citation) citation = validate_query(citation) response = Ref2bibtex.request(payload: citation) if has_match?(response) response['results'].first['score'] else -1.0 end end |
.has_match?(response) ⇒ Boolean
91 92 93 |
# File 'lib/ref2bibtex.rb', line 91 def self.has_match?(response) response['query_ok'] && response['results']&.first['match'] end |
.parse_json(string) ⇒ Object
Parse the response into json
44 45 46 47 48 49 50 51 |
# File 'lib/ref2bibtex.rb', line 44 def self.parse_json(string) begin @json = JSON.parse(string) rescue JSON::ParserError => e puts e. ap request end end |
.request(url = CROSSREF_URI, payload: nil, headers: {'content-type' => 'application/json' }, protocol: 'POST', process_response_as: 'json', redirect_limit: 10) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ref2bibtex.rb', line 111 def self.request(url = CROSSREF_URI, payload: nil, headers: {'content-type' => 'application/json' }, protocol: 'POST', process_response_as: 'json', redirect_limit: 10) raise 'Infinite redirect?' if redirect_limit == 0 body = request_body(protocol, payload) request = new_request(protocol, url, headers) response = Net::HTTP.start(request.uri.hostname, request.uri.port, use_ssl: request.uri.scheme == 'https') do |http| request.body = body http.request(request) end case response when Net::HTTPSuccess response = response when Net::HTTPRedirection url = URI(response['location']) request = Net::HTTP::Get.new(url, initheader = {'Accept' => 'application/x-bibtex'}) response = Net::HTTP.start(request.uri.hostname, request.uri.port, use_ssl: request.uri.scheme == 'https') do |http| http.request(request) end else response = response.value end process_response(response, process_response_as) end |
.reset_cutoff ⇒ Object
39 40 41 |
# File 'lib/ref2bibtex.rb', line 39 def self.reset_cutoff @@cutoff = DEFAULT_CUTOFF end |
.validate_query(citation) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/ref2bibtex.rb', line 95 def self.validate_query(citation) return [citation] if citation.kind_of?(String) && citation.length > 0 raise 'citation is not String or Array' if !citation.kind_of?(Array) raise 'citation in array is empty' if citation.empty? || citation.select{|a| a.length == 0}.size > 0 citation end |