Class: Synapse::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_api/client.rb

Overview

header values

Constant Summary collapse

VALID_QUERY_PARAMS =
[:query, :page, :per_page, :full_dehydrate, :radius, :zip, :lat, :lon, :limit, :currency].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, ip_address:, fingerprint: nil, development_mode: true, raise_for_202: nil, **options) ⇒ Client

Returns a new instance of Client.

Parameters:

  • log_to (String)

    (optional) file path to log to file (logging must be true)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/synapse_api/client.rb', line 41

def initialize(client_id:, client_secret:, ip_address:, fingerprint:nil,development_mode: true, raise_for_202:nil, **options)
base_url = if development_mode
               'https://uat-api.synapsefi.com/v3.1'
             else
               'https://api.synapsefi.com/v3.1'
             end
    @client_id = client_id
    @client_secret = client_secret
    @http_client  = HTTPClient.new(base_url: base_url,
                                 client_id: client_id,
                                 client_secret: client_secret,
                                 fingerprint: fingerprint,
                                 ip_address: ip_address,
                                 raise_for_202: raise_for_202,
                                 **options)
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



27
28
29
# File 'lib/synapse_api/client.rb', line 27

def client_id
  @client_id
end

#http_clientObject Also known as: client

Returns the value of attribute http_client.



25
26
27
# File 'lib/synapse_api/client.rb', line 25

def http_client
  @http_client
end

Instance Method Details

#create_subscriptions(scope:, url:, **options) ⇒ Synapse::Subscription

Queries Synapse API to create a webhook subscriptions for platform

Parameters:

  • scope (Array<String>)
  • idempotency_key (String)

    (optional)

  • url (String)

Returns:

See Also:



181
182
183
184
185
186
187
188
189
190
# File 'lib/synapse_api/client.rb', line 181

def create_subscriptions(scope:, url:, **options)
  payload = {
         'scope' => scope,
         'url' => url,
       }

  response = client.post(subscriptions_path , payload, options)

  Subscription.new(subscription_id: response["_id"], url: response["url"], payload: response)
end

#create_user(payload:, **options) ⇒ Synapse::User

Queries Synapse API to create a new user

Parameters:

  • payload (Hash)
  • idempotency_key (String)

    (optional)

Returns:

See Also:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/synapse_api/client.rb', line 63

def create_user(payload:, **options)
   response = client.post(user_path,payload, options)

   user = User.new(
         user_id:           response['_id'],
         refresh_token:     response['refresh_token'],
         client:            client,
         full_dehydrate:    "no",
         payload:           response
       )
   user
end

#get_all_institutions(**options) ⇒ Object

Queries Synapse API for all institutions available for bank logins

Parameters:

  • page (Integer)

    (optional) response will default to 1

  • per_page (Integer)

    (optional) response will default to 20

Returns:

  • API response [Hash]



171
172
173
# File 'lib/synapse_api/client.rb', line 171

def get_all_institutions(**options)
  client.get(institutions_path(options))
end

#get_all_nodes(**options) ⇒ Array<Synapse::Nodes>

Queries Synapse API for all nodes belonging to platform

Parameters:

  • page (Integer)

    (optional) response will default to 1

  • per_page (Integer)

    (optional) response will default to 20

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/synapse_api/client.rb', line 153

def get_all_nodes(**options)
  [options[:page], options[:per_page]].each do |arg|
     if arg && (!arg.is_a?(Integer) || arg < 1)
       raise ArgumentError, "#{arg} must be nil or an Integer >= 1"
     end
   end
  path = nodes_path(options: options)
  nodes = client.get(path)

  return [] if nodes["nodes"].empty?
  response = nodes["nodes"].map { |node_data| Node.new(node_id: node_data['_id'], user_id: node_data['user_id'], payload: node_data, full_dehydrate: "no")}
  nodes = Nodes.new(limit: nodes["limit"], page: nodes["page"], page_count: nodes["page_count"], nodes_count: nodes["node_count"], payload: response)
end

#get_all_subscriptions(**options) ⇒ Array<Synapse::Subscriptions>

Queries Synapse API for all platform subscriptions

Parameters:

  • page (Integer)

    (optional) response will default to 1

  • per_page (Integer)

    (optional) response will default to 20

Returns:



196
197
198
199
200
201
202
# File 'lib/synapse_api/client.rb', line 196

def get_all_subscriptions(**options)
  subscriptions = client.get(subscriptions_path(options))

  return [] if subscriptions["subscriptions"].empty?
  response = subscriptions["subscriptions"].map { |subscription_data| Subscription.new(subscription_id: subscription_data["_id"], url: subscription_data["url"], payload: subscription_data)}
  subscriptions = Subscriptions.new(limit: subscriptions["limit"], page: subscriptions["page"], page_count: subscriptions["page_count"], subscriptions_count: subscriptions["subscription_count"], payload: response)
end

#get_all_transaction(**options) ⇒ Array<Synapse::Transactions>

Queries Synapse for all transactions on platform

Parameters:

  • page (Integer)

    (optional) response will default to 1

  • per_page (Integer)

    (optional) response will default to 20

Returns:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/synapse_api/client.rb', line 131

def get_all_transaction(**options)
  path = '/trans'

  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[p]}" : nil
  end.compact

  path += '?' + params.join('&') if params.any?

  trans = client.get(path)

  return [] if trans["trans"].empty?
  response = trans["trans"].map { |trans_data| Transaction.new(trans_id: trans_data['_id'], payload: trans_data)}
  trans = Transactions.new(limit: trans["limit"], page: trans["page"], page_count: trans["page_count"], trans_count: trans["trans_count"], payload: response)
  trans

end

