module Emites

Constants

VERSION

Attributes

endpoint[RW]
token[RW]

Public Class Methods

clean_cnpj(cnpj) click to toggle source
# File lib/emites.rb, line 24
def self.clean_cnpj(cnpj)
  cnpj.gsub('.','').gsub('/','').gsub('-','').gsub(' ','')
end
cria_lote(cnpj, nome) click to toggle source
# File lib/emites/emissao.rb, line 3
def self.cria_lote(cnpj, nome)
  emitente_id = Emites.emitter_id(cnpj)
  lote = {
      emitter_id: emitente_id,
      name: nome
  }
  response = lpost '/api/v1/batches', lote
  response
end
cria_servico(servico) click to toggle source

criar modelo de servico

# File lib/emites/servico.rb, line 18
def self.cria_servico(servico)
  response = lpost '/api/v1/service-values', servico
  response
end
emite_nfs(nfs, lote = nil) click to toggle source
# File lib/emites/emissao.rb, line 13
def self.emite_nfs(nfs, lote = nil)
  nfs['batch_name'] = lote if lote
  response = lpost '/api/v1/nfse', nfs
  puts response.inspect unless response['number'].present?
  response
end
emitente(cnpj) click to toggle source
# File lib/emites/emitente.rb, line 10
def self.emitente(cnpj)
  id = emitente_id(cnpj)
  response = lget "/api/v1/emitters/#{id}"
  response.parsed_response
end
emitente_id(cnpj) click to toggle source
# File lib/emites/emitente.rb, line 3
def self.emitente_id(cnpj)
  cnpj = clean_cnpj(cnpj)
  emitter = lget "/api/v1/emitters?cnpj=#{cnpj}"
  # assumindo que volta apenas um registro na collection
  emitter["collection"][0]['id']
end
emitentes() click to toggle source
# File lib/emites/emitente.rb, line 16
def self.emitentes
  lget '/api/v1/emitters'
end
servico_id(nome) click to toggle source

pego o id do servico pelo seu nome

# File lib/emites/servico.rb, line 4
def self.servico_id(nome)
  servico_id = nil
  response = lget '/api/v1/service-values'
  servicos = response['collection']
  servicos.each do |servico|
    if servico['name'] == nome
      servico_id = servico['id']
      break
    end
  end
  servico_id
end
setup(token,production) click to toggle source
# File lib/emites.rb, line 14
def self.setup(token,production)
  if production
    @endpoint = 'https://app.emites.com.br'
  else
    @endpoint = 'https://sandbox.emites.com.br'
  end
  base_uri @endpoint
  @token = token
end
tomador(cnpj) click to toggle source
# File lib/emites/tomador.rb, line 16
def self.tomador(cnpj)
  id = taker_id(cnpj)
  lget "/api/v1/emitters/#{id}"
end
tomador_id(cnpj) click to toggle source
# File lib/emites/tomador.rb, line 3
def self.tomador_id(cnpj)
  cnpj = clean_cnpj(cnpj)
  emitter = lget "/api/v1/takers?cnpj=#{cnpj}"
  id = nil
  # assumindo que volta apenas um registro na collection
  if emitter and emitter["collection"] and emitter["collection"][0]
    id = emitter["collection"][0]['id']
  else
    puts "Não encontramos o tomador #{cnpj}"
  end
  id
end

Private Class Methods

format_time(dt) click to toggle source
# File lib/emites.rb, line 57
def self.format_time(dt)
  dt.strftime('%FT%H:%MZ') rescue nil
end
lget(url) click to toggle source
# File lib/emites.rb, line 30
def self.lget(url)
  options = {
      :basic_auth => {:username => @token, :password => 'x'},
  }
  get url, options
end
lpost(url,data) click to toggle source
# File lib/emites.rb, line 37
def self.lpost(url,data)
  options = {
      :basic_auth => {:username => @token, :password => 'x'},
      :body => data.to_json,
      :headers => { 'Content-Type' => 'application/json' }
  }
  response = post url, options
  response
end
lput(url,post_data) click to toggle source
# File lib/emites.rb, line 47
def self.lput(url,post_data)
  options = {
      :basic_auth => {:username => @token, :password => 'x'},
      :body => post_data.to_json,
      :headers => { 'Content-Type' => 'application/json' }
  }
  response = put url, options
  response
end