Blame view

lib/syspro/api_resource.rb 1021 Bytes
dc8aa5b6   Joe Weakley   Rubocop corrections
1
2
3
4
  # frozen_string_literal: true
  
  require 'syspro/syspro_object'
  require 'syspro/api_operations/request'
db76748d   Isaac Lewis   cop a bunch of St...
5
6
  
  module Syspro
3d0157a5   Isaac Lewis   add logoff
7
8
    class ApiResource < SysproObject
      include Syspro::ApiOperations::Request
db76748d   Isaac Lewis   cop a bunch of St...
9
10
  
      def self.class_name
dc8aa5b6   Joe Weakley   Rubocop corrections
11
        name.split('::')[-1]
db76748d   Isaac Lewis   cop a bunch of St...
12
13
14
      end
  
      def self.resource_url
3d0157a5   Isaac Lewis   add logoff
15
        if self == ApiResource
dc8aa5b6   Joe Weakley   Rubocop corrections
16
          raise NotImplementedError, 'APIResource is an abstract class.  You should perform actions on its subclasses (Charge, Customer, etc.)'
db76748d   Isaac Lewis   cop a bunch of St...
17
        end
3d0157a5   Isaac Lewis   add logoff
18
        "/#{CGI.escape(class_name.downcase)}"
db76748d   Isaac Lewis   cop a bunch of St...
19
20
      end
  
7d46a01e   Isaac Lewis   handle errors ref...
21
22
23
24
25
26
      def handle_errors(resp)
        body = resp[0].http_body
        raise AuthenticationError, body if body =~ /^(ERROR: The supplied UserID is invalid.)/
        raise SysproError, body if body =~ /^(ERROR)/
      end
  
db76748d   Isaac Lewis   cop a bunch of St...
27
28
29
30
31
32
33
34
35
36
37
38
39
      def refresh
        resp, opts = request(:get, resource_url, @retrieve_params)
        initialize_from(resp.data, opts)
      end
  
      def self.retrieve(id, opts = {})
        opts = Util.normalize_opts(opts)
        instance = new(id, opts)
        instance.refresh
        instance
      end
    end
  end