Class: PFS::Client

Inherits:
Object
  • Object
show all
Includes:
API::Services
Defined in:
lib/pfs/client.rb

Constant Summary collapse

BASE_URL =
"https://api.prepaidfinancialservices.com/finac/api"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Services

#accounts, #authentication, #statements, #transactions, #transfers

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
# File 'lib/pfs/client.rb', line 17

def initialize(options = {})
  @base_url = options[:base_url] || BASE_URL
  @origin = options[:origin] || default_origin
  @proxy = options[:proxy]
  @username = options[:username] || ENV["PFS_USERNAME"]
  @password = options[:password] || ENV["PFS_PASSWORD"]
  @token = nil
  @logger_enabled = options[:logger_enabled].nil? ? true : options[:logger_enabled]
  @services = {}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



15
16
17
# File 'lib/pfs/client.rb', line 15

def base_url
  @base_url
end

#logger_enabledObject (readonly)

Returns the value of attribute logger_enabled.



15
16
17
# File 'lib/pfs/client.rb', line 15

def logger_enabled
  @logger_enabled
end

#originObject (readonly)

Returns the value of attribute origin.



15
16
17
# File 'lib/pfs/client.rb', line 15

def origin
  @origin
end

#proxyObject (readonly)

Returns the value of attribute proxy.



15
16
17
# File 'lib/pfs/client.rb', line 15

def proxy
  @proxy
end

#tokenObject (readonly)

Returns the value of attribute token.



15
16
17
# File 'lib/pfs/client.rb', line 15

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/pfs/client.rb', line 15

def username
  @username
end

Instance Method Details

#add_auth_options!(options) ⇒ Object



102
103
104
105
106
# File 'lib/pfs/client.rb', line 102

def add_auth_options!(options)
  login! unless @token&.valid?

  options[:headers][:authorization] = "Bearer #{@token.access_token}"
end

#connectionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pfs/client.rb', line 28

def connection
  @connection ||= Faraday.new do |builder|
    builder.use Faraday::Response::RaiseError
    builder.response :json,
                     content_type: /\bjson$/,
                     preserve_raw: true,
                     parser_options: { symbolize_names: true }
    builder.proxy = @proxy if proxy
    if @logger_enabled
      builder.response :logger, nil, { headers: true, bodies: true } do |logger|
        logger.filter(/("password":)"(\w+)"/, '\1[FILTERED]')
      end
    end
    builder.adapter :net_http
  end
end

#default_originObject



49
50
51
# File 'lib/pfs/client.rb', line 49

def default_origin
  "PFS/#{PFS::VERSION} Ruby Client (Faraday/#{Faraday::VERSION})"
end

#execute(method, path, data = nil, options = {}) ⇒ Object



65
66
67
# File 'lib/pfs/client.rb', line 65

def execute(method, path, data = nil, options = {})
  request(method, path, data, options)
end

#get(path, options = {}) ⇒ Object



53
54
55
# File 'lib/pfs/client.rb', line 53

def get(path, options = {})
  execute :get, path, nil, options
end

#handle_request_error(error) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/pfs/client.rb', line 87

def handle_request_error(error)
  case error
  when Faraday::ClientError
    raise ClientError, error
  when Faraday::ServerError
    raise ServerError, error
  else
    raise Error, error
  end
end

#login!Object



108
109
110
# File 'lib/pfs/client.rb', line 108

def login!
  @token = authentication.(@username, @password)
end

#login_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/pfs/client.rb', line 98

def (path)
  path == "/Auth/Jwt"
end

#patch(path, data = nil, options = {}) ⇒ Object



61
62
63
# File 'lib/pfs/client.rb', line 61

def patch(path, data = nil, options = {})
  execute :patch, path, data, options
end

#post(path, data = nil, options = {}) ⇒ Object



57
58
59
# File 'lib/pfs/client.rb', line 57

def post(path, data = nil, options = {})
  execute :post, path, data, options
end

#request(method, path, data = nil, options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/pfs/client.rb', line 69

def request(method, path, data = nil, options = {})
  request_options = request_options(method, path, data, options)
  uri = "#{base_url}#{path}"

  begin
    connection.run_request(method, uri, request_options[:body], request_options[:headers])
  rescue StandardError => e
    handle_request_error(e)
  end
end

#request_options(_method, path, data, _options) ⇒ Object



80
81
82
83
84
85
# File 'lib/pfs/client.rb', line 80

def request_options(_method, path, data, _options)
  default_options.tap do |defaults|
    add_auth_options!(defaults) unless (path)
    defaults[:body] = JSON.dump(data) if data
  end
end

#resourcesObject



45
46
47
# File 'lib/pfs/client.rb', line 45

def resources
  @resources ||= {}
end