Class: LabWhereClient::LabWhere

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

Instance Method Summary collapse

Instance Method Details

#base_urlObject


7
8
9
# File 'lib/lab_where_client.rb', line 7

def base_url
  configatron.fetch(:labwhere_api)
end

#get(instance, target) ⇒ Object


25
26
27
28
29
# File 'lib/lab_where_client.rb', line 25

def get(instance, target)
  parse_json(RestClient.get(path_to(instance, target)))
rescue Errno::ECONNREFUSED => e
  raise LabwhereException.new(e), 'LabWhere service is down', e.backtrace
end

#parse_json(str) ⇒ Object


17
18
19
20
21
22
23
# File 'lib/lab_where_client.rb', line 17

def parse_json(str)
  return nil if str == 'null'

  JSON.parse(str)
rescue JSON::ParserError => e
  raise LabwhereException.new(e), 'LabWhere is returning unexpected content', e.backtrace
end

#path_to(instance, target) ⇒ Object

Raises:


11
12
13
14
15
# File 'lib/lab_where_client.rb', line 11

def path_to(instance, target)
  raise LabwhereException, 'LabWhere service URL not set' if base_url.nil?

  [base_url, instance.endpoint, target].compact.join('/')
end

#post(instance, target, payload) ⇒ Object


31
32
33
34
35
36
37
# File 'lib/lab_where_client.rb', line 31

def post(instance, target, payload)
  parse_json(RestClient.post(path_to(instance, target), payload))
rescue Errno::ECONNREFUSED => e
  raise LabwhereException.new(e), 'LabWhere service is down', e.backtrace
rescue RestClient::UnprocessableEntity => e
  return parse_json(e.response)
end

#put(instance, target, payload) ⇒ Object


39
40
41
42
43
# File 'lib/lab_where_client.rb', line 39

def put(instance, target, payload)
  parse_json(RestClient.put(path_to(instance, target), payload))
rescue Errno::ECONNREFUSED => e
  raise LabwhereException.new(e), 'LabWhere service is down', e.backtrace
end