Class: ODDB::MedData::Session

Inherits:
HttpSession show all
Defined in:
ext/meddata/src/session.rb

Constant Summary

FORM_KEYS =
{
  :partner => [
    [:name,  'txtSearchName'],
    [:country, 'ddlSearchCountry'],
    [:plz, 'txtSearchZIP'],
    [:city,  'txtSearchCity'],
    [:state, 'ddlSearchStates'],
    [:functions, 'ddlSearchFunctions'],
    [:ean, 'txtSearchEAN'],
  ],
  :product => [
    [:name,  'txtSearchProductName'],
    [:ean, 'txtSearchEAN'],
    [:pharmacode, 'txtSearchPharmacode'],
    [:company, 'txtSearchProducer'],
  ],
  :refdata => [
    [:name,  'txtSearchProductName'],
    [:ean, 'txtSearchEAN'],
    [:company, 'txtSearchProducer'],
  ],
}
HTTP_PATHS =
{
  :partner => '/refdata_wa_medwin/frmSearchPartner.aspx?lang=de',
  :product => '/refdata_wa_medwin/frmSearchProduct.aspx?lang=de',
  :refdata => '/refdata_wa/frmSearchProduct.aspx?lang=de'
}
DETAIL_KEYS =
{
  :partner => "DgMedwinPartner",
  :product => "DgMedrefProduct",
  :refdata => "DgMedrefProduct",
}
SERVERS =
{
  :partner  => 'www.medwin.ch',
  :product  => 'www.medwin.ch',
  :refdata  => 'www.refdata.ch',
}

Constants inherited from HttpSession

HTTP_CLASS, RETRIES, RETRY_WAIT

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from HttpSession

#get, #get_headers, #post, #post_body

Constructor Details

- (Session) initialize(search_type = :partner, server = SERVERS[search_type])

A new instance of Session



52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/meddata/src/session.rb', line 52

def initialize(search_type=:partner, server=SERVERS[search_type])
  @http_path = HTTP_PATHS[search_type]
  @form_keys = FORM_KEYS[search_type]
  @detail_key = DETAIL_KEYS[search_type]
  super(server)
  resp = get '/'
  resp = get @http_path
  handle_resp!(resp)
rescue Timeout::Error => err
  err.message << " - #{server} is not responding"
  raise err
end

Instance Attribute Details

- (Object) detail_key

Returns the value of attribute detail_key



51
52
53
# File 'ext/meddata/src/session.rb', line 51

def detail_key
  @detail_key
end

- (Object) form_keys

Returns the value of attribute form_keys



51
52
53
# File 'ext/meddata/src/session.rb', line 51

def form_keys
  @form_keys
end

- (Object) http_path

Returns the value of attribute http_path



51
52
53
# File 'ext/meddata/src/session.rb', line 51

def http_path
  @http_path
end

Instance Method Details

- (Object) detail_html(ctl)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'ext/meddata/src/session.rb', line 64

def detail_html(ctl)
  hash = post_hash({}, ctl)
  tries = 3
  begin
    resp = post(@http_path, hash)
    resp.body
  rescue Errno::ECONNRESET
    if(tries > 0)
      tries -= 1
      sleep(3 - tries)
      retry
    else
      raise
    end
  end
end

- (Object) get_result_list(criteria)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/meddata/src/session.rb', line 95

def get_result_list(criteria)
  hash = post_hash(criteria)
  resp = post(self.http_path, hash)
  @viewstate = handle_resp!(resp)
  resp.body
rescue Errno::ENETUNREACH
  retries ||= 3
  if retries > 0
    retries -= 1
    sleep 60 # wait a minute for the network to recover
    retry
  else
    raise
  end
rescue RuntimeError => err
  if /InternalServerError/u.match err.message
    require 'pp'
    puts "error for criteria: #{criteria.pretty_inspect}"
    puts "... post_data: #{hash.pretty_inspect}"
    retries ||= 3
    if retries > 0
      retries -= 1
      sleep 600 # wait 10 minutes for the server to recover
      retry
    else
      raise
    end
  else
    raise
  end
end

- (Object) handle_resp!(resp)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/meddata/src/session.rb', line 80

def handle_resp!(resp)
  @cookie_header = resp["set-cookie"]
  body = resp.body
  if(match = /VIEWSTATE.*?value="([^"]+)"/u.match(body))
    @viewstate = match[1]
  else
    @viewstate = nil
  end
  if(match = /EVENTVALIDATION.*?value="([^"]+)"/u.match(body))
    @eventvalidation = match[1]
  else
    @eventvalidation = nil
  end
  @viewstate
end

- (Object) post_hash(criteria, ctl = nil)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/meddata/src/session.rb', line 126

def post_hash(criteria, ctl=nil)
  data = if(ctl)
    [['__EVENTTARGET',
      "#@detail_key:#{ctl}:ctl00"],
      ['__EVENTARGUMENT', ''],
    ]
  else
    [
      ['__EVENTTARGET', ''],
      ['__EVENTARGUMENT', ''],
      ['btnSearch', 'Suche'],
    ]
  end
  if(@viewstate)
    data.push(['__VIEWSTATE', @viewstate])
  end
  if @eventvalidation
    data.push(['__EVENTVALIDATION', @eventvalidation])
  end
  @form_keys.each { |key, new_key|
    if(val = criteria[key])
      data.push([new_key, CGI.escape(val).tr('+', ' ')])
    end
  }
  data.push(['hiddenlang',  'de'])
  data
end

- (Object) post_headers



153
154
155
156
157
158
159
# File 'ext/meddata/src/session.rb', line 153

def post_headers
  headers = super
  if(@cookie_header)
    headers.push(['Cookie', @cookie_header])
  end
  headers
end