Class: Qpid

Inherits:
Object
  • Object
show all
Defined in:
lib/qpid.rb

Defined Under Namespace

Classes: BadContentError, NoPatientError, SearchItem, TimeoutError

Constant Summary collapse

VERSION =
'0.1.3'
DEF_PORT =
80
DEF_TIMEOUT_INTERVAL =
300
DEF_QUERY_INTERVAL =
9.5
DEF_ERROR_TOLERANCE =
2
@@report_re =
/<pre>(.+)<\/pre>/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Qpid

Returns a new instance of Qpid.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/qpid.rb', line 49

def initialize(params = {})
  missing_params = []
  @user = params[:user] || params['user'] || missing_params << 'user' 
  @host = params[:host] || params['host'] || missing_params << 'host'
  @path = params[:path] || params['path'] || missing_params << 'path'
  if missing_params.length > 0
    raise ArgumentError, "Did not specify #{missing_params.join(', ')} to initialize QPID object"
  end
  @port = params[:port] || DEF_PORT
  @timeout_interval = params[:timeout_interval] || params['timeout_interval'] || DEF_TIMEOUT_INTERVAL
  @query_interval = params[:query_interval] || params['query_interval'] || DEF_QUERY_INTERVAL
  @error_tolerance = params[:error_tolerance] || params['error_tolerance'] || DEF_ERROR_TOLERANCE
  @client = params[:client] || params['client'] || HTTPClient.new
  if (params[:http_user] && params[:http_password])
    client.set_auth(nil, params[:http_user], params[:http_password])
  elsif (params['http_user'] && params['http_password'])
    client.set_auth(nil, params['http_user'], params['http_password'])
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



47
48
49
# File 'lib/qpid.rb', line 47

def client
  @client
end

#error_toleranceObject

Returns the value of attribute error_tolerance.



46
47
48
# File 'lib/qpid.rb', line 46

def error_tolerance
  @error_tolerance
end

#hostObject

Returns the value of attribute host.



46
47
48
# File 'lib/qpid.rb', line 46

def host
  @host
end

#pathObject

Returns the value of attribute path.



46
47
48
# File 'lib/qpid.rb', line 46

def path
  @path
end

#portObject

Returns the value of attribute port.



46
47
48
# File 'lib/qpid.rb', line 46

def port
  @port
end

#query_intervalObject

Returns the value of attribute query_interval.



46
47
48
# File 'lib/qpid.rb', line 46

def query_interval
  @query_interval
end

#timeout_intervalObject

Returns the value of attribute timeout_interval.



46
47
48
# File 'lib/qpid.rb', line 46

def timeout_interval
  @timeout_interval
end

#userObject

Returns the value of attribute user.



46
47
48
# File 'lib/qpid.rb', line 46

def user
  @user
end

Class Method Details

.daysago(num_days) ⇒ Object



175
176
177
# File 'lib/qpid.rb', line 175

def self.daysago(num_days)
  "daysago:#{num_days}"
end

.fromdate(date) ⇒ Object



167
168
169
# File 'lib/qpid.rb', line 167

def self.fromdate(date)
  "fromdate:" + date.strftime("\"%m/%d/%Y\"")
end

.last(num_items) ⇒ Object



171
172
173
# File 'lib/qpid.rb', line 171

def self.last(num_items)
  "last:#{num_items}"
end

.ondate(date) ⇒ Object



183
184
185
# File 'lib/qpid.rb', line 183

def self.ondate(date)
  todate(date) + " AND " + fromdate(date)
end

.title(string) ⇒ Object



179
180
181
# File 'lib/qpid.rb', line 179

def self.title(string)
  "title:#{string}"
end

.todate(date) ⇒ Object



163
164
165
# File 'lib/qpid.rb', line 163

def self.todate(date)
  "todate:" + date.strftime("\"%m/%d/%Y\"")
end

.type(type) ⇒ Object



