Class: Patinfo2csv::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/patinfo2csv/converter.rb

Constant Summary collapse

CHAPTERS =
[
  :effects,
  :contra_indications,
  :pregnancy,
  :usage,
  :composition
]
DELIMITER =
';'
WORKERS =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



18
19
20
21
22
23
24
25
# File 'lib/patinfo2csv/converter.rb', line 18

def initialize
  @mutex = Mutex.new
  @q     = Queue.new
  @lang     = "de"
  @patinfos = []
  @codes    = []
  @rows     = {}
end

Instance Attribute Details

#codesObject

Returns the value of attribute codes.



8
9
10
# File 'lib/patinfo2csv/converter.rb', line 8

def codes
  @codes
end

#langObject

Returns the value of attribute lang.



8
9
10
# File 'lib/patinfo2csv/converter.rb', line 8

def lang
  @lang
end

#patinfosObject

Returns the value of attribute patinfos.



8
9
10
# File 'lib/patinfo2csv/converter.rb', line 8

def patinfos
  @patinfos
end

Instance Method Details

#extract_chapters(row) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/patinfo2csv/converter.rb', line 73

def extract_chapters(row)
  return nil if row['descriptions'][@lang].empty?
  desc = row['descriptions'][@lang]
  chapters = []
  CHAPTERS.each do |chapter|
    if (desc.has_key?(chapter.to_s) and !desc[chapter.to_s].nil?) and
       !desc[chapter.to_s]['sections'].nil?
      text = ''
      heading = desc[chapter.to_s]['heading']
      desc[chapter.to_s]['sections'].each do |section|
        heading = unless section['subheading'].empty?
                    section['subheading'].chomp
                  else
                    heading.chomp
                  end
        text << "<strong>#{heading}</strong><br />"
        if section.has_key?('paragraphs')
          section['paragraphs'].each do |paragraph|
            unless paragraph['text'].empty?
              text << paragraph['text'].gsub(/\r\n|\r|\n/, "<br />") + "<br />"
            end
          end
        end
      end
    else
      text = "" #empty
    end
    text.gsub!(/\<br\s\/\>$/, '')
    chapters << %Q!"#{text.chomp}"!
  end
  chapters.join(DELIMITER)
end

#to_csvObject



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
65
66
67
68
69
70
71
72
# File 'lib/patinfo2csv/converter.rb', line 26

def to_csv
  @patinfos.each do |row|
    @q << row
  end
  Array.new(WORKERS) do
    @q << nil
    Thread.new do
      while row = @q.pop
        target = []
        next if row['article_codes'].nil?
        row['article_codes'].each do |article|
          @mutex.synchronize do
            if @codes.include?(article[:article_ean13]) # EAN
              target << article[:article_ean13]
              @codes.delete(article[:article_ean13])
            end
            #elsif @code.include?(code[:article_pcode]) # Pharmacode
            #  target << code[:article_pcode]
            #  @code.delete(code[:article_pcode])
            #end
          end
        end
        next if target.empty?
        chapters = extract_chapters(row)
        next if chapters.empty?
        target.each do |code|
          @mutex.synchronize do
            @rows[code.to_i] = [%Q!"#{code}"!, chapters].flatten.join(DELIMITER)
          end
        end
      end
    end
  end.map(&:join)
  unless @rows.empty? #header
    rows = @rows.sort_by{|k, v| k}.map{|row| row[1]}
    rows.unshift([
      "EAN",
      #"Pharmacode",
      "Effects",
      "Contra Indication",
      "Pregnancy",
      "Usage",
      "Composition"
    ].join(DELIMITER))
  end
  return rows
end