Class: SynapsePayRest::Users
- Inherits:
-
Object
- Object
- SynapsePayRest::Users
- Defined in:
- lib/synapse_pay_rest/api/users.rb
Overview
Wrapper class for /users endpoints
Constant Summary collapse
- VALID_QUERY_PARAMS =
TODO:
Should refactor this to HTTPClient
Valid optional args for #get
[:query, :page, :per_page, :full_dehydrate].freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#attach_file(user_id:, file_path:) ⇒ Object
deprecated
Deprecated.
Use #update with KYC 2.0 payload instead.
-
#attach_file_with_file_type(user_id:, file_path:, file_type:) ⇒ Object
deprecated
Deprecated.
Use #update with KYC 2.0 payload instead.
-
#create(payload:) ⇒ Hash
Sends a POST request to /users endpoint to create a new user, and returns the response.
-
#encode_attachment(file_path:, file_type: nil) ⇒ String
Converts a file to base64 for use in payloads for adding physical documents.
-
#get(user_id: nil, **options) ⇒ Hash
Sends a GET request to /users endpoint and returns the response.
-
#initialize(client) ⇒ Users
constructor
A new instance of Users.
-
#refresh(user_id:, payload:) ⇒ Hash
Sends a POST request to /oauth/:user_id endpoint to obtain a new oauth key and update the client’s headers, and returns the response.
-
#update(user_id:, payload:) ⇒ Hash
(also: #answer_kba, #add_doc)
Sends a PATCH request to /users endpoint, updating the current user, which can also include adding/updating user CIP documents, and returns the response.
Constructor Details
#initialize(client) ⇒ Users
Returns a new instance of Users.
17 18 19 |
# File 'lib/synapse_pay_rest/api/users.rb', line 17 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ SynapsePayRest::HTTPClient
14 15 16 |
# File 'lib/synapse_pay_rest/api/users.rb', line 14 def client @client end |
Instance Method Details
#attach_file(user_id:, file_path:) ⇒ Object
Use #update with KYC 2.0 payload instead.
Detects the file type of the file and calls #attach_file_with_file_type on it.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/synapse_pay_rest/api/users.rb', line 136 def attach_file(user_id:, file_path:) warn caller.first + " DEPRECATION WARNING: #{self.class}##{__method__} is deprecated. Use #update with encode_attachment instead." file_contents = open(file_path) { |f| f.read } content_types = MIME::Types.type_for(file_path) file_type = content_types.first.content_type if content_types.any? if file_type.nil? raise('File type not found. Use attach_file_with_file_type(user_id: <user_id>, file_path: <file_path>, file_type: <file_type>)') else attach_file_with_file_type(user_id: user_id, file_path: file_path, file_type: file_type) end end |
#attach_file_with_file_type(user_id:, file_path:, file_type:) ⇒ Object
Use #update with KYC 2.0 payload instead.
Converts a file to base64 and sends it to the API using deprecated KYC 1.0 call.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/synapse_pay_rest/api/users.rb', line 155 def attach_file_with_file_type(user_id:, file_path:, file_type:) warn caller.first + " DEPRECATION WARNING: #{self.class}##{__method__} is deprecated. Use #update with encode_attachment instead." path = user_path(user_id: user_id) file_contents = open(file_path) { |f| f.read } encoded = Base64.encode64(file_contents) mime_padding = "data:#{file_type};base64," = mime_padding + encoded payload = { 'doc' => { 'attachment' => } } client.patch(path, payload) end |
#create(payload:) ⇒ Hash
Sends a POST request to /users endpoint to create a new user, and returns the response.
HTTP response from API
60 61 62 |
# File 'lib/synapse_pay_rest/api/users.rb', line 60 def create(payload:) client.post(user_path, payload) end |
#encode_attachment(file_path:, file_type: nil) ⇒ String
Converts a file to base64 for use in payloads for adding physical documents.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/synapse_pay_rest/api/users.rb', line 113 def (file_path:, file_type: nil) # try to find file_type if file_type.nil? content_types = MIME::Types.type_for(file_path) file_type = content_types.first.content_type if content_types.any? end # if file_type not found in previous step if file_type.nil? raise('File type not found. Specify a file_type argument.') end file_contents = open(file_path) { |f| f.read } encoded = Base64.encode64(file_contents) mime_padding = "data:#{file_type};base64," mime_padding + encoded end |
#get(user_id: nil, **options) ⇒ Hash
Probably should use CGI or RestClient’s param builder instead of
Sends a GET request to /users endpoint and returns the response. Queries a specific user_id if user_id supplied, else queries all users.
HTTP response from API
rolling our own, probably error-prone and untested version github.com/rest-client/rest-client#usage-raw-url
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/synapse_pay_rest/api/users.rb', line 39 def get(user_id: nil, **) path = user_path(user_id: user_id) params = VALID_QUERY_PARAMS.map do |p| [p] ? "#{p}=#{[p]}" : nil end.compact path += '?' + params.join('&') if params.any? client.get(path) end |
#refresh(user_id:, payload:) ⇒ Hash
Sends a POST request to /oauth/:user_id endpoint to obtain a new oauth key and update the client’s headers, and returns the response
HTTP response from API
75 76 77 78 79 80 |
# File 'lib/synapse_pay_rest/api/users.rb', line 75 def refresh(user_id:, payload:) path = "/oauth/#{user_id}" response = client.post(path, payload) client.update_headers(oauth_key: response['oauth_key']) if response['oauth_key'] response end |
#update(user_id:, payload:) ⇒ Hash Also known as: answer_kba, add_doc
Sends a PATCH request to /users endpoint, updating the current user, which can also include adding/updating user CIP documents, and returns the response.
HTTP response from API
97 98 99 100 |
# File 'lib/synapse_pay_rest/api/users.rb', line 97 def update(user_id:, payload:) path = user_path(user_id: user_id) client.patch(path, payload) end |