Class: SynapsePayRest::Users

Inherits:
Object
  • Object
show all
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

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

#clientSynapsePayRest::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

Deprecated.

Use #update with KYC 2.0 payload instead.

Detects the file type of the file and calls #attach_file_with_file_type on it.

Parameters:

  • file_path (String)


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

Deprecated.

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.

Parameters:

  • file_path (String)
  • file_type (String)

    MIME type



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,"
  base64_attachment = mime_padding + encoded

  payload = {
    'doc' => {
      'attachment' => base64_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

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:

See Also:



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.

Parameters:

  • file_path (String)
  • file_type (String, void) (defaults to: nil)

    (optional) MIME type of file (will attempt to autodetect if nil)

Returns:

  • (String)

    base64 encoded file



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 encode_attachment(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

TODO:

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

Parameters:

  • user_id (String, void) (defaults to: nil)

    id of the user

  • query (String)

    (optional) response will be filtered to users with matching name/email

  • page (String, Integer)

    (optional) response will default to 1

  • per_page (String, Integer)

    (optional) response will default to 20

  • full_dehydrate (String, String)

    (optional) response will inclulde all KYC info on user

Returns:

  • (Hash)

    API response

Raises:



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, **options)
  path = user_path(user_id: user_id)

  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[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

Parameters:

  • user_id (String)
  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:

See Also:



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

Parameters:

  • payload (Hash)

Returns:

  • (Hash)

    API response

Raises:

See Also:



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