myfinance-rails 0.4.8 → 0.5.2

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
  SHA1:
3
- metadata.gz: 6b9cabf1f12fdc447f38254d140a7887d3800f83
4
- data.tar.gz: 2692b2cc9cf377069df3f33f40810cea7fde744b
3
+ metadata.gz: 40ca5caa6b36f4bd7a68db1a620b4d24722cb2b7
4
+ data.tar.gz: 259d31f3a117393d11752e7536ce7689e60b1cec
5
5
  SHA512:
6
- metadata.gz: f2256ab1b44da4e8c72f182c89b4781b49141b278f1b16aec7ccb2669d696a3fc858c1341f9a7201a30530ef99e747603e82feb648176788abf43ff7b1594fcc
7
- data.tar.gz: b62cd438742ca91c752ceb9f95c53c52e207cf7c2557e33aa91188fabc3b80ff954d883bdcdf6fa2f94329eab2e66c2c8da4dd50251e992cbd8cb5b4f8f79268
6
+ metadata.gz: 7156beb008367522c50944b8b80e2131c8509de618b9db9ec8bdc5cd7b698497a8d47f42714f7927eea7ccf471e0e3d7173698d69455706dfb9828a9b10e69b1
7
+ data.tar.gz: e2692cd1d3638386ee74df64fd9068389efb48ddf05f64cb33fb187ab6d48a903e911cdfdff5a1b619084fdcabee813de405709cdfcd85c74af0cdec53b6b81b
@@ -1,7 +1,9 @@
1
1
  require 'httparty'
2
+ require 'httmultiparty'
2
3
  require 'myfinance/version'
3
4
  require 'myfinance/entidade'
4
5
  require 'myfinance/pessoa'
6
+ require 'myfinance/conta_a_pagar'
5
7
  require 'myfinance/conta_a_receber'
6
8
  require 'myfinance/imposto'
7
9
  require 'myfinance/categoria'
@@ -9,21 +11,27 @@ require 'myfinance/centro_receita_custo'
9
11
  require 'myfinance/account'
10
12
  require 'myfinance/conta_deposito'
11
13
  require 'myfinance/webhook'
14
+ require 'myfinance/anexo'
12
15
  require 'json'
13
16
 
14
17
  module Myfinance
15
18
 
16
- include HTTParty
19
+ # include HTTParty
20
+ include HTTMultiParty
17
21
 
18
22
  attr_accessor :token, :endpoint, :account_id
19
23
 
20
- def self.setup(token, production=false, account_id=nil, logger=nil)
24
+ # se o usuario tiver mais de um account e não informarmos o account id,
25
+ # ele pega o primeiro
26
+ # se tiver mais de um, precisamos informar
27
+ def self.setup(token, production=false, account=nil, logger=nil)
21
28
  logger(logger, :info, :curl) if logger
22
29
 
23
30
  if production
24
31
  @endpoint = 'https://app.myfinance.com.br'
25
32
  else
26
33
  @endpoint = 'https://sandbox.myfinance.com.br'
34
+ # @endpoint = 'https://app.myfinance.com.br'
27
35
  end
28
36
  base_uri @endpoint
29
37
  @token = token
@@ -33,15 +41,36 @@ module Myfinance
33
41
  unless response.code == 200
34
42
  raise "Erro ao inicializar a API do MyFinance: #{response.code} : #{response.parsed_response}"
35
43
  end
36
- @account_id = get_account_id(account_id, response)
44
+ if account and account.is_a?(String)
45
+ @account_id = account_id(account)
46
+ # else
47
+ # @account_id = get_account_id(account, response)
48
+ end
49
+ end
50
+
51
+ def account_id
52
+ @account_id
53
+ end
54
+
55
+ def set_account_id(account_id)
56
+ @account_id = account_id
57
+ end
58
+
59
+ def set_account_id_by_name(account_name)
60
+ @account_id = Myfinance.account_id(account_name)
37
61
  end
38
62
 
39
63
  private
40
64
 
