Class: ODDB::CoMarketingPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/comarketing.rb

Constant Summary

COMARKETING_PARSER =
DRb::DRbObject.new(nil, COMARKETING_URI)
SOURCE_URI =
'http://www.swissmedic.ch/daten/00080/00260/index.html?lang=de'

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Plugin

#initialize, #l10n_sessions, #log_info, #recipients, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

This class inherits a constructor from ODDB::Plugin

Instance Method Details

- (Object) _find(name, fuzz = false)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'src/plugin/comarketing.rb', line 17

def _find(name, fuzz=false)
  search_term = ODDB.search_term(name)
  sequences = @app.search_sequences(search_term, fuzz)
  registrations = sequence_registrations(sequences)
  if(registrations.size > 1)
    if(registrations.size > 1 && (match = /,\s*([^,]+)$/u.match(name)) \
       && (galform = @app.galenic_form(match[1])))
      registrations = sequence_registrations(sequences) { |seq|
        galform.equivalent_to?(seq.galenic_form)
      }
      if(registrations.size > 1)
        registrations = sequence_registrations(sequences) { |seq|
          galform == seq.galenic_form
        }
      end
    end
  end
  if(registrations.size == 1)
    return registrations.first
  end
end

- (Object) _sequence_registrations(sequences)



72
73
74
75
# File 'src/plugin/comarketing.rb', line 72

def _sequence_registrations(sequences)
  sequences.collect { |seq| 
    seq.registration }.uniq.reject { |reg| reg.parallel_import }
end

- (Object) find(iksnr)



14
15
16
# File 'src/plugin/comarketing.rb', line 14

def find(iksnr)
  @app.registration(iksnr)
end

- (Object) prune_comarketing(except_pairs)



38
39
40
41
42
43
44
45
46
47
48
49
# File 'src/plugin/comarketing.rb', line 38

def prune_comarketing except_pairs
  table = {}
  except_pairs.each do |original, comarketing|
    table.store comarketing, original
  end
  @app.registrations.each do |iksnr, reg|
    if (com_reg = reg.comarketing_with) && table[iksnr] != com_reg.iksnr
      @app.update reg.pointer, :comarketing_with => nil
      @deleted += 1
    end
  end
end

- (Object) report



50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/plugin/comarketing.rb', line 50

def report
  fmt =  "Found                %3i Co-Marketing-Pairs\n"
  fmt << "of which             %3i were found in the Database\n"
  fmt << "New Connections:     %3i\n"
  fmt << "Deleted Connections: %3i\n\n"
  fmt << "The following        %3i Original/Comarketing-Pairs were not found in the Database:\n"
  txt = sprintf(fmt, @pairs.size, @found, @updated, @deleted, @not_found.size)
  @not_found.each { |original, comarketing|  
    txt << sprintf("%s\n -> %s\n\n", original, comarketing)
  }
  txt
end

- (Object) sequence_registrations(sequences, &block)



62
63
64
65
66
67
68
69
70
71
# File 'src/plugin/comarketing.rb', line 62

def sequence_registrations(sequences, &block)
  if(block)
    sequences = sequences.select(&block)
  end
  regs = _sequence_registrations(sequences)
  if(regs.size > 1)
    regs = _sequence_registrations(sequences.select { |seq| seq.active? })
  end
  regs
end

- (Object) update(agent = Mechanize.new)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'src/plugin/comarketing.rb', line 76

def update(agent=Mechanize.new)
  @deleted = 0
  @updated = 0
  @found = 0
  @not_found = []
  page = agent.get SOURCE_URI
  link = page.links.find do |node|
    /Sortiert\s*nach\s*Basis/iu.match node.attributes['title']
  end or raise "unable to identify url for Co-Marketing-data"
  url = "http://www.swissmedic.ch/#{link.attributes['href']}"
  @pairs = COMARKETING_PARSER.get_pairs(url)
  @pairs.each { |pair|
    update_pair(*pair)
  }
  prune_comarketing @pairs
end

- (Object) update_pair(original_iksnr, comarketing_iksnr)



92
93
94
95
96
97
98
99
100
# File 'src/plugin/comarketing.rb', line 92

def update_pair(original_iksnr, comarketing_iksnr)
  if((original = find(original_iksnr)) \
     && (comarketing = find(comarketing_iksnr)))
    @found += 1
    update_registration(original, comarketing)
  else
    @not_found.push([original_iksnr, comarketing_iksnr])
  end
end

- (Object) update_registration(original, comarketing)



101
102
103
104
105
106
107
108
# File 'src/plugin/comarketing.rb', line 101

def update_registration(original, comarketing)
  unless((old = comarketing.comarketing_with) \
         && old.pointer == original.pointer)
    @updated += 1
    @app.update(comarketing.pointer, 
                {:comarketing_with => original.pointer}, :swissmedic)
  end
end