nubank_sdk 0.4.0 → 0.5.0

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: ec434c6062ddfb9c5f4f85e39e991c1ebbd95add122bef24232b242172906bdb
4
+ data.tar.gz: d7c490ff9c027c0aa6e6f8045ea03b3a99a1f4e24a7294db032c9581486cae32
5
5
  SHA512:
6
- metadata.gz: e659147f1a8eb81e4442a4268c915e3391e337e44a81a59c7faecbcf4e9eaaf0de4e1abc2650de1e91aa5226d680b58b5b19b277e13b2bbc0fd3999076dcdaab
7
- data.tar.gz: e362fd35b8ab55e31ec48740369570b964b56990bfe51940a61ea03d5f22518bd08b8cb7fa919b6265a1dbc184271a09557352f3844b44b719ee2f12501d4b31
6
+ metadata.gz: 7f8e0d67ef141bd01cbdb7604183708792be2a146a441b09d5581a4be32698d88f7ce87386e71dcc9dbf009139b980958e934dbef6c97c6dc72fb3248e17d528
7
+ data.tar.gz: 49fbe8d14b4da3f83cb676755633c2174477696178e0925aa5f3a9796c9b459c2f40bdfb9668548bba5ea4aaafb037434f3094606c9650e4ed740402eb21e487
@@ -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.5.0)
5
5
  faraday (~> 0.15.0)
6
6
  json (~> 2.1.0)
7
7
 
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![Gem](https://img.shields.io/gem/dt/nubank_sdk?color=%23701516&logo=ruby&logoColor=%23701516&style=for-the-badge)
2
+
1
3
  # NubankSdk (Work in progress)
2
4
 
3
5
  A gem to make it ease to monitorize your Nubank account.
@@ -0,0 +1,22 @@
1
+ module NubankSdk
2
+ class Account
3
+ def initialize(connection:, api_routes:)
4
+ @connection = connection
5
+ @api_routes = api_routes
6
+ end
7
+
8
+ def balance
9
+ query_url = @api_routes.entrypoint(path: :ssl, entrypoint: :query)
10
+
11
+ response = @connection.post(
12
+ query_url, {
13
+ 'variables': {},
14
+ 'query': '{viewer {savingsAccount {currentSavingsBalance {netAmount}}}}'
15
+ }
16
+ )
17
+
18
+ data = NubankSdk::Client.get_body(response)
19
+ data[:data][:viewer][:savingsAccount][:currentSavingsBalance][:netAmount]
20
+ end
21
+ end
22
+ end
@@ -4,17 +4,16 @@ module NubankSdk
4
4
  class Auth
5
5
  attr_reader :refresh_token, :refresh_before, :access_token
6
6
 
7
- def initialize(cpf:, device_id:, key: nil, api_routes: nil, adapter: nil)
7
+ def initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil)
8
8
  @cpf = cpf
9
- @device_id = device_id
10
- @key = key || generate_key
9
+ @device_id = device_id || generate_device_id
11
10
  @api_routes = api_routes || NubankSdk::ApiRoutes.new
12
11
 
13
- @adapter = adapter
12
+ @connection_adapter = connection_adapter
14
13
  end
15
14
 
16
15
  def certificate
17
- @certificate ||= NubankSdk::Certificate.new(@cpf, @key)
16
+ @certificate ||= NubankSdk::Certificate.new(@cpf)
18
17
  end
19
18
 
20
19
  def authenticate_with_certificate(password)
@@ -47,7 +46,7 @@ module NubankSdk
47
46
  )
48
47
 
49
48
  response_data = Client.get_body(response)
50
- certificate.process_decoded response_data[:certificate]
49
+ certificate.process_decoded(key, response_data[:certificate])
51
50
  end
52
51
 
53
52
  private
@@ -70,7 +69,7 @@ module NubankSdk
70
69
  {
71
70
  login: @cpf,
72
71
  password: password,
73
- public_key: @key.public_key.to_pem,
72
+ public_key: key.public_key.to_pem,
74
73
  device_id: @device_id,
75
74
  model: "NubankSdk Client (#@device_id)",
76
75
  }
@@ -95,41 +94,47 @@ module NubankSdk
95
94
  bills_url_keys = ['bills_summary']
96
95
  customer_url_keys = ['customer']
97
96
  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
97
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :revoke_token, url: links[:revoke_token][:href])
98
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :query, url: links[:ghostflame][:href])
99
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :feed, url: find_url(feed_url_keys, links))
100
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :bills, url: find_url(bills_url_keys, links))
101
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :customer, url: find_url(customer_url_keys, links))
102
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :account, url: find_url(account_url_keys, links))
106
103
  end
107
104
 
108
105
  def find_url(keys, list)
109
106
  links_keys = list.keys
110
107
 
111
- keys.each do |key|
112
- return list[key]['href'] if links_keys.include?(key)
108
+ keys.each do |url_key|
109
+ return list[url_key]['href'] if links_keys.include?(url_key)
113
110
  end
114
111
  ''
115
112
  end
116
113
 
117
- def prepare_connections
114
+ def prepare_default_connection
118
115
  uri, @gen_certificate_path = @api_routes.entrypoint(
119
116
  path: :app,
120
117
  entrypoint: :gen_certificate,
121
118
  type: :splitted
122
119
  )
123
120
 
124
- Client::HTTP.new(uri, @adapter)
121
+ Client::HTTP.new(uri, @connection_adapter)
125
122
  end
126
123
 
127
124
  def default_connection
