Class: ArxivApi

Inherits:
Object
  • Object
show all
Defined in:
lib/arxiv/references/ArxivApi.rb

Constant Summary collapse

BASE_URL =
'https://arxiv.org'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ ArxivApi

Returns a new instance of ArxivApi.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arxiv/references/ArxivApi.rb', line 9

def initialize(id)
  id = "#{BASE_URL}/abs/#{id}" if id.index('http').nil?
  charset = nil
  html = open(id) do |f|
    charset = f.charset
    f.read
  end
  @page = Nokogiri::HTML.parse(html, nil, charset)
  @title = fetch_title
  @authors = fetch_authors
  @abstruct = fetch_abstruct
  @pdfurl = fetch_pdfurl
  @references = nil
end

Instance Attribute Details

#abstructObject (readonly)

Returns the value of attribute abstruct.



6
7
8
# File 'lib/arxiv/references/ArxivApi.rb', line 6

def abstruct
  @abstruct
end

#authorsObject (readonly)

Returns the value of attribute authors.



6
7
8
# File 'lib/arxiv/references/ArxivApi.rb', line 6

def authors
  @authors
end

#pdfurlObject (readonly)

Returns the value of attribute pdfurl.



6
7
8
# File 'lib/arxiv/references/ArxivApi.rb', line 6

def pdfurl
  @pdfurl
end

#referencesObject

Returns the value of attribute references.



7
8
9
# File 'lib/arxiv/references/ArxivApi.rb', line 7

def references
  @references
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/arxiv/references/ArxivApi.rb', line 6

def title
  @title
end

Instance Method Details

#fetch_abstructObject



32
33
34
# File 'lib/arxiv/references/ArxivApi.rb', line 32

def fetch_abstruct
  @page.xpath('//*[@id="abs"]/div[2]/blockquote').children.select{|i| i.name = 'text'}.reverse.shift.text
end

#fetch_authorsObject



28
29
30
# File 'lib/arxiv/references/ArxivApi.rb', line 28

def fetch_authors
  @page.xpath('//*[@id="abs"]/div[2]/div[2]/a').map(&:text)
end

#fetch_pdfurlObject



36
37
38
# File 'lib/arxiv/references/ArxivApi.rb', line 36

def fetch_pdfurl
  "#{BASE_URL}#{@page.xpath('//*[@id="abs"]/div[1]/div[1]/ul/li[1]/a').attr('href').value}"
end

#fetch_titleObject



24
25
26
# File 'lib/arxiv/references/ArxivApi.rb', line 24

def fetch_title
  @page.xpath('//*[@id="abs"]/div[2]/h1').children.select{|i| i.name=='text'}.shift.text.gsub(/\n/,'')
end

#to_jsonObject



40
41
42
# File 'lib/arxiv/references/ArxivApi.rb', line 40

def to_json
  JSON.pretty_generate({title: @title, authors: @authors, abstruct: @abstruct, pdfurl: @pdfurl, references: @references})
end