class Doi2bibtex::Doi2bibtex

parse daily weather description from cwb website

Constants

CROSSREF_URL

Attributes

bibtex[R]
doi[R]

Public Class Methods

new(doi) click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 12
def initialize(doi)
  @doi ||= doi
  handle_doi_request
  parsing_bibtex
end

Public Instance Methods

author() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 22
def author
  @hash["author"].to_s
end
book_title() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 34
def book_title
  if journal?
    @hash["journal"].to_s
  else
    @hash["booktitle"].to_s
  end
end
inspect() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 70
def inspect
  "<#{Doi2bibtex}:0x#{'%x' % (object_id << 1)} @doi=#{doi.inspect}, @bibtex=#{@bibtex.inspect}>"
end
issue() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 50
def issue
  @hash["number"].nil? ? nil:@hash["number"].to_i
end
journal?() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 30
def journal?
  !@hash["journal"].nil?
end
month() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 62
def month
  if @hash["month"].nil?
    nil
  else
    Date::MONTHNAMES.index(@hash["month"].capitalize)
  end
end
pages() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 54
def pages
  @hash["pages"].to_s.gsub('--', '-')
end
publisher() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 42
def publisher
  @hash["publisher"].to_s
end
title() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 26
def title
  @hash["title"].to_s
end
to_h() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 78
def to_h
  {
    doi: doi,
    url: url,
    author: author,
    title: title,
    book_title: book_title,
    publisher: publisher,
    volume: volume,
    issue: issue,
    pages: pages,
    year: year,
    month: month
  }
end
to_s() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 74
def to_s
  bibtex
end
url() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 18
def url
  CGI.unescape(@hash["url"])
end
volume() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 46
def volume
  @hash["volume"].nil? ? nil:@hash["volume"].to_i
end
year() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 58
def year
  @hash["year"].to_i
end

Private Instance Methods

handle_doi_request() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 96
def handle_doi_request
  url = URI(CROSSREF_URL + doi)

  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Get.new(url)
  request["accept"] = 'application/x-bibtex'
  res = http.request(request)

  # res = HTTP.headers(:accept => 'application/x-bibtex').get(CROSSREF_URL + doi)
  if res.code.to_i == 200
    @bibtex ||= res.body
  else
    @bibtex = nil
  end
end
parsing_bibtex() click to toggle source
# File lib/doi2bibtex/doi2bibtex.rb, line 112
def parsing_bibtex
  if bibtex.nil?
    @hash = nil
  else
    data = bibtex.delete("{},\\").delete("\n").split("\t").drop(1)
    @hash ||= data.map { |e| e.split(" = ")}.to_h
  end
end