fintual 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f98a9ffaa3da5c224140665a955488ed5f42710614b9e67fa497e4faf3e8c97d
4
+ data.tar.gz: 35b157ec4af6f583e80d1998efaa7259d60640b5dce7546448327dde064f81de
5
+ SHA512:
6
+ metadata.gz: 5dfb711bf11e35352c36ad38b4a9b03f9e90f954e8102d0f614d4185981ee1e2fe2e2a1e58e408c5c29be9ecfca59f79e4427f145fccc0f52d5f4c0e31f6fc95
7
+ data.tar.gz: bbe463d14fa28fb46e968fe2a2bc02573ed69dfe196b3472d07500f8212651c869451fb7d8ad379c0917b33b697e046b3f946e26c75ac58c98f3c4c512c42970
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ *.gem
2
+ coverage/*
3
+ Gemfile.lock
4
+ *~
5
+ .bundle
6
+ .rvmrc
7
+ log/*
8
+ measurement/*
9
+ pkg/*
10
+ .DS_Store
11
+ .env
12
+ spec/dummy/tmp/*
13
+ spec/dummy/log/*.log
14
+ demo.rb
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.5
6
+ NewCops: enable
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fintoc.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
8
+ end
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+ require 'rubocop/rake_task'
3
+
4
+ begin
5
+ require 'bundler/setup'
6
+ Bundler::GemHelper.install_tasks
7
+ rescue LoadError
8
+ puts 'although not required, bundler is recommended for running the tests'
9
+ end
10
+ task default: :spec
11
+ require 'rspec/core/rake_task'
12
+ RSpec::Core::RakeTask.new(:spec)
13
+ require 'rubocop/rake_task'
14
+ RuboCop::RakeTask.new do |task|
15
+ task.requires << 'rubocop-performance'
16
+ task.requires << 'rubocop-rspec'
17
+ end
data/fintual.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'fintual'
3
+ s.version = '0.1.0'
4
+ s.summary = 'client for fintual.cl'
5
+ s.description = 'A simple api client for fintual.cl'
6
+ s.authors = ['Alfredo Enrione']
7
+ s.email = 'aenrione@protonmail.com'
8
+ s.homepage =
9
+ 'https://rubygems.org/gems/fintual'
10
+ s.license = 'MIT'
11
+ s.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
12
+ s.add_dependency 'http'
13
+ s.add_dependency 'json'
14
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ s.bindir = 'exe'
18
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'fintual/errors'
5
+ require 'fintual/constants'
6
+ require 'fintual/version'
7
+ require 'fintual/resources/goal'
8
+ require 'json'
9
+ require 'net/http'
10
+ require 'net/https'
11
+
12
+ module Fintual
13
+ # Client component to make the requests and get data
14
+ class Client
15
+ def initialize(email, password)
16
+ @email = email
17
+ @password = password
18
+ @user_agent = "fintual-ruby/#{Fintual::VERSION}"
19
+ @headers = {
20
+ 'Content-Type': 'application/json',
21
+ 'User-Agent': @user_agent
22
+ }
23
+ @default_params = {}
24
+ @default_params = {
25
+ user_email: @email,
26
+ user_token: get_token
27
+ }
28
+ end
29
+
30
+ def get
31
+ request('get')
32
+ end
33
+
34
+ def delete
35
+ request('delete')
36
+ end
37
+
38
+ def request(method)
39
+ proc do |resource, **kwargs|
40
+ parameters = params(method, **kwargs)
41
+ response = make_request(method, resource, parameters)
42
+ content = JSON.parse(response.body, symbolize_names: true)
43
+ data = content[:data]
44
+ error = content[:error]
45
+
46
+ if content[:status] == "error"
47
+ raise_custom_error(content)
48
+ end
49
+ data
50
+ end
51
+ end
52
+
53
+ def goals
54
+ result = get.call('goals')
55
+ result.map { |data| build_goal(data[:attributes]) }
56
+ end
57
+
58
+ def build_goal(**kwargs)
59
+ Fintual::Goal.new(**kwargs)
60
+ end
61
+
62
+ def to_s
63
+ visible_chars = 4
64
+ hidden_part = '*' * (@api_key.size - visible_chars)
65
+ visible_key = @api_key.slice(0, visible_chars)
66
+ "Client(🔑=#{hidden_part + visible_key}"
67
+ end
68
+
69
+ private
70
+
71
+ def client
72
+ @client ||= Net::HTTP
73
+ end
74
+
75
+ def make_request(method, resource, parameters)
76
+ uri = URI.parse("#{Fintual::Constants::SCHEME}#{Fintual::Constants::BASE_URL}#{resource}")
77
+ https = Net::HTTP.new(uri.host, uri.port)
78
+ https.use_ssl = true
79
+ case method.downcase
80
+ when 'get'
81
+ req = Net::HTTP::Get.new(uri.path, @headers)
82
+ when 'post'
83
+ req = Net::HTTP::Post.new(uri.path, @headers)
84
+ end
85
+ req.body = parameters.to_json
86
+ https.request(req)
87
+ end
88
+
89
+ def params(method, **kwargs)
90
+ { **@default_params, **kwargs }
91
+
92
+ end
93
+
94
+ def raise_custom_error(error)
95
+ raise Fintual::Errors::FintualError.new(error[:message])
96
+ end
97
+
98
+ def get_token
99
+ payload = {
100
+ user: {email: @email, password: @password}
101
+ }
102
+ request('post').call('access_tokens', payload)[:attributes][:token]
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Constant values for the client
4
+ module Fintual
5
+ module Constants
6
+ FIELDSUBS = [%w[id id_], %w[type type_]].freeze
7
+ GENERAL_DOC_URL = 'https://fintual.cl/api-docs/index.html'
8
+ SCHEME = 'https://'
9
+ BASE_URL = 'fintual.cl/api/'
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fintual/constants'
4
+
5
+ module Fintual
6
+ module Errors
7
+ # Error definitions for handling wrong requests
8
+ class FintualError < StandardError
9
+ def initialize(message, doc_url = Fintual::Constants::GENERAL_DOC_URL)
10
+ super()
11
+ @message = message
12
+ @doc_url = doc_url
13
+ end
14
+
15
+ def message
16
+ "\n#{@message}\n Please check the docs at: #{@doc_url}"
17
+ end
18
+
19
+ def to_s
20
+ message
21
+ end
22
+ end
23
+
24
+ class InvalidRequestError < FintualError; end
25
+ class LinkError < FintualError; end
26
+ class AuthenticationError < FintualError; end
27
+ class InstitutionError < FintualError; end
28
+ class ApiError < FintualError; end
29
+ class MissingResourceError < FintualError; end
30
+ class InvalidLinkTokenError < FintualError; end
31
+ class InvalidUsernameError < FintualError; end
32
+ class InvalidHolderTypeError < FintualError; end
33
+ class MissingParameterError < FintualError; end
34
+ class EmptyStringError < FintualError; end
35
+ class UnrecognizedRequestError < FintualError; end
36
+ class InvalidDateError < FintualError; end
37
+ class InvalidCredentialsError < FintualError; end
38
+ class LockedCredentialsError < FintualError; end
39
+ class InvalidApiKeyError < FintualError; end
40
+ class UnavailableInstitutionError < FintualError; end
41
+ class InternalServerError < FintualError; end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fintual
4
+ # for handling the total balance
5
+ class Goal
6
+ attr_reader :name, :current, :deposited, :profit, :created_at
7
+
8
+ def initialize(name:, nav:, created_at:, deposited:, profit:, **)
9
+ @name = name
10
+ @deposited = deposited
11
+ @current = nav
12
+ @deposited = deposited
13
+ @profit = profit
14
+ @created_at = created_at
15
+ end
16
+
17
+ def to_s
18
+ "#{name}: #{@current} (#{@deposited})"
19
+ end
20
+
21
+ def inspect
22
+ "<Goal #{name}: #{@current} (#{@deposited})>"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fintual
4
+ VERSION = '0.1.1'
5
+ end
data/lib/fintual.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fintual/version'
4
+ require 'fintual/client'
5
+
6
+ # Main Module
7
+ module Fintual
8
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fintual
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alfredo Enrione
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple api client for fintual.cl
42
+ email: aenrione@protonmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".rubocop.yml"
49
+ - Gemfile
50
+ - Rakefile
51
+ - fintual.gemspec
52
+ - lib/fintual.rb
53
+ - lib/fintual/client.rb
54
+ - lib/fintual/constants.rb
55
+ - lib/fintual/errors.rb
56
+ - lib/fintual/resources/goal.rb
57
+ - lib/fintual/version.rb
58
+ homepage: https://rubygems.org/gems/fintual
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.3.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: client for fintual.cl
81
+ test_files: []