Class: ArxivApi
- Inherits:
-
Object
- Object
- ArxivApi
- Defined in:
- lib/arxiv/references/ArxivApi.rb
Constant Summary collapse
- BASE_URL =
'https://arxiv.org'
Instance Attribute Summary collapse
-
#abstruct ⇒ Object
readonly
Returns the value of attribute abstruct.
-
#authors ⇒ Object
readonly
Returns the value of attribute authors.
-
#pdfurl ⇒ Object
readonly
Returns the value of attribute pdfurl.
-
#references ⇒ Object
Returns the value of attribute references.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #fetch_abstruct ⇒ Object
- #fetch_authors ⇒ Object
- #fetch_pdfurl ⇒ Object
- #fetch_title ⇒ Object
-
#initialize(id) ⇒ ArxivApi
constructor
A new instance of ArxivApi.
- #to_json ⇒ Object
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 = @abstruct = fetch_abstruct @pdfurl = fetch_pdfurl @references = nil end |
Instance Attribute Details
#abstruct ⇒ Object (readonly)
Returns the value of attribute abstruct.
6 7 8 |
# File 'lib/arxiv/references/ArxivApi.rb', line 6 def abstruct @abstruct end |
#authors ⇒ Object (readonly)
Returns the value of attribute authors.
6 7 8 |
# File 'lib/arxiv/references/ArxivApi.rb', line 6 def @authors end |
#pdfurl ⇒ Object (readonly)
Returns the value of attribute pdfurl.
6 7 8 |
# File 'lib/arxiv/references/ArxivApi.rb', line 6 def pdfurl @pdfurl end |
#references ⇒ Object
Returns the value of attribute references.
7 8 9 |
# File 'lib/arxiv/references/ArxivApi.rb', line 7 def references @references end |
#title ⇒ Object (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_abstruct ⇒ Object
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_authors ⇒ Object
28 29 30 |
# File 'lib/arxiv/references/ArxivApi.rb', line 28 def @page.xpath('//*[@id="abs"]/div[2]/div[2]/a').map(&:text) end |
#fetch_pdfurl ⇒ Object
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_title ⇒ Object
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_json ⇒ Object
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 |