159
160
161
# File 'lib/qpid.rb', line 159

def self.type(type)
  "type:#{type.to_s.upcase}"
end

Instance Method Details

#function_uri(function, mrn, params = {}) ⇒ Object



69
70
71
72
# File 'lib/qpid.rb', line 69

def function_uri(function, mrn, params = {})
  query = query({:function => function, :MRN => mrn}.merge(params))
  URI::HTTP.build(:host => host, :port => port, :path => path, :query => query )
end

#query(params = {}) ⇒ Object



74
75
76
77
78
# File 'lib/qpid.rb', line 74

def query(params = {})
  params[:USER] = user
  params[:xml] ||= 1
  params.map { |p| "#{CGI.escape(p[0].to_s)}=#{CGI.escape(p[1].to_s)}" }.join('&')
end

#reload(mrn) ⇒ Object

Causes QPID to re-update the patient’s record. Returns true when done, or false if the attempt times out.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/qpid.rb', line 100

def reload(mrn)
  start_time = Time.now
  error_count = error_tolerance
  task_description = "reloading patient #{mrn}"
  while true
    case resp = client.get_content(function_uri(:reload, mrn))
    when /<message>OK<\/message>/
      return true
    when /<message>(NEW|UPDATING|STALE)<\/message>/
      return false if Time.now - start_time > timeout_interval
      sleep query_interval
    when /<message>NO PATIENT<\/message>/
      raise Qpid::NoPatientError.new(mrn)
    else
      error_count = error_count - 1
      if error_count > 0
        sleep query_interval/2
      else
        raise Qpid::BadContentError.new(task_description, resp)
      end
    end
  end
end

#report(mrn, search_item) ⇒ Object

Actually get the report for a given patient (specified by MRN) referred to by the given Qpid::SearchItem object.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/qpid.rb', line 147

def report(mrn, search_item)
  case data = client.get_content(function_uri(:report, mrn, :type => search_item.type, :MID => search_item.mid))
  when /<message>NO PATIENT<\/message>/
    raise Qpid::NoPatientError.new(mrn)
  when /<table id=\"report\">/
    match = @@report_re.match(data)
    match[1]
  else
    raise Qpid::BadContentError.new("getting report for MRN #{mrn}, item [#{search_item.inspect}]", data)
  end
end

#search(mrn, *search_terms) ⇒ Object

Does a search in the patient’s medical record (as of the last time they were updated) and returns an array of Qpid::SearchItem objects which match the search



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/qpid.rb', line 126

def search(mrn, *search_terms)
  search_string = search_terms.join(' and ')
  task_description = "searching MRN #{mrn} with [#{search_string}]"
  case resp = client.get_content(function_uri(:search, mrn, :search => search_string))
  when /<message>NO PATIENT<\/message>/
    raise Qpid::NoPatientError.new(mrn)
  when /<searchlist>/
    begin
      SearchItem.parse(resp)
    rescue Exception => e
      raise Qpid::BadContentError.new(task_description, resp, e.inspect)
    end
  else
    raise Qpid::BadContentError.new(task_description, resp)
  end
end

#updatestatus(mrn) ⇒ Object

Get the time a patient’s record was last updated in QPID



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/qpid.rb', line 81

def updatestatus(mrn)
  case xml = client.get_content(function_uri(:updatestatus, mrn)) 
  when /<message>NO PATIENT<\/message>/
    raise Qpid::NoPatientError.new(mrn)
  when /<message>(?:NEW|UNKNOWN STATUS)<\/message>/
    nil
  when /<lastupdate>(.+)<\/lastupdate>/
    begin
      DateTime.parse($1)      
    rescue Exception => e
      raise Qpid::BadContentError.new("parsing time in update status for MRN #{mrn}", xml, e.inspect)
    end
  else
    raise Qpid::BadContentError.new("getting update status for MRN #{mrn}", xml)
  end
end