65
+ def self.header_info
66
+ # { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
67
+ { 'Content-Type' => 'application/json' }
68
+ end
69
+
41
70
  def self.lget(url)
42
71
  options = {
43
72
  :basic_auth => {:username => @token, :password => 'x'},
44
- :headers => { 'Content-Type' => 'application/json' }
73
+ :headers => header_info
45
74
  }
46
75
  add_account_id options
47
76
  get url, options
@@ -51,18 +80,32 @@ module Myfinance
51
80
  options = {
52
81
  :basic_auth => {:username => @token, :password => 'x'},
53
82
  :body => post_data.to_json,
54
- :headers => { 'Content-Type' => 'application/json' }
83
+ :headers => header_info
55
84
  }
56
85
  add_account_id options
57
86
  response = post url, options
58
87
  response
59
88
  end
60
89
 
90
+ def self.multi_party_post(url,post_data)
91
+ options = {
92
+ :basic_auth => {:username => @token, :password => 'x'},
93
+ :body => post_data,
94
+ :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' },
95
+ :detect_mime_type => true
96
+ }
97
+ add_account_id options
98
+ # puts options.inspect
99
+ response = post url, options
100
+ response
101
+ end
102
+
103
+
61
104
  def self.lput(url,post_data)
62
105
  options = {
63
106
  :basic_auth => {:username => @token, :password => 'x'},
64
107
  :body => post_data.to_json,
65
- :headers => { 'Content-Type' => 'application/json' }
108
+ :headers => header_info
66
109
  }
67
110
  add_account_id options
68
111
  response = put url, options
@@ -72,7 +115,7 @@ module Myfinance
72
115
  def self.ldelete(url)
73
116
  options = {
74
117
  :basic_auth => {:username => @token, :password => 'x'},
75
- :headers => { 'Content-Type' => 'application/json' }
118
+ :headers => header_info
76
119
  }
77
120
  add_account_id options
78
121
  response = delete url, options
@@ -88,7 +131,10 @@ module Myfinance
88
131
  end
89
132
 
90
133
  def self.get_account_id(account_id, accounts_response)
91
- return account_id unless (account_id.nil? or account_id == "")
92
- accounts_response.first["account"]["id"]
134
+ return account_id if account_id
135
+ qtd_de_contas = accounts_response.size
136
+ raise 'Esse usuário não tem uma Conta associada ao seu usuário, por favor defina qual conta usar no acesso da API' if qtd_de_contas == 0
137
+ raise 'Esse usuário tem mais de uma Conta associada ao seu usuário, por favor defina qual conta usar no acesso da API' if qtd_de_contas > 1
138
+ accounts_response.first['account']['id']
93
139
  end
94
140
  end
@@ -1,7 +1,23 @@
1
1
  module Myfinance
2
2
 
3
+ def self.account_id(nome)
4
+ mid = nil
5
+ available_accounts = accounts
6
+ available_accounts.each do | item |
7
+ item = item['account']
8
+ if item['name'] == nome
9
+ mid = item['id']
10
+ break
11
+ end
12
+ end
13
+ mid
14
+ rescue => e
15
+ raise "Não foi retornado um array de contas como esperado: #{available_accounts.inspect}. #{e.message}"
16
+ end
17
+
3
18
  def self.accounts
4
19
  lget '/accounts.json'
5
20
  end
21
+
6
22
  end
7
23
 
