notasegura 0.0.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.
Files changed (2) hide show
  1. data/lib/notasegura.rb +77 -0
  2. metadata +46 -0
data/lib/notasegura.rb ADDED
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rest-client'
4
+ require 'xmlsimple'
5
+
6
+ class AuthError < Exception
7
+ end
8
+
9
+ class RequestError < Exception
10
+ end
11
+
12
+ class NotaSegura
13
+
14
+
15
+ def initialize(email, senha)
16
+ if email.nil? or not email.is_a? String or email.empty?
17
+ raise ArgumentError, 'Usuário inválido'
18
+ end
19
+
20
+ if senha.nil? or not senha.is_a? String or senha.empty?
21
+ raise ArgumentError, 'Senha inválida'
22
+ end
23
+
24
+ @url = 'https://app.notasegura.com.br/api/invoices'
25
+ @email = email
26
+ @senha = senha
27
+ end
28
+
29
+ def token=(token)
30
+ @token = token
31
+ end
32
+
33
+ def enviar_xml(xml)
34
+ @xml = xml
35
+
36
+ ler_xml!
37
+ checar_xml!
38
+
39
+ begin
40
+ @url = "#{@url}?token=#{@token}" if @token
41
+ requisicao = RestClient::Request.new(
42
+ :method => :post,
43
+ :url => @url,
44
+ :user => @email,
45
+ :password => @senha
46
+ )
47
+ resposta = requisicao.execute
48
+ resultado = XmlSimple.xml_in resposta
49
+ return resultado
50
+ rescue => e
51
+ if e.message.include? '401'
52
+ raise AuthError, 'Erro ao autenticar: token inválido'
53
+ else
54
+ raise RequestError, "Erro ao enviar XML: #{e}"
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def ler_xml!
62
+ if File.file? @xml
63
+ begin
64
+ arquivo = File.open @xml, 'rb'
65
+ @xml = arquivo.read
66
+ arquivo.close
67
+ rescue => e
68
+ raise TypeError, "Erro ao ler arquivo: #{e.message}"
69
+ end
70
+ end
71
+ end
72
+
73
+ def checar_xml!
74
+ raise TypeError, 'Arquivo inválido: não é XML' unless @xml.include? '<?xml'
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notasegura
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - notasegura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-04-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Integração com notasegura.com.br
15
+ email:
16
+ - contato@notasegura.com.br
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/notasegura.rb
22
+ homepage: http://notasegura.com.br/
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.11
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Integração com notasegura.com.br
46
+ test_files: []