Class: FinAppsCore::REST::Resources

Inherits:
Object
  • Object
show all
Includes:
Utils::Loggeable, Utils::Validatable
Defined in:
lib/finapps_core/rest/resources.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Validatable

#nil_or_empty?, #not_blank

Methods included from Utils::Loggeable

#logger

Constructor Details

#initialize(client) ⇒ FinAppsCore::REST::Resources

Parameters:

  • client (FinAppsCore::REST::Client)


17
18
19
20
21
# File 'lib/finapps_core/rest/resources.rb', line 17

def initialize(client)
  not_blank(client, :client)
  @client = client
  @logger = client.logger if client.respond_to?(:logger)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/finapps_core/rest/resources.rb', line 13

def client
  @client
end

Instance Method Details

#create(params = {}, path = nil) ⇒ Object



32
33
34
# File 'lib/finapps_core/rest/resources.rb', line 32

def create(params = {}, path = nil)
  send_request path, :post, params
end

#destroy(id = nil, path = nil) ⇒ Object



40
41
42
# File 'lib/finapps_core/rest/resources.rb', line 40

def destroy(id = nil, path = nil)
  send_request_for_id path, :delete, id
end

#end_pointObject



44
45
46
# File 'lib/finapps_core/rest/resources.rb', line 44

def end_point
  self.class.name.split('::').last.downcase
end

#list(path = nil) ⇒ Object



23
24
25
26
# File 'lib/finapps_core/rest/resources.rb', line 23

def list(path = nil)
  path = end_point.to_s if path.nil?
  send_request path, :get
end

#resource_path(id) ⇒ Object



53
54
55
56
# File 'lib/finapps_core/rest/resources.rb', line 53

def resource_path(id)
  not_blank id, :id
  "#{end_point}/#{ERB::Util.url_encode(id)}"
end

#send_request(path, method, params = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/finapps_core/rest/resources.rb', line 58

def send_request(path, method, params = {})
  path = end_point if path.nil?
  logger.debug "#{self.class.name}##{__method__} => path: #{path} params: #{params}"

  client.send_request path, method, params
end

#send_request_for_id(path, method, id) ⇒ Object



48
49
50
51
# File 'lib/finapps_core/rest/resources.rb', line 48

def send_request_for_id(path, method, id)
  path = resource_path(id) if path.nil?
  send_request path, method
end

#show(id = nil, path = nil) ⇒ Object



28
29
30
# File 'lib/finapps_core/rest/resources.rb', line 28

def show(id = nil, path = nil)
  send_request_for_id path, :get, id
end

#update(params = {}, path = nil) ⇒ Object



36
37
38
# File 'lib/finapps_core/rest/resources.rb', line 36

def update(params = {}, path = nil)
  send_request path, :put, params
end