@@ -0,0 +1,33 @@
1
+ module Myfinance
2
+
3
+ require 'tempfile'
4
+
5
+ def self.cria_e_anexa_arquivo(entity_id, receivable_id, nome_do_arquivo, conteudo_do_arquivo)
6
+ file = nome_do_arquivo.split('.')
7
+ #:encoding => 'ascii-8bit' -- na verdade não altere o arquivo original
8
+ attached_file = Tempfile.new([file[0], ".#{file[1]}"])
9
+ attached_file << conteudo_do_arquivo.encode('UTF-8', :invalid => :replace, :undef => :replace )
10
+ attached_file.rewind
11
+ response = anexa_arquivo(entity_id, receivable_id, nome_do_arquivo, attached_file)
12
+ # attached_file.close
13
+ # attached_file.unlink
14
+ response
15
+ end
16
+
17
+
18
+ def self.anexa_arquivo(entity_id, receivable_id, titulo, arquivo)
19
+ post_data = { attachment: {
20
+ attachment: arquivo,
21
+ title: titulo,
22
+ associated_financial_account_ids: [receivable_id]
23
+ }
24
+ }
25
+ response = multi_party_post( "/entities/#{entity_id}/attachments",post_data)
26
+ if response['attachment'][0] == "não pode ser vazio."
27
+ raise "O arquivo anexado foi vazio: #{arquivo}"
28
+ end
29
+ response
30
+ end
31
+
32
+ end
33
+
@@ -1,13 +1,36 @@
1
1
  module Myfinance
2
2
 
3
- def self.cria_conta_a_pagar(nome_da_entidade,pagamento)
3
+ def self.cria_conta_a_pagar_entidade(nome_da_entidade,faturamento)
4
4
  entity_id = entidade_id(nome_da_entidade)
