nubank_sdk 0.4.1 → 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: ee3ffd6c280bb2b48dba0c1b46d4c3a560f985a55b22793aa251ece82f69da28
4
- data.tar.gz: a4a69ad6502fd3852fb91b21fc37ee2fe872f9432a575d8d72367125201d8ecd
3
+ metadata.gz: ec434c6062ddfb9c5f4f85e39e991c1ebbd95add122bef24232b242172906bdb
4
+ data.tar.gz: d7c490ff9c027c0aa6e6f8045ea03b3a99a1f4e24a7294db032c9581486cae32
5
5
  SHA512:
6
- metadata.gz: 232922b481796a104a241ee8a9c890082c93c4e23eb3de6635f06633539c0d87770cb0e85b86e1a72d94548004080a5277010cb529ab623299ac40364fa07ac9
7
- data.tar.gz: 1e82254c8026f4f5cb4e9bd3d80971d31979aced7145fe15e44e9e7a7a9a4c0f6303e232f3468860b07b5ab5cf358b624f1da4f0e03669765494a4f07fd21cd2
6
+ metadata.gz: 7f8e0d67ef141bd01cbdb7604183708792be2a146a441b09d5581a4be32698d88f7ce87386e71dcc9dbf009139b980958e934dbef6c97c6dc72fb3248e17d528
7
+ data.tar.gz: 49fbe8d14b4da3f83cb676755633c2174477696178e0925aa5f3a9796c9b459c2f40bdfb9668548bba5ea4aaafb037434f3094606c9650e4ed740402eb21e487
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nubank_sdk (0.4.1)
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.
@@ -1,43 +1,22 @@
1
1
  module NubankSdk
2
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
- )
3
+ def initialize(connection:, api_routes:)
4
+ @connection = connection
5
+ @api_routes = api_routes
22
6
  end
23
7
 
24
- def account_balance
25
- query_url = auth.api_routes.entrypoint(path: :ssl, entrypoint: :query)
26
- connection = Client::HTTPS.new(auth.certificate.encoded, @adapter)
8
+ def balance
9
+ query_url = @api_routes.entrypoint(path: :ssl, entrypoint: :query)
27
10
 
28
- response = connection.post(query_url, {
29
- 'variables': {},
30
- 'query': '{viewer {savingsAccount {currentSavingsBalance {netAmount}}}}'
31
- }, { Authorization: "Bearer #{auth.access_token}" })
11
+ response = @connection.post(
12
+ query_url, {
13
+ 'variables': {},
14
+ 'query': '{viewer {savingsAccount {currentSavingsBalance {netAmount}}}}'
15
+ }
16
+ )
32
17
 
33
- data = JSON.parse(response.body, symbolize_names: true)
18
+ data = NubankSdk::Client.get_body(response)
34
19
  data[:data][:viewer][:savingsAccount][:currentSavingsBalance][:netAmount]
35
20
  end
36
-
37
- private
38
-
39
- def generate_device_id
40
- SecureRandom.uuid.split('-').last
41
- end
42
21
  end
43
22
  end
@@ -4,21 +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
14
- end
15
-
16
- def api_routes
17
- @api_routes
12
+ @connection_adapter = connection_adapter
18
13
  end
19
14
 
20
15
  def certificate
21
- @certificate ||= NubankSdk::Certificate.new(@cpf, @key)
16
+ @certificate ||= NubankSdk::Certificate.new(@cpf)
22
17
  end
23
18
 
24
19
  def authenticate_with_certificate(password)
@@ -51,7 +46,7 @@ module NubankSdk
51
46
  )
52
47
 
53
48
  response_data = Client.get_body(response)
54
- certificate.process_decoded response_data[:certificate]
49
+ certificate.process_decoded(key, response_data[:certificate])
55
50
  end
56
51
 
57
52
  private
@@ -74,7 +69,7 @@ module NubankSdk
74
69
  {
75
70
  login: @cpf,
76
71
  password: password,
77
- public_key: @key.public_key.to_pem,
72
+ public_key: key.public_key.to_pem,
78
73
  device_id: @device_id,
79
74
  model: "NubankSdk Client (#@device_id)",
80
75
  }
@@ -110,28 +105,36 @@ module NubankSdk
110
105
  def find_url(keys, list)
111
106
  links_keys = list.keys
112
107
 
113
- keys.each do |key|
114
- 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)
115
110
  end
116
111
  ''
117
112
  end
118
113
 
119
- def prepare_connections
114
+ def prepare_default_connection
120
115
  uri, @gen_certificate_path = @api_routes.entrypoint(
121
116
  path: :app,
122
117
  entrypoint: :gen_certificate,
123
118
  type: :splitted
124
119
  )
125
120
 
126
- Client::HTTP.new(uri, @adapter)
121
+ Client::HTTP.new(uri, @connection_adapter)
127
122
  end
128
123
 
129
124
  def default_connection
130
- @default_connection ||= prepare_connections
125
+ @default_connection ||= prepare_default_connection
131
126
  end
132
127
 
133
128
  def ssl_connection
134
- @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
135
138
  end
136
139
  end
137
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,25 +30,27 @@ module NubankSdk
30
30
  end
31
31
 
32
32
  class HTTPS
33
- def initialize(certificate, adapter = nil)
33
+ attr_accessor :headers
34
+
35
+ def initialize(certificate, connection_adapter = nil)
34
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
- def post(url, body, headers = {})
45
+ def post(url, body)
44
46
  @connection.post(url) do |req|
45
47
  req.headers['Content-Type'] = 'application/json'
46
48
  req.headers['X-Correlation-Id'] = '772428d8-f0ee-43d6-8093-a13de3c9ce96'
47
49
  req.headers['User-Agent'] = "NubankSdk Client (#{NubankSdk::VERSION})"
48
50
 
49
- headers.each do |key, value|
50
- req.headers[key] = value
51
- end
51
+ @headers.each do |header_key, value|
52
+ req.headers[header_key] = value
53
+ end unless @headers.nil?
52
54
 
53
55
  req.body = body.to_json
54
56
  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.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/usage_example.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  require 'nubank_sdk'
2
2
 
3
3
  # instance a nubank account object
4
- account = NubankSdk::Account.new(cpf: '12345678909')
4
+ user = NubankSdk::User.new(cpf: '12345678909')
5
5
  password = 'dracarys'
6
6
  # authenticate the account
7
7
 
8
8
  # request an email code
9
- account_email = account.auth.request_email_code(password)
9
+ account_email = user.auth.request_email_code(password)
10
10
 
11
11
  # get the email code from the user
12
12
  puts "Enter the code sent to #{account_email}: "
13
13
  email_code = gets.chomp
14
- account.auth.exchange_certs(email_code, password)
14
+ user.auth.exchange_certs(email_code, password)
15
15
 
16
- account.auth.authenticate_with_certificate(password)
16
+ user.auth.authenticate_with_certificate(password)
17
17
 
18
18
  # get the account balance
19
- account.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.1
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-24 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
@@ -105,6 +105,7 @@ files:
105
105
  - lib/nubank_sdk/auth.rb
106
106
  - lib/nubank_sdk/certificate.rb
107
107
  - lib/nubank_sdk/client.rb
108
+ - lib/nubank_sdk/user.rb
108
109
  - lib/nubank_sdk/version.rb
109
110
  - nubank_sdk.gemspec
110
111
  - usage_example.rb