Class: PsengEkgClient::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pseng_ekg_client.rb

Constant Summary collapse

VALID_ACTIONS =
%w(index show create update destroy)
VALID_RECORD_TYPES =
%w(application task)

Instance Method Summary collapse

Constructor Details

#initialize(base_uri) ⇒ Api

Returns a new instance of Api.



10
11
12
# File 'lib/pseng_ekg_client.rb', line 10

def initialize(base_uri)
  self.class.base_uri base_uri
end

Instance Method Details

#send_message(record_type, action, payload, id = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pseng_ekg_client.rb', line 14

def send_message(record_type, action, payload, id = nil)
  raise 'Invalid Action' unless VALID_ACTIONS.include?(action)
  raise 'Invalid Record Type' unless VALID_RECORD_TYPES.include?(record_type)

  options = {
    body: payload.to_json,
    headers: { 'Content-Type' => 'application/json' }
  }

  response = case action
  when 'index'
    self.class.get("/#{record_type}s.json", options)
  when 'show'
    self.class.get("/#{record_type}s/#{id}.json", options)
  when 'create'
    self.class.post("/#{record_type}s.json", options)
  when 'update'
    self.class.patch("/#{record_type}s/#{id}.json", options)
  when 'destroy'
    self.class.delete("/#{record_type}s/#{id}.json", options)
  end
  response
end