view · edit · sidebar · attach · print · history

20110809-migel-search-oddb_org

<< | Index | >>


  1. Develop migel search function
  2. Develop migel search method in SwissindexNonPharma class

Goal/Estimate/Evaluation
  • Migel search function (show data) / 80% / 80%
Milestones
  • Get a migel search result
  • Put the data into a view (state) class
Summary
Commits
ToDo Tomorrow
  • How to make stripe, snapback, subheading (for Höchstvergütungsbetrag)
  • Sort, import functions

Develop migel search function

git status

# Changed but not updated:
#       modified:   ext/swissindex/src/swissindex.rb
#       modified:   src/state/global.rb
#       modified:   src/util/validator.rb
#       modified:   src/view/migel/result.rb
# Untracked files:
#       src/state/migel/test.rb
#       src/view/migel/test.rb

Next

  • How to make a relation between data and state class

Experiment

  • src/state/migel/test.rb
 #!/usr/bin/env ruby
 #  ODDB::State::Migel:Test -- oddb.org -- 09.08.2011 -- mhatakeyama@ywesee.com

 require 'state/global_predefine'
 require 'view/migel/test' 

 module ODDB
   module State
     module Migel
 class TestModel
   def masa
     'masamasa'
   end
 end
 class Test < State::Migel::Global
   VIEW = View::Migel::Test
   def init
     @model = [TestModel.new]
   end
 end
     end
   end
 end
  • src/view/migel/test.rb
 #!/usr/bin/env ruby
 # ODDB::View::Migel::Test -- oddb.org -- 09.08.2011 -- mhatakeyama@ywesee.com

 require 'htmlgrid/list'

 module ODDB
   module View
     module Migel

 class List < HtmlGrid::List
   def init
     @components = {
       [0,0]   =>  :masa,
       [1,0]   =>  'hata',
     }
     super
   end
   def compose_list(model=@model, offset=[0,0])
     super(model, offset)
   end
 end
 class ResultComposite < HtmlGrid::Composite
   COMPONENTS = {
     [0,0] => List
   }
 end
 class Test < View::PrivateTemplate
   CONTENT = ResultComposite
 end
     end
   end
 end

Access

Result

Note

  • The value (symbol) of View::@components (Hash) is correspoding to a method defined in State::@model
  • Usually, State@model is set when a State instance is created in a event (method) of src/state/global.rb

Next

  • Update the migel search method in SwissindexNonPharma class

Develop migel search method in SwissindexNonPharma class

Experiment

  • ext/swissindex/src/swissindex.rb
  def search_migel_table(code, query_key = 'Pharmacode')
    agent = Mechanize.new
    try_time = 3
    begin
      agent.get(@base_url + query_key + '=' + code)
      count = 100
      table = []
      line  = []
        agent.page.search('td').each_with_index do |td, i|
          text = td.inner_text.chomp.strip
          if text.is_a?(String) && text.length == 7 && text.match(/\d{7}/)
            unless table
              table = []
            else
              table << line
              line = []
            end
            count = 0
          end
          if count < 7
            text = text.split(/\n/)[1] || text.split(/\n/)[0]
            text = text.gsub(/\302\240/, '').strip if text
            line << text
            count += 1
          end
        end
        table << line
        table.shift
      table
    rescue StandardError, Timeout::Error => err
      if try_time > 0
        puts err
        puts err.backtrace
        puts
        puts "retry"
        sleep 10
        agent = Mechanize.new
        try_time -= 1
        retry
      else
        return []
      end
    end
  end
  • src/plugin/swissindex.rb
    def search_migel_table(migel_code)
      table = nil
      open('/home/masa/work/log.dat', 'w') do |out|
        SWISSINDEX_NONPHARMA_SERVER.session(ODDB::Swissindex::SwissindexNonpharma) do |swissindex|
          table = swissindex.search_migel_table(migel_code, 'MiGelCode')
        end
        out.print table.pretty_inspect
      end
      table.length.to_s + ' lines'
    end

Run

  • bin/oddbd
  • ext/swissindex/bin/swissindex_nonpharmad
  • bin/admin
ch.oddb> ODDB::SwissindexNonpharmaPlugin.new(self).search_migel_table '50704001'
-> 157 lines

Result

