Module: ECFS::Util

Included in:
ProceedingsQuery, SpreadsheetParser
Defined in:
lib/ecfs.rb,
lib/ecfs/util.rb

Constant Summary collapse

SIGNALS =
[
  'E.g.', 'Accord', 'See', 'See also', 'Cf.',
  'Compare', 'Contra', 'But see', 'But cf.',
  'See generally'
].map {|s| "#{s} Id."} << 'Id.'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_footnotes(url: nil, id_tree: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ecfs.rb', line 20

def self.get_footnotes(url: nil, id_tree: false)
  # hacky 'temp' file
  rando = (rand * 1000000000000000000).to_i
  FileUtils.mkdir_p "tmp/#{rando}"
  path = "tmp/#{rando}/document.doc.zip"

  open(path, 'wb', allow_redirections: :all) do |file|
    file << open(url, allow_redirections: :all).read
    `unzip #{path} -d tmp/#{rando}`
  end

  xml = File.open("tmp/#{rando}/word/footnotes.xml").read
  doc = Nokogiri::XML(xml)

  footnotes = doc.children[0].children[3..-1]

  my_footnotes = footnotes.to_ary.map do |fn|
    {
      index: fn.attributes['id'].value.to_i - 1,
      text: fn.text.strip
    }
  end

  # compute the tree of id. citations
  if id_tree
    my_footnotes.each {|fn| fn[:ids] = []}
    my_footnotes.each {|fn| fn[:id] = false}
    ids = my_footnotes.select {|fn| fn[:text].start_with?(*ECFS::Util::SIGNALS)}
    ids.each {|id| id[:id] = true}

    my_footnotes.each do |fn|
      if fn[:id] == true
        parent_idx = fn[:index]-1
        my_footnotes.find {|fn| fn[:index] == parent_idx}[:ids] << fn
      end
    end

    my_footnotes = send_ids_to_parent(my_footnotes)

  end

  FileUtils.rm_rf("tmp/#{rando}")

  my_footnotes
end

Instance Method Details

#format_iso_date(date) ⇒ Object



3
4
5
6
7
8
# File 'lib/ecfs/util.rb', line 3

def format_iso_date(date)
  # input format 12/22/1988
  chunks = date.split("/")
  new_date = "#{chunks[2]}-#{chunks[0]}-#{chunks[1]}" # "22-12-1988"
  "#{new_date}T00:00:00.000Z" # dumb hack
end

#iso_date_to_simple_date(iso_date) ⇒ Object



10
11
12
13
# File 'lib/ecfs/util.rb', line 10

def iso_date_to_simple_date(iso_date)
  chunks = iso_date.split("T")[0].split("-")
  "#{chunks[1]}-#{chunks[0]}-#{chunks[2]}"
end