Sha256: 317efd36de9a7f8ff82cf979ea25cbc4ce5eaada52a5f1fcc70a98ade5f03ea4

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require_relative '../utils/loggeable'
require_relative '../utils/validatable'
require_relative '../utils/parameter_filter'

module FinAppsCore
  module REST
    class Resources # :nodoc:
      include FinAppsCore::Utils::Loggeable
      include FinAppsCore::Utils::Validatable
      include FinAppsCore::Utils::ParameterFilter
      require 'erb'

      attr_reader :client

      # @param [FinAppsCore::REST::Client] client
      # @return [FinAppsCore::REST::Resources]
      def initialize(client)
        not_blank(client, :client)
        @client = client
        @logger = client.logger if client.respond_to?(:logger)
      end

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

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

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

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

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

      protected

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

      private

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

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

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

        client.send_request path, method, params
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
finapps_core-3.0.6 lib/finapps_core/rest/resources.rb
finapps_core-3.0.5 lib/finapps_core/rest/resources.rb
finapps_core-3.0.3 lib/finapps_core/rest/resources.rb
finapps_core-3.0.1 lib/finapps_core/rest/resources.rb
finapps_core-2.1.2 lib/finapps_core/rest/resources.rb