#get_crypto_market_data(**options) ⇒ Object

Queries Synapse API for Crypto Currencies Market data

Parameters:

  • limit (Integer)
  • currency (String)

Returns:

  • API response [Hash]



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/synapse_api/client.rb', line 283

def get_crypto_market_data(**options)
  path = '/nodes/crypto-market-watch'

  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[p]}" : nil
  end.compact

  path += '?' + params.join('&') if params.any?

  data = client.get(path)
  data
end

#get_crypto_quotesObject

Queries Synapse API for Crypto Currencies Quotes

Returns:

  • API response [Hash]



268
269
270
271
272
273
274
275
276
277
# File 'lib/synapse_api/client.rb', line 268

def get_crypto_quotes()
  path = '/nodes/crypto-quotes'
  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[p]}" : nil
  end.compact

  path += '?' + params.join('&') if params.any?
  quotes = client.get(path)
  quotes
end

#get_subscription(subscription_id:) ⇒ Synapse::Subscription

Queries Synapse API for a subscription by subscription_id

Parameters:

  • subscription_id (String)

Returns:



208
209
210
211
212
# File 'lib/synapse_api/client.rb', line 208

def get_subscription(subscription_id:)
  path = subscriptions_path + "/#{subscription_id}"
  response = client.get(path)
  Subscription.new(subscription_id: response["_id"], url: response["url"], payload: response)
end

#get_user(user_id:, **options) ⇒ Synapse::User

Parameters:

  • full_dehydrate (String)

    (optional) if true, returns all KYC on user

Returns:

Raises:

  • (ArgumentError)

See Also:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/synapse_api/client.rb', line 90

def get_user(user_id:, **options)
  raise ArgumentError, 'client must be a Synapse::Client' unless self.is_a?(Client)
  raise ArgumentError, 'user_id must be a String' unless user_id.is_a?(String)

  options[:full_dehydrate] = "yes" if options[:full_dehydrate] == true
  options[:full_dehydrate] = "no" if options[:full_dehydrate] == false

  path = user_path(user_id: user_id, full_dehydrate: options[:full_dehydrate])
  response = client.get(path)

  user = User.new(
         user_id:                response['_id'],
         refresh_token:     response['refresh_token'],
         client:            client,
         full_dehydrate:    options[:full_dehydrate] == "yes" ? true : false,
         payload:           response
       )
   user
end

#get_users(**options) ⇒ Array<Synapse::Users>

Note:

users created this way are not automatically OAuthed

users with matching name/email

Parameters:

  • page (Integer)

    (optional) response will default to 1

  • per_page (Integer)

    (optional) response will default to 20

Returns:



117
118
119
120
121
122
123
124
125
# File 'lib/synapse_api/client.rb', line 117

def get_users(**options)
  path = user_path(options)
  response = client.get(path)
  return [] if response["users"].empty?
  users = response["users"].map { |user_data| User.new(user_id: user_data['_id'], refresh_token: user_data['refresh_token'], client: client, full_dehydrate: "no", payload: user_data)}
  users = Users.new(limit: response["limit"], page: response["page"], page_count: response["page_count"], user_count: response["user_count"], payload: users, http_client: client)

   users
end

#issue_public_key(scope:) ⇒ Object

Note:

valid scope “OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS”

Raises:

  • (ArgumentError)


240
241
242
243
244
245
246
# File 'lib/synapse_api/client.rb', line 240

def issue_public_key(scope:)
  raise ArgumentError, 'scope must be a string' unless scope.is_a?(String)
  path = '/client?issue_public_key=YES'
  path += "&scope=#{scope}"
  response = client.get(path)
  response[ "public_key_obj"]
end

#locate_atm(**options) ⇒ Hash

Queries Synapse API for ATMS nearby

Parameters:

  • zip (String)
  • radius (String)
  • lat (String)
  • lon (String)

Returns:

  • (Hash)

See Also:



255
256
257
258
259
260
261
262
263
264
# File 'lib/synapse_api/client.rb', line 255

def locate_atm(**options)
  params = VALID_QUERY_PARAMS.map do |p|
    options[p] ? "#{p}=#{options[p]}" : nil
  end.compact

  path = "/nodes/atms?"
  path += params.join('&') if params.any?
  atms = client.get(path)
  atms
end

#update_headers(fingerprint: nil, idemopotency_key: nil, ip_address: nil) ⇒ Object

Update headers in HTTPClient class for API request headers

Parameters:

  • fingerprint (Hash) (defaults to: nil)
  • idemopotency_key (Hash) (defaults to: nil)
  • ip_address (Hash) (defaults to: nil)


81
82
83
# File 'lib/synapse_api/client.rb', line 81

def update_headers(fingerprint:nil, idemopotency_key:nil, ip_address:nil)
  client.update_headers(fingerprint: fingerprint, idemopotency_key: idemopotency_key, ip_address: ip_address)
end

#update_subscriptions(subscription_id:, url: nil, scope: nil, is_active: nil) ⇒ Synapse::Subscription

updates subscription platform subscription see docs.synapsefi.com/docs/update-subscription

Parameters:

  • subscription_id (String)
  • is_active (boolean) (defaults to: nil)
  • url (String) (defaults to: nil)
  • scope (Array<String>) (defaults to: nil)

Returns:



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/synapse_api/client.rb', line 221

def update_subscriptions(subscription_id:, url:nil, scope:nil, is_active:nil)
  path = subscriptions_path + "/#{subscription_id}"

  payload = {}

  payload["url"] = url if url
  payload["scope"] = scope if scope
  payload["is_active"] = is_active if is_active

  response = client.patch(path, payload)
  Subscription.new(subscription_id: response["_id"], url: response["url"], payload: response)
end