Class: ArxivSync::XMLParser

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/arxivsync/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#papersObject

Returns the value of attribute papers.



28
29
30
# File 'lib/arxivsync/parser.rb', line 28

def papers
  @papers
end

Instance Method Details

#clean(str) ⇒ Object



43
44
45
# File 'lib/arxivsync/parser.rb', line 43

def clean(str)
  str.gsub(/\s+/, ' ').strip
end

#end_element(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arxivsync/parser.rb', line 88

def end_element(name)
  case name
  when :version
    @versions.push(@version)
  when :metadata # End of a paper entry
    @model.versions = @versions

    @papers.push(@model)
  end
  @el = nil
end

#start_element(name, attributes = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arxivsync/parser.rb', line 30

def start_element(name, attributes=[])
  @el = name
  case name
  when :ListRecords
    @papers = []
  when :metadata
    @model = Paper.new
    @versions = []
  when :version
    @version = Version.new
  end
end

#text(str) ⇒ Object



47
48
49
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
77
78
79
80
81
82
83
84
85
86
# File 'lib/arxivsync/parser.rb', line 47

def text(str)
  case @el
  # Necessary elements
  when :id
    @model.id = clean(str)
  when :submitter
    @model.submitter = clean(str)
  when :title
    @model.title = clean(str)
  when :authors
    @model.authors = clean(str).split(/,| and /)
      .map { |s| LaTeX.decode(clean(s)) }.reject { |s| s.empty? }
  when :categories
    @model.categories = clean(str).split(/\s/)
  when :abstract
    @model.abstract = clean(str)

  # Optional elements
  when :comments
    @model.comments = clean(str)
  when :"msc-class"
    @model.msc_class = clean(str)
  when :"report-no"
    @model.report_no = clean(str)
  when :"journal-ref"
    @model.journal_ref = clean(str)
  when :doi
    @model.doi = clean(str)
  when :proxy
    @model.proxy = clean(str)
  when :license
    @model.license = clean(str)

  # Versions
  when :date
    @version.date = Time.parse(clean(str))
  when :size
    @version.size = clean(str)
  end
end