Class: ODDB::Swissreg::Session

Inherits:
HttpSession
  • Object
show all
Defined in:
ext/swissreg/src/session.rb

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



12
13
14
15
# File 'ext/swissreg/src/session.rb', line 12

def initialize
	super('www.swissreg.ch')
	@http.read_timeout = 120 
end

Instance Method Details



16
17
18
19
20
21
22
23
# File 'ext/swissreg/src/session.rb', line 16

def extract_result_links(html)
   doc = Hpricot(html)
   path = "//a[@target='detail']/span[@title='zur Detailansicht']" 
   link = "/srclient/faces/jsp/spc/sr300.jsp?language=de&section=spc&id=%s"
   (doc/path).collect { |span|
     link % span.inner_html
   }
end

#get(url, *args) ⇒ Object



24
25
26
27
28
# File 'ext/swissreg/src/session.rb', line 24

def get(url, *args)
  res = super
  @referer = url
  res
end

#get_detail(url) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'ext/swissreg/src/session.rb', line 29

def get_detail(url)
	response = get(url)
	writer = DetailWriter.new
	formatter = ODDB::HtmlFormatter.new(writer)
	parser = ODDB::HtmlParser.new(formatter)
	parser.feed(response.body)
	writer.extract_data
rescue Timeout::Error
	{}
end

#get_headersObject



39
40
41
42
43
44
45
# File 'ext/swissreg/src/session.rb', line 39

def get_headers
   hdrs = super
   if(@cookie_id)
     hdrs.push(['Cookie', @cookie_id])
   end
   hdrs
end

#get_result_list(substance) ⇒ Object



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
73
74
75
76
# File 'ext/swissreg/src/session.rb', line 46

def get_result_list(substance)
   response = get("/srclient/")
   update_cookie(response)
   response = get(response['location'])
   data = [
     [ 'id_swissreg_SUBMIT', '1' ],
     [ 'id_swissreg:_idcl', 
      'id_swissreg_sub_nav_ipiNavigation_item10'],
     ["javax.faces.ViewState", view_state(response)],
   ]
   url = "/srclient/faces/jsp/start.jsp"
	response = post(url, data)
   update_cookie(response)
	criteria = [
     ["id_swissreg:mainContent:id_txf_title", "%s*" % substance],
     ["id_swissreg:mainContent:id_txf_basic_pat_no", ""],
     ["id_swissreg:mainContent:id_txf_spc_no", ""],
     ["id_swissreg:mainContent:sub_fieldset:id_submit", "suchen"],
     ["id_swissreg:_link_hidden_", ""],
     ["id_swissreg_SUBMIT", "1"],
     ["id_swissreg:_idcl", ""],
     ["autoScroll", "0"],
     ["javax.faces.ViewState", view_state(response)],
	]
   url = "/srclient/faces/jsp/spc/sr1.jsp"
	response = post(url, criteria)
   update_cookie(response)
	extract_result_links(response.body)
rescue Timeout::Error
	[]
end

#post(url, *args) ⇒ Object



77
78
79
80
81
# File 'ext/swissreg/src/session.rb', line 77

def post(url, *args)
  res = super
  @referer = url
  res
end


82
83
84
85
86
# File 'ext/swissreg/src/session.rb', line 82

def update_cookie(response)
  if(hdr = response['set-cookie'])
    @cookie_id = hdr[/^[^;]+/u]
  end
end

#view_state(response) ⇒ Object



87
88
89
90
91
# File 'ext/swissreg/src/session.rb', line 87

def view_state(response)
  if match = /javax.faces.ViewState.*?value="([^"]+)"/u.match(response.body)
    match[1]
  end
end