5
- payable_account = {
5
+ cria_conta_a_pagar(entity_id,faturamento)
6
+ end
7
+
8
+ def self.busca_conta_a_pagar(entity_id, nfe_id, search_field = 'description')
9
+ raise 'Enitidade não informada!' unless entity_id
10
+ lget "/entities/#{entity_id}/payable_accounts.json?search[#{search_field}_like]=#{nfe_id}"
11
+ end
12
+
13
+ def self.cria_conta_a_pagar(entity_id,pagamento)
14
+ raise 'Enitidade não informada!' unless entity_id
15
+ response = lpost "/entities/#{entity_id}/payable_accounts.json", parametro_conta_a_pagar(pagamento)
16
+ response
17
+ end
18
+
19
+ def self.apaga_conta_a_pagar(id, entity_id)
20
+ ldelete "/entities/#{entity_id}/payable_accounts/#{id}.json"
21
+ end
22
+
23
+ def self.conta_a_pagar(id, entity_id)
24
+ lget "/entities/#{entity_id}/payable_accounts/#{id}.json"
25
+ end
26
+
27
+ def self.parametro_conta_a_pagar(pagamento)
28
+ {
6
29
  'payable_account' => pagamento
7
30
  }
8
- response = lpost "/entities/#{entity_id}/payable_accounts.json", payable_account
9
- response
10
31
  end
11
32
 
33
+ private_class_method :parametro_conta_a_pagar
34
+
12
35
  end
13
36
 
@@ -5,14 +5,25 @@ module Myfinance
5
5
  end
6
6
 
7
7
  def self.conta_deposito_id(entity_id, nome)
8
- mid = nil
9
8
  contas_deposito(entity_id).each do | item |
10
9
  conta = item["deposit_account"]
11
- if conta["name"].strip == nome.strip
12
- mid = conta["id"]
10
+ if conta["name"].strip.downcase == nome.strip.downcase
11
+ return conta["id"]
13
12
  break
14
13
  end
15
14
  end
16
- mid
15
+ nil
17
16
  end
17
+
18
+ def self.contas_deposito_like(entity_id, like_str)
19
+ contas = []
20
+ contas_deposito(entity_id).each do | item |
21
+ conta = item["deposit_account"]
22
+ if conta["name"].strip.downcase.include?(like_str.downcase)
23
+ contas << conta
24
+ end
25
+ end
26
+ contas
27
+ end
28
+
18
29
  end
@@ -1,3 +1,3 @@
1
1
  module Myfinance
2
- VERSION = '0.4.8'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -5,12 +5,12 @@ require 'myfinance/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'myfinance-rails'
8
- spec.version = Myfinance::VERSION
8
+ spec.version = '0.5.2'
9
9
  spec.authors = ['José Lopes Neto']
10
10
  spec.email = ['jose.neto@taxweb.com.br']
11
11
  spec.summary = %q{GEM para facilitar a uso da API do Myfinance em Aplicativos Rails}
12
12
  spec.description = %q{GEM para facilitar a uso da API do Myfinance em Aplicativos Rails usando HTTParty}
13
- spec.homepage = ''
13
+ spec.homepage = 'http://wwww.taxweb.com.br'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.6'
22
- spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec'
24
- spec.add_dependency 'httparty','~> 0.13.1'
22
+ spec.add_development_dependency 'rake', '~> 0'
23
+ spec.add_development_dependency 'rspec', '~> 0'
24
+ spec.add_dependency 'httparty','~> 0.13'
25
+ spec.add_dependency 'httmultiparty', '~> 0'
26
+
25
27
  end
@@ -3,47 +3,47 @@ describe 'Account', type: :feature do
3
3
  require 'myfinance'
4
4
 
5
5
  it '#accounts' do
6
- double accounts_double = double('account')
6
+ # double accounts_double = double('account')
7
7
  expect(Myfinance).to receive(:lget).once.with('/accounts.json').and_return accounts_double
8
8
  expect(Myfinance.accounts).to eql(accounts_double)
9
9
  end
10
10
 
11
- describe "specific account" do
12
- let(:response_double) { double("response", code: 200) }
13
- describe "informed" do
11
+ describe 'specific account' do
12
+ let(:response_double) { double('response', code: 200) }
13
+ describe 'informed' do
14
14
  before do
15
15
  expect(Myfinance).to receive(:accounts).and_return response_double
16
- Myfinance.setup "123456", false, 25
16
+ Myfinance.setup '123456', false, 25
17
17
  end
18
- it "get" do
19
- expect(Myfinance).to receive(:get).with("/entities.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :headers=>{"Content-Type"=>"application/json", "ACCOUNT_ID"=>"25"}})
18
+ it 'get' do
19
+ expect(Myfinance).to receive(:get).with('/entities.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :headers=>{'Content-Type' => 'application/json', 'ACCOUNT_ID' => '25'}})
20
20
  Myfinance.entidades
21
21
  end
22
- it "post" do
23
- expect(Myfinance).to receive(:post).with("/people.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :body=>"{\"person\":{}}", :headers=>{"Content-Type"=>"application/json", "ACCOUNT_ID"=>"25"}})
22
+ it 'post' do
23
+ expect(Myfinance).to receive(:post).with('/people.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :body=>"{\"person\":{}}", :headers=>{'Content-Type' => 'application/json', 'ACCOUNT_ID' => '25'}})
24
24
  Myfinance.cria_pessoa({})
25
25
  end
26
- it "put" do
27
- expect(Myfinance).to receive(:put).with("/people/30.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :body=>"{}", :headers=>{"Content-Type"=>"application/json", "ACCOUNT_ID"=>"25"}})
26
+ it 'put' do
27
+ expect(Myfinance).to receive(:put).with('/people/30.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :body=> '{}', :headers=>{'Content-Type' => 'application/json', 'ACCOUNT_ID' => '25'}})
28
28
  Myfinance.atualiza_pessoa(30, {})
29
29
  end
30
30
  end
31
- describe "not informed" do
31
+ describe 'not informed' do
32
32
  before do
33
33
  expect(Myfinance).to receive(:accounts).and_return response_double
34
34
  expect(Myfinance).to receive(:get_account_id).and_return nil
35
- Myfinance.setup "123456", false
35
+ Myfinance.setup '123456', false
36
36
  end
37
- it "get" do
38
- expect(Myfinance).to receive(:get).with("/entities.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :headers=>{"Content-Type"=>"application/json"}})
37
+ it 'get' do
38
+ expect(Myfinance).to receive(:get).with('/entities.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :headers=>{'Content-Type' => 'application/json'}})
39
39
  Myfinance.entidades
40
40
  end
41
- it "post" do
42
- expect(Myfinance).to receive(:post).with("/people.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :body=>"{\"person\":{}}", :headers=>{"Content-Type"=>"application/json"}})
41
+ it 'post' do
42
+ expect(Myfinance).to receive(:post).with('/people.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :body=>"{\"person\":{}}", :headers=>{'Content-Type' => 'application/json'}})
43
43
  Myfinance.cria_pessoa({})
44
44
  end
45
- it "put" do
46
- expect(Myfinance).to receive(:put).with("/people/30.json", {:basic_auth=>{:username=>"123456", :password=>"x"}, :body=>"{}", :headers=>{"Content-Type"=>"application/json"}})
45
+ it 'put' do
46
+ expect(Myfinance).to receive(:put).with('/people/30.json', {:basic_auth=>{:username=> '123456', :password=> 'x'}, :body=> '{}', :headers=>{'Content-Type' => 'application/json'}})
47
47
  Myfinance.atualiza_pessoa(30, {})
48
48
  end
49
49
  end
@@ -10,7 +10,8 @@ describe 'Manipulando Contas a Receber', type: :feature do
10
10
  end
11
11
 
12
12
  it 'deve poder criar uma conta a receber para uma entidade e um cliente' do
13
- Myfinance.setup('2acecbb483842ebbfb2c638070bf019b70e757190166d277')
13
+ token = '2acecbb483842ebbfb2c638070bf019b70e757190166d277'
14
+ Myfinance.setup(token)
14
15
 
15
16
  # entidade_id = Myfinance.entidade_id('06604529878')
16
17
  entidade_id = Myfinance.entidade_id('11.023.029/0001-05')
@@ -18,6 +19,7 @@ describe 'Manipulando Contas a Receber', type: :feature do
18
19
  categoria_id = Myfinance.categoria_id('Alimentação / Restaurantes')
19
20
  centro_de_receita_id = Myfinance.centro_de_receita_id('Projetos')
20
21
 
22
+
21
23
  faturamento = {
22
24
  'entity_id' => entidade_id,
23
25
  'due_date' => (Date.today + 30),
@@ -56,10 +58,16 @@ describe 'Manipulando Contas a Receber', type: :feature do
56
58
  ]
57
59
  }
58
60
  conta_a_receber = Myfinance.cria_conta_a_receber_entidade('Minhas Finanças', faturamento)
59
- expect(conta_a_receber['receivable_account']).to_not be_nil
61
+ expect(conta_a_receber.code).to eql(201)
60
62
 
61
63
  conta_a_receber = Myfinance.cria_conta_a_receber(entidade_id, faturamento)
62
- expect(conta_a_receber['receivable_account']).to_not be_nil
64
+ expect(conta_a_receber.code).to eql(201)
65
+
66
+ # devo poder anexar um arquivo
67
+ cr_id = conta_a_receber['receivable_account']['id']
68
+ anexo = Myfinance.cria_e_anexa_arquivo(entidade_id, cr_id, 'teste.txt', '********** Conteúdo do Arquivo de Teste *************')
69
+ expect(anexo.code).to eql(201)
70
+
63
71
  end
64
72
 
65
73
  it '.conta_a_receber' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myfinance-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Lopes Neto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,28 +28,28 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
@@ -58,14 +58,28 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.13.1
61
+ version: '0.13'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.13.1
68
+ version: '0.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: httmultiparty
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: GEM para facilitar a uso da API do Myfinance em Aplicativos Rails usando
70
84
  HTTParty
71
85
  email:
@@ -82,6 +96,7 @@ files:
82
96
  - Rakefile
83
97
  - lib/myfinance.rb
84
98
  - lib/myfinance/account.rb
99
+ - lib/myfinance/anexo.rb
85
100
  - lib/myfinance/categoria.rb
86
101
  - lib/myfinance/centro_receita_custo.rb
87
102
  - lib/myfinance/conta_a_pagar.rb
@@ -103,7 +118,7 @@ files:
103
118
  - spec/features/pessoas_spec.rb
104
119
  - spec/features/webhooks_spec.rb
105
120
  - spec/spec_helper.rb
106
- homepage: ''
121
+ homepage: http://wwww.taxweb.com.br
107
122
  licenses:
108
123
  - MIT
109
124
  metadata: {}