[["4674132",
  "3M FUTURO Daumenschiene L/XL",
  "3M Consumer Health Care",
  "",
  "52.60",
  "1",
  "52.60"],
...

Next

  • How to set the table data as State@model variable

Experiment

  • src/util/validator.rb
    STRINGS = [
...
      :migel_code
    ]
  • src/state/global.rb
      class MigelSearchedData
        def initialize(session, migel_code)
          plugin = ODDB::SwissindexNonpharmaPlugin.new(session.app)
          @table = plugin.search_migel_table(migel_code)
          @line = -1
        end
        def empty?
          nil
        end
        def each_with_index
          @table.each do |line|
            yield self
          end
        end
        def masa
          @table[@line+=1][0]
        end
      end
      def test
        migel_code = @session.user_input(:migel_code)
        ODDB::State::Migel::Test.new(@session, MigelSearchedData.new(@session, migel_code))
      end

Access

Result

Note

  • I should not define the MigelSearchedData class in src/state/global.rb
  • Move it somewhere appropriate place later (for example, in src/model/migel)
  • @table[ @line+=1] should be updated, probably it is possible to replace it by using offset method

Experiment

  • src/custom/lookandfeelbase.rb
     DICTIONARIES = {
      'de'      =>    {
...
        :th_article_name          =>  'Artikelname',
        :th_companyname           =>  'Companyname',
        :th_ppha                  =>  'PPHA',
        :th_ppub                  =>  'PPUB',
        :th_factor                =>  'Faktor',
        :th_pzr                   =>  'PZR'
  • src/state/global.rb
      class MigelSearchedData
        def initialize(session, migel_code)
          plugin = ODDB::SwissindexNonpharmaPlugin.new(session.app)
          @table = plugin.search_migel_table(migel_code)
          @line = -1
        end
        def empty?
          nil
        end
        def each_with_index
          @table.each do |line|
            yield self
          end
        end
        def pharmacode
          @table[@line+=1][0]
        end
        def article_name
          @table[@line][1]
        end
        def companyname
          @table[@line][2]
        end
        def ppha
          @table[@line][3]
        end
        def ppub
          @table[@line][4]
        end
        def factor
          @table[@line][5]
        end
        def pzr
          @table[@line][6]
        end
      end
      def test
        migel_code = @session.user_input(:migel_code)
        ODDB::State::Migel::Test.new(@session, MigelSearchedData.new(@session, migel_code))
      end
  • src/state/migel/test.rb
#!/usr/bin/env ruby
#  ODDB::State::Migel:Test -- oddb.org -- 09.08.2011 -- mhatakeyama@ywesee.com

require 'state/global_predefine'
require 'view/migel/test'

module ODDB
  module State
    module Migel
class Test < State::Migel::Global
  VIEW = View::Migel::Test
end
    end
  end
end
  • src/view/migel/test.rb
#!/usr/bin/env ruby
# ODDB::View::Migel::Test -- oddb.org -- 08.08.2011 -- mhatakeyama@ywesee.com

require 'htmlgrid/list'

module ODDB
  module View
    module Migel

class List < HtmlGrid::List
  def init
    @components = {
      [0,0]   =>  :pharmacode,
      [1,0]   =>  :article_name,
      [2,0]   =>  :companyname,
      [3,0]   =>  :ppha,
      [4,0]   =>  :ppub,
      [5,0]   =>  :factor,
      [6,0]   =>  :pzr,
    }
    super
  end
  def compose_list(model=@model, offset=[0,0])
    super(model, offset)
  end
end
class ResultComposite < HtmlGrid::Composite
  COMPONENTS = {
    [0,0] => List
  }
end
class Test < View::PrivateTemplate
  CONTENT = ResultComposite
end
    end
  end
end
  • src/plugin/swissindex.rb
    def search_migel_table(migel_code)
      table = nil
      SWISSINDEX_NONPHARMA_SERVER.session(ODDB::Swissindex::SwissindexNonpharma) do |swissindex|
        table = swissindex.search_migel_table(migel_code, 'MiGelCode')
      end
      table
    end
  • ext/swissindex/src/swissindex.rb
  def search_migel_table(code, query_key = 'Pharmacode')
    # 'MiGelCode' is also available for query_key
    agent = Mechanize.new
    try_time = 3
    begin
      agent.get(@base_url + query_key + '=' + code)
      count = 100
      table = []
      line  = []
      agent.page.search('td').each_with_index do |td, i|
        text = td.inner_text.chomp.strip
        if text.is_a?(String) && text.length == 7 && text.match(/\d{7}/)
          unless table
            table = []
          else
            table << line
            line = []
          end
          count = 0
        end
        if count < 7
          text = text.split(/\n/)[1] || text.split(/\n/)[0]
          text = text.gsub(/\302\240/, '').strip if text
          line << text
          count += 1
        end
      end
      table << line
      table.shift
      table
    rescue StandardError, Timeout::Error => err
      if try_time > 0
        puts err
        puts err.backtrace
        puts
        puts "retry"
        sleep 10
        agent = Mechanize.new
        try_time -= 1
        retry
      else
        return []
      end
    end
  end
  • src/util/validator.rb
    EVENTS = [
...
      :test
...
    STRINGS = [
...
      :migel_code
...

Run

  • bin/oddbd
  • ext/swissindex/bin/swissindex_nonpharmad

Access

Result

Next

  • Move the MigelSearchedData class definition to src/model/migel/
  • Make a header and footer

Experiment

  • src/model/migel/searched_table.rb
#!/usr/bin/env ruby
# encoding: utf-8
# ODDB::Migel::SearchedTable -- oddb.org -- 09.08.2011 -- mhatakeyama@ywesee.com

require 'plugin/swissindex'

module ODDB
  module Migel

class SearchedTable
  class Record
    attr_reader :pharmacode, :article_name, :companyname, :ppha, :ppub, :factor, :pzr
    def initialize(record)
      @pharmacode, @article_name, @companyname, @ppha, @ppub, @factor, @pzr = *record
    end
  end
  def initialize(session, migel_code)
    plugin = ODDB::SwissindexNonpharmaPlugin.new(session.app)
    @table = plugin.search_migel_table(migel_code).map do |record|
      Record.new(record)
    end
  end
  def empty?
    @table.empty?
  end
  def sort_by
    @table.sort_by do |record|
      yield record
    end
  end
  def each_with_index
    @table.each do |record|
      yield record
    end
  end
end

  end
end

Access

Result

  • success

Next

  • Header, footer, css, snapback, subheading (for Höchstvergütungsbetrag)
  • Sort function

Final today

view · edit · sidebar · attach · print · history
Page last modified on August 09, 2011, at 04:55 PM