Class: NotaSegura

Inherits:
Object
  • Object
show all
Defined in:
lib/notasegura.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, senha) ⇒ NotaSegura

Returns a new instance of NotaSegura.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/notasegura.rb', line 14

def initialize(email, senha)
  if email.nil? or not email.is_a? String or email.empty?
    raise ArgumentError, 'Usuário inválido' 
  end

  if senha.nil? or not senha.is_a? String or senha.empty? 
    raise ArgumentError, 'Senha inválida'
  end

  @url = 'https://app.notasegura.com.br'
  @email = email
  @senha = senha
end

Instance Method Details

#consultar_nota(chave) ⇒ Object



54
55
56
57
58
59
# File 'lib/notasegura.rb', line 54

def consultar_nota(chave)
  url = "#{@url}/api/invoices/key/#{chave}"
  resposta = RestClient::Resource.new(url, @email, @senha).get
  resultado = XmlSimple.xml_in resposta
  return resultado
end

#enviar_anexo(hash, arquivo, nome) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/notasegura.rb', line 78

def enviar_anexo(hash, arquivo, nome)
  begin
    url = "#{@url}/api/uploads"
    url = "#{url}?token=#{@token}" if @token
    requisicao = RestClient::Resource.new(url, @email, @senha)
    body = {:hash => hash, :file => arquivo}
    body['name'] = nome if nome
    resposta = requisicao.post(body)
    resultado = XmlSimple.xml_in resposta
    return resultado
  rescue => e
    if e.message.include? '401'
      raise AuthError, 'Erro ao autenticar: token inválido'
    else
      raise RequestError, "Erro ao enviar anexo: #{e}"
    end
  end
end

#enviar_xml(xml) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/notasegura.rb', line 32

def enviar_xml(xml)
  @xml = xml

  ler_xml!
  checar_xml!

  begin
    url = "#{@url}/api/invoices"
    url = "#{url}?token=#{@token}" if @token
    requisicao = RestClient::Resource.new(url, @email, @senha)
    resposta = requisicao.post(:xml => @xml)
    resultado = XmlSimple.xml_in resposta
    return resultado
  rescue => e
    if e.message.include? '401'
      raise AuthError, 'Erro ao autenticar: token inválido'
    else
      raise RequestError, "Erro ao enviar XML: #{e}"
    end
  end
end

#reenviar_email(hash, email) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/notasegura.rb', line 61

def reenviar_email(hash, email)
  begin
    url = "#{@url}/api/invoices/resend"
    url = "#{url}?token=#{@token}" if @token
    requisicao = RestClient::Resource.new(url, @email, @senha)
    resposta = requisicao.post({:email => email, :hash => hash})
    resultado = XmlSimple.xml_in resposta
    return resultado
  rescue => e
    if e.message.include? '401'
      raise AuthError, 'Erro ao autenticar: token inválido'
    else
      raise RequestError, "Erro ao reenviar email: #{e}"
    end
  end
end

#token=(token) ⇒ Object



28
29
30
# File 'lib/notasegura.rb', line 28

def token=(token)
  @token = token
end