Class: Whitepaper::Paper

Inherits:
Object
  • Object
show all
Defined in:
lib/whitepaper/paper.rb

Overview

The representation of a paper, including title, author, and pdf urls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, authors, options = {}) ⇒ Paper

Construct an object representing paper metadata with the given fields. Title and authors are required, all other fields can be omitted.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/whitepaper/paper.rb', line 34

def initialize(title, authors, options = {})
  @title = title
  @authors = authors
  @description = options[:description] || ""
  @keywords = options[:keywords] || []
  @year = options[:year] || ""
  @conference = options[:conference] || ""
  @metadata_url = options[:metadata_url] || ""

  @pdf_urls = options[:pdf_urls] || []
  @ps_urls = options[:ps_urls] || []
end

Instance Attribute Details

#authorsObject (readonly)

The list of authors of the paper.



8
9
10
# File 'lib/whitepaper/paper.rb', line 8

def authors
  @authors
end

#conferenceObject (readonly)

The conference, if any, the paper appeared. Defaults to “”.



20
21
22
# File 'lib/whitepaper/paper.rb', line 20

def conference
  @conference
end

#descriptionObject (readonly)

A summary of the paper, typically an abstract. Defaults to “”.



11
12
13
# File 'lib/whitepaper/paper.rb', line 11

def description
  @description
end

#keywordsObject (readonly)

A list of keywords associated with the paper. Defaults to [].



14
15
16
# File 'lib/whitepaper/paper.rb', line 14

def keywords
  @keywords
end

#metadata_urlObject (readonly)

The link to the resource with the most metadata to use as attribution. Defaults to “”.



24
25
26
# File 'lib/whitepaper/paper.rb', line 24

def 
  @metadata_url
end

#pdf_urlsObject (readonly)

A list of urls to pdf copies of the paper. Defaults to [].



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

def pdf_urls
  @pdf_urls
end

#ps_urlsObject (readonly)

A list of urls to ps copies of the paper. Defaults to [].



30
31
32
# File 'lib/whitepaper/paper.rb', line 30

def ps_urls
  @ps_urls
end

#titleObject (readonly)

The title of the paper.



5
6
7
# File 'lib/whitepaper/paper.rb', line 5

def title
  @title
end

#yearObject (readonly)

The year of publication. Defaults to “”.



17
18
19
# File 'lib/whitepaper/paper.rb', line 17

def year
  @year
end

Instance Method Details

#download(filename = nil) ⇒ Object

Downloads the paper by using the pdf urls. The created file will be named after the title if no filename is given. The file will overwrite any existing file with the same name in the current directory.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/whitepaper/paper.rb', line 50

def download(filename = nil)
  if filename.nil?
    filename = title.to_s
  end
  escaped_filename = filename.gsub(/[\t:\?\<\>\*\"\\\/]/, "") + ".pdf"

  f = open(escaped_filename, "w+")

  if pdf_urls.empty?
    return false
  end

  uri = URI.parse(pdf_urls.first)
  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.request_get(uri.request_uri) do |resp|
        resp.read_body do |segment|
          f.write(segment)
        end
      end
    end
  ensure
    f.close()
  end

  true
end

#score_by_title(keywords) ⇒ Object

Gives a score of relevancy to the title keywords given. Higher scores mean that the keywords are more reflective of the title.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/whitepaper/paper.rb', line 80

def score_by_title(keywords)
  keywords = keywords.split(" ").map(&:strip).map(&:downcase)
  title_words = title.split(" ").map(&:strip).map(&:downcase)

  score = 1.0

  # found words are worth x10
  # not found words are worth /2

  keywords.each do |k|
    if title_words.include? k
      score *= 10.0
    end
  end

  title_words.each do |k|
    unless keywords.include? k
      score /= 2.0
    end
  end

  score
end

#to_sObject

Output a simple description of the paper metadata.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/whitepaper/paper.rb', line 105

def to_s
  "Title:       #{@title}\n" +
  "Authors:     #{@authors}\n" +
  "Description: #{@description}\n" +
  "Keywords:    #{@keywords}\n" +
  "Year:        #{@year}\n" +
  "Conference:  #{@conference}\n" + 
  "More info:   #{@metadata_url}\n" +

  "Pdf Available: #{@pdf_urls}\n" +
  "Ps Available: #{@ps_urls}"
end