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.2"
@@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



84
85
86
# File 'lib/ref2bibtex.rb', line 84

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

.cutoffObject



27
28
29
# File 'lib/ref2bibtex.rb', line 27

def self.cutoff
  @@cutoff
end

.cutoff=(value) ⇒ Object



31
32
33
# File 'lib/ref2bibtex.rb', line 31

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

.get_bibtex(doi) ⇒ Object

Pass a String doi get a bibtex formatted string back



50
51
52
53
54
55
# File 'lib/ref2bibtex.rb', line 50

def self.get_bibtex(doi)
  return false if !doi
  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



58
59
60
61
62
63
64
65
66
# File 'lib/ref2bibtex.rb', line 58

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



69
70
71
72
73
74
# File 'lib/ref2bibtex.rb', line 69

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

.interpret_response(response) ⇒ Object



123
124
# File 'lib/ref2bibtex.rb', line 123

def self.interpret_response(response)
end

.new_request(protocol, url, headers) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/ref2bibtex.rb', line 135

def self.new_request(protocol, url, headers)
  case protocol
  when 'POST'
    Net::HTTP::Post.new(url, initheader = headers) 
  when 'GET'
    Net::HTTP::Get.new(url, initheader = headers) 
  else
    raise 'invalid protocol'
  end
end

.parse_json(string) ⇒ Object

Parse the response into json



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

def self.parse_json(string)
  begin
    @json = JSON.parse(string) 
  rescue JSON::ParserError => e
    puts e.message
    ap request
  end
end

.process_response(response, as) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/ref2bibtex.rb', line 146

def self.process_response(response, as)
  case as
  when 'text' 
    response.body
  when 'json'
    parse_json(response.body)
  else
    raise 'response process type not provided'
  end
end

.request(url = CROSSREF_URI, payload: nil, headers: {'content-type' => 'application/json' }, protocol: 'POST', process_response_as: 'json', redirect_limit: 10) ⇒ Object



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

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

.request_body(protocol, payload) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/ref2bibtex.rb', line 126

def self.request_body(protocol, payload)
  if protocol == 'POST'
    payload = {} if payload.nil?
    JSON.generate(payload) # Json.new(payload) # utf-8 encoding?
  else
    nil
  end
end

.reset_cutoffObject



35
36
37
# File 'lib/ref2bibtex.rb', line 35

def self.reset_cutoff
  @@cutoff = DEFAULT_CUTOFF
end

.validate_query(citation) ⇒ Object



76
77
78
79
80
81
# File 'lib/ref2bibtex.rb', line 76

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