Module: Ref2bibtex

Defined in:
lib/ref2bibtex.rb,
lib/ref2bibtex/version.rb

Constant Summary collapse

USER_EMAIL_FILE_PATH =
File.expand_path("#{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.2.3"
@@cutoff =
DEFAULT_CUTOFF

Class Method Summary collapse

Class Method Details

.citation2bibtex(citation) ⇒ Object Also known as: get

Pass a citation, get a String in BibTeX back



88
89
90
# File 'lib/ref2bibtex.rb', line 88

def self.citation2bibtex(citation)
  get_bibtex(get_doi(citation) )
end

.cutoffObject



29
30
31
# File 'lib/ref2bibtex.rb', line 29

def self.cutoff
  @@cutoff
end

.cutoff=(value) ⇒ Object



33
34
35
# File 'lib/ref2bibtex.rb', line 33

def self.cutoff=(value)
  @@cutoff = value
end

.get_bibtex(doi) ⇒ Object

Pass a String doi get a bibtex formatted string back



53
54
55
56
57
58
59
# File 'lib/ref2bibtex.rb', line 53

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
  response = Ref2bibtex.request(uri, headers: {'Accept' => 'application/x-bibtex' }, protocol: 'GET', process_response_as: 'text') 
end

.get_doi(citation) ⇒ Object

Pass a String citation, get a DOI back



62
63
64
65
66
67
68
69
70
# File 'lib/ref2bibtex.rb', line 62

def self.get_doi(citation)
  citation = validate_query(citation)
  response = Ref2bibtex.request(payload: citation) 

  return false if !response['results'][0]['match']
  return false if response['results'][0]['score'] < @@cutoff

  response['results'][0]['doi']
end

.get_score(citation) ⇒ Object

Pass a String citation, get a score back



73
74
75
76
77
78
# File 'lib/ref2bibtex.rb', line 73

def self.get_score(citation)
  citation = validate_query(citation)
  response = Ref2bibtex.request(payload: citation) 
  return false if !response['results'][0]['match']
  response['results'][0]['score']
end

.parse_json(string) ⇒ Object

Parse the response into json



42
43
44
45
46
47
48
49
# File 'lib/ref2bibtex.rb', line 42

def self.parse_json(string)
  begin
    @json = JSON.parse(string) 
  rescue JSON::ParserError => e
    puts e.message
    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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ref2bibtex.rb', line 96

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_cutoffObject



37
38
39
# File 'lib/ref2bibtex.rb', line 37

def self.reset_cutoff
  @@cutoff = DEFAULT_CUTOFF
end

.validate_query(citation) ⇒ Object



80
81
82
83
84
85
# File 'lib/ref2bibtex.rb', line 80

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