128
- @default_connection ||= prepare_connections
125
+ @default_connection ||= prepare_default_connection
129
126
  end
130
127
 
131
128
  def ssl_connection
132
- @ssl_connection ||= Client::HTTPS.new(certificate.encoded, @adapter)
129
+ @ssl_connection ||= Client::HTTPS.new(certificate.encoded, @connection_adapter)
130
+ end
131
+
132
+ def key
133
+ @key ||= generate_key
134
+ end
135
+
136
+ def generate_device_id
137
+ SecureRandom.uuid.split('-').last
133
138
  end
134
139
  end
135
140
  end
@@ -6,15 +6,14 @@ module NubankSdk
6
6
  class Certificate
7
7
  FILES_PATH = './certificates/'
8
8
 
9
- def initialize(cpf, key)
9
+ def initialize(cpf)
10
10
  @cpf = cpf
11
- @key = key
12
11
  end
13
12
 
14
- def process_decoded(certificate)
13
+ def process_decoded(key, certificate)
15
14
  encoded = encode certificate
16
15
 
17
- p12 = create_pkcs12_from encoded
16
+ p12 = create_pkcs12_from(key, encoded)
18
17
  save p12
19
18
  end
20
19
 
@@ -34,8 +33,8 @@ module NubankSdk
34
33
  end
35
34
  end
36
35
 
37
- def create_pkcs12_from(certificate)
38
- OpenSSL::PKCS12.create("password", "key", @key, certificate)
36
+ def create_pkcs12_from(key, certificate)
37
+ OpenSSL::PKCS12.create("password", "key", key, certificate)
39
38
  end
40
39
 
41
40
  def encode(certificate)
@@ -10,10 +10,10 @@ module NubankSdk
10
10
  end
11
11
 
12
12
  class HTTP
13
- def initialize(base_url, adapter = nil)
13
+ def initialize(base_url, connection_adapter = nil)
14
14
  @connection = Faraday.new(url: base_url) do |faraday|
15
- faraday.adapter *adapter if adapter
16
- faraday.adapter Faraday.default_adapter unless adapter
15
+ faraday.adapter *connection_adapter if connection_adapter
16
+ faraday.adapter Faraday.default_adapter unless connection_adapter
17
17
  end
18
18
  end
19
19
 
@@ -30,19 +30,28 @@ 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
+ attr_accessor :headers
34
+
35
+ def initialize(certificate, connection_adapter = nil)
36
+ client_cert = OpenSSL::X509::Certificate.new(certificate.certificate)
35
37
  client_key = OpenSSL::PKey::RSA.new(certificate.key)
36
38
 
37
39
  @connection = Faraday.new(ssl: { client_cert: client_cert, client_key: client_key}) do |faraday|
38
- faraday.adapter *adapter if adapter
39
- faraday.adapter Faraday.default_adapter unless adapter
40
+ faraday.adapter *connection_adapter if connection_adapter
41
+ faraday.adapter Faraday.default_adapter unless connection_adapter
40
42
  end
41
43
  end
42
44
 
43
45
  def post(url, body)
44
46
  @connection.post(url) do |req|
45
47
  req.headers['Content-Type'] = 'application/json'
48
+ req.headers['X-Correlation-Id'] = '772428d8-f0ee-43d6-8093-a13de3c9ce96'
49
+ req.headers['User-Agent'] = "NubankSdk Client (#{NubankSdk::VERSION})"
50
+
51
+ @headers.each do |header_key, value|
52
+ req.headers[header_key] = value
53
+ end unless @headers.nil?
54
+
46
55
  req.body = body.to_json
47
56
  end
48
57
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NubankSdk
4
+ class User
5
+ def initialize(cpf:, connection_adapter: nil)
6
+ @cpf = cpf
7
+ @connection_adapter = connection_adapter
8
+ end
9
+
10
+ def auth
11
+ @auth ||= NubankSdk::Auth.new(
12
+ cpf: @cpf,
13
+ api_routes: api_routes,
14
+ connection_adapter: @connection_adapter
15
+ )
16
+ end
17
+
18
+ def account
19
+ @account ||= NubankSdk::Account.new(connection: connection, api_routes: api_routes)
20
+ end
21
+
22
+ def api_routes
23
+ @api_routes ||= NubankSdk::ApiRoutes.new
24
+ end
25
+
26
+ private
27
+
28
+ def connection
29
+ @connection ||= setup_connection
30
+ end
31
+
32
+ def setup_connection
33
+ connection = Client::HTTPS.new(
34
+ auth.certificate.encoded,
35
+ @connection_adapter
36
+ )
37
+ connection.headers = { 'Authorization': "Bearer #{auth.access_token}" }
38
+ connection
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module NubankSdk
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
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
+ user = NubankSdk::User.new(cpf: '12345678909')
5
+ password = 'dracarys'
6
+ # authenticate the account
7
+
8
+ # request an email code
9
+ account_email = user.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
+ user.auth.exchange_certs(email_code, password)
15
+
16
+ user.auth.authenticate_with_certificate(password)
17
+
18
+ # get the account balance
19
+ user.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.5.0
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-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,12 +100,15 @@ 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
108
+ - lib/nubank_sdk/user.rb
107
109
  - lib/nubank_sdk/version.rb
108
110
  - nubank_sdk.gemspec
111
+ - usage_example.rb
109
112
  homepage: https://github.com/Viserion77/nubank_sdk
110
113
  licenses: []
111
114
  metadata: {}