Class: ODDB::LppvPlugin

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

Defined Under Namespace

Classes: PriceUpdate

Constant Summary

LPPV_HOST =
'www.lppa.ch'
LPPV_PATH =
'/index/%s.htm'

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Plugin

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

Methods included from HttpFile

#http_body, #http_file

Constructor Details

- (LppvPlugin) initialize(app)

A new instance of LppvPlugin



81
82
83
84
85
86
# File 'src/plugin/lppv.rb', line 81

def initialize(app)
  super
  @updated_packages = []
  @packages_with_sl_entry = []
  @not_updated_chars = []
end

Instance Attribute Details

- (Object) updated_packages (readonly)

Returns the value of attribute updated_packages



80
81
82
# File 'src/plugin/lppv.rb', line 80

def updated_packages
  @updated_packages
end

Instance Method Details

- (Object) do_price_update(package, price)



150
151
152
153
154
155
156
157
158
159
160
# File 'src/plugin/lppv.rb', line 150

def do_price_update(package, price)
  price_obj = PriceUpdate.new(package, price)
  if(price_obj.changed? || !package.lppv)
    args = {
      :price_public => price,
      :lppv          => true,
    }
    @app.update(package.pointer, args, :lppv)
    @updated_packages.push(price_obj)
  end
end

- (Object) get_prices(char, http)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'src/plugin/lppv.rb', line 96

def get_prices(char, http)
  writer = LppvWriter.new
  path = sprintf(LPPV_PATH, char)
  response = http.get(path)
  formatter = HtmlFormatter.new(writer)
  parser = HtmlParser.new(formatter)
  parser.feed(response.body)
  if writer.prices.empty?
    @not_updated_chars.push(char)
  end
  origin = "http://#{LPPV_HOST}#{path}"
  prices = writer.prices
  prices.each do |key, price| price.origin = origin end
  prices
end

- (Object) report



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'src/plugin/lppv.rb', line 127

def report
  ups, downs = @updated_packages.sort_by { |price| 
    price.package.name }.partition { |price| price.up? } 
    lines = [
      "Downloaded Prices: #{@prices.size}",
      "Updated Packages: #{@updated_packages.size}",
      nil,
      "Packages with SL-Entry: #{@packages_with_sl_entry.size}",
      nil,
      "The following Packages experienced a Price RAISE:",
      sprintf("%-20s  %-20s  %-20s %-s", 
              "IKS-Number", "Old Price", "New Price", "Package Name"),
    ]
    ups.each { |price| lines.concat(price.report_lines) }
    lines.concat([ nil,
      "The following Packages experienced a Price CUT:",
      sprintf("%-20s  %-20s  %-20s %-s,", 
              "IKS-Number", "Old Price", "New Price", "Package Name"),
    ])
    downs.each { |price| lines.concat(price.report_lines) }
    lines.push("Not updated were: " << @not_updated_chars.join(', '))
    lines.flatten.join("\n")
end

- (Object) update(range = 'A'..'Z')



87
88
89
90
91
92
93
94
95
# File 'src/plugin/lppv.rb', line 87

def update(range = 'A'..'Z')
  @prices = {}
  Net::HTTP.new(LPPV_HOST).start { |http| 
    range.each { |char| 
      @prices.update(get_prices(char, http))
    }
  }
  update_packages(@prices.dup)
end

- (Object) update_package(package, data)



111
112
113
114
115
116
117
118
119
120
121
# File 'src/plugin/lppv.rb', line 111

def update_package(package, data)
  if(price_dat = data.delete(package.pharmacode))
    if(package.sl_entry && package.price_public)
      @packages_with_sl_entry.push(package)
    else
      do_price_update(package, price_dat)
    end
  elsif(package.lppv && package.data_origin(:lppv) == :lppv)
    @app.update(package.pointer, {:lppv => false}, :lppv)
  end
end

- (Object) update_packages(data)



122
123
124
125
126
# File 'src/plugin/lppv.rb', line 122

def update_packages(data)
  @app.each_package { |package| 
    update_package(package, data)
  }
end