Class: LabWhereClient::LabWhere

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

Overview

rubocop:todo Style/Documentation

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


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

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
25
# 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


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

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
  return parse_json(e.response)
end

#put(instance, target, payload) ⇒ Object


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

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