nubank_sdk 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b37684fbfd690e5b972c3626514ef706766af1279c9c1f4049310f94f31e90b9
4
- data.tar.gz: 6052b2f3a8cc2d879960414d415898b9c5bbc6eb66a7aafad7003788d38696f7
3
+ metadata.gz: ee3ffd6c280bb2b48dba0c1b46d4c3a560f985a55b22793aa251ece82f69da28
4
+ data.tar.gz: a4a69ad6502fd3852fb91b21fc37ee2fe872f9432a575d8d72367125201d8ecd
5
5
  SHA512:
6
- metadata.gz: e659147f1a8eb81e4442a4268c915e3391e337e44a81a59c7faecbcf4e9eaaf0de4e1abc2650de1e91aa5226d680b58b5b19b277e13b2bbc0fd3999076dcdaab
7
- data.tar.gz: e362fd35b8ab55e31ec48740369570b964b56990bfe51940a61ea03d5f22518bd08b8cb7fa919b6265a1dbc184271a09557352f3844b44b719ee2f12501d4b31
6
+ metadata.gz: 232922b481796a104a241ee8a9c890082c93c4e23eb3de6635f06633539c0d87770cb0e85b86e1a72d94548004080a5277010cb529ab623299ac40364fa07ac9
7
+ data.tar.gz: 1e82254c8026f4f5cb4e9bd3d80971d31979aced7145fe15e44e9e7a7a9a4c0f6303e232f3468860b07b5ab5cf358b624f1da4f0e03669765494a4f07fd21cd2
@@ -10,6 +10,7 @@
10
10
  "certificates",
11
11
  "auth",
12
12
  "client",
13
- "version"
13
+ "version",
14
+ "account"
14
15
  ]
15
16
  }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nubank_sdk (0.4.0)
4
+ nubank_sdk (0.4.1)
5
5
  faraday (~> 0.15.0)
6
6
  json (~> 2.1.0)
7
7
 
@@ -0,0 +1,43 @@
1
+ module NubankSdk
2
+ class Account
3
+ attr_reader :cpf, :device_id
4
+
5
+ def initialize(cpf:, key:, device_id: nil, adapter: nil)
6
+ @cpf = cpf
7
+ @device_id = device_id || generate_device_id
8
+
9
+ @api_routes = NubankSdk::ApiRoutes.new(connection_adapter: adapter)
10
+ @adapter = adapter
11
+ @key = key
12
+ end
13
+
14
+ def auth
15
+ @auth ||= NubankSdk::Auth.new(
16
+ cpf: @cpf,
17
+ key: @key,
18
+ device_id: @device_id,
19
+ api_routes: @api_routes,
20
+ adapter: @adapter
21
+ )
22
+ end
23
+
24
+ def account_balance
25
+ query_url = auth.api_routes.entrypoint(path: :ssl, entrypoint: :query)
26
+ connection = Client::HTTPS.new(auth.certificate.encoded, @adapter)
27
+
28
+ response = connection.post(query_url, {
29
+ 'variables': {},
30
+ 'query': '{viewer {savingsAccount {currentSavingsBalance {netAmount}}}}'
31
+ }, { Authorization: "Bearer #{auth.access_token}" })
32
+
33
+ data = JSON.parse(response.body, symbolize_names: true)
34
+ data[:data][:viewer][:savingsAccount][:currentSavingsBalance][:netAmount]
35
+ end
36
+
37
+ private
38
+
39
+ def generate_device_id
40
+ SecureRandom.uuid.split('-').last
41
+ end
42
+ end
43
+ end
@@ -13,6 +13,10 @@ module NubankSdk
13
13
  @adapter = adapter
14
14
  end
15
15
 
16
+ def api_routes
17
+ @api_routes
18
+ end
19
+
16
20
  def certificate
17
21
  @certificate ||= NubankSdk::Certificate.new(@cpf, @key)
18
22
  end
@@ -95,14 +99,12 @@ module NubankSdk
95
99
  bills_url_keys = ['bills_summary']
96
100
  customer_url_keys = ['customer']
97
101
  account_url_keys = ['account']
98
-
99
- @api_routes.add_entrypoint(:ssl, :revoke_token, links[:revoke_token][:href])
100
- @api_routes.add_entrypoint(:ssl, :query, links[:ghostflame][:href])
101
- @api_routes.add_entrypoint(:ssl, :feed, find_url(feed_url_keys, links))
102
- @api_routes.add_entrypoint(:ssl, :bills, find_url(bills_url_keys, links))
103
- @api_routes.add_entrypoint(:ssl, :customer, find_url(customer_url_keys, links))
104
- @api_routes.add_entrypoint(:ssl, :account, find_url(account_url_keys, links))
105
- @api_routes
102
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :revoke_token, url: links[:revoke_token][:href])
103
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :query, url: links[:ghostflame][:href])
104
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :feed, url: find_url(feed_url_keys, links))
105
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :bills, url: find_url(bills_url_keys, links))
106
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :customer, url: find_url(customer_url_keys, links))
107
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :account, url: find_url(account_url_keys, links))
106
108
  end
107
109
 
108
110
  def find_url(keys, list)
@@ -30,8 +30,8 @@ module NubankSdk
30
30
  end
31
31
 
32
32
  class HTTPS
33
- def initialize(certificate, adapter = nil)
34
- client_cert = OpenSSL::X509::Certificate.new(certificate.certificate),
33
+ def initialize(certificate, adapter = nil)
34
+ client_cert = OpenSSL::X509::Certificate.new(certificate.certificate)
35
35
  client_key = OpenSSL::PKey::RSA.new(certificate.key)
36
36
 
37
37
  @connection = Faraday.new(ssl: { client_cert: client_cert, client_key: client_key}) do |faraday|
@@ -40,9 +40,16 @@ module NubankSdk
40
40
  end
41
41
  end
42
42
 
43
- def post(url, body)
43
+ def post(url, body, headers = {})
44
44
  @connection.post(url) do |req|
45
45
  req.headers['Content-Type'] = 'application/json'
46
+ req.headers['X-Correlation-Id'] = '772428d8-f0ee-43d6-8093-a13de3c9ce96'
47
+ req.headers['User-Agent'] = "NubankSdk Client (#{NubankSdk::VERSION})"
48
+
49
+ headers.each do |key, value|
50
+ req.headers[key] = value
51
+ end
52
+
46
53
  req.body = body.to_json
47
54
  end
48
55
  end
@@ -1,3 +1,3 @@
1
1
  module NubankSdk
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/nubank_sdk.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "nubank_sdk/account"
1
2
  require "nubank_sdk/api_routes"
2
3
  require "nubank_sdk/auth"
3
4
  require "nubank_sdk/certificate"
data/usage_example.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'nubank_sdk'
2
+
3
+ # instance a nubank account object
4
+ account = NubankSdk::Account.new(cpf: '12345678909')
5
+ password = 'dracarys'
6
+ # authenticate the account
7
+
8
+ # request an email code
9
+ account_email = account.auth.request_email_code(password)
10
+
11
+ # get the email code from the user
12
+ puts "Enter the code sent to #{account_email}: "
13
+ email_code = gets.chomp
14
+ account.auth.exchange_certs(email_code, password)
15
+
16
+ account.auth.authenticate_with_certificate(password)
17
+
18
+ # get the account balance
19
+ account.account_balance
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nubank_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viserion77
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-23 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,12 +100,14 @@ files:
100
100
  - bin/setup
101
101
  - certificates/.gitkeep
102
102
  - lib/nubank_sdk.rb
103
+ - lib/nubank_sdk/account.rb
103
104
  - lib/nubank_sdk/api_routes.rb
104
105
  - lib/nubank_sdk/auth.rb
105
106
  - lib/nubank_sdk/certificate.rb
106
107
  - lib/nubank_sdk/client.rb
107
108
  - lib/nubank_sdk/version.rb
108
109
  - nubank_sdk.gemspec
110
+ - usage_example.rb
109
111
  homepage: https://github.com/Viserion77/nubank_sdk
110
112
  licenses: []
111
113
  metadata: {}