Class: LabWhereClient::LabWhere

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

Instance Method Summary collapse

Instance Method Details

#base_urlObject



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

def base_url
  configatron.fetch(:labwhere_api)
end

#get(instance, target) ⇒ Object



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

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

#parse_json(str) ⇒ Object



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

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:



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

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

  encoded_target = ERB::Util.url_encode(target)
  [base_url, instance.endpoint, encoded_target].compact.join('/')
end

#post(instance, target, payload) ⇒ Object



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

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

#put(instance, target, payload) ⇒ Object



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

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