class Nfeiow::Client

Attributes

api_key[R]
company_id[R]

Public Class Methods

new(company_id, api_key) click to toggle source
# File lib/nfeiow/client.rb, line 11
def initialize(company_id, api_key)
  @company_id = company_id
  @api_key = api_key
end

Public Instance Methods

cancel_invoice(invoice_id) click to toggle source
# File lib/nfeiow/client.rb, line 32
def cancel_invoice(invoice_id)
  safe_http_call do
    connection.delete(
      path: "/v1/companies/#{company_id}/serviceinvoices/#{invoice_id}",
      headers: headers
    )
  end
end
connection() click to toggle source
# File lib/nfeiow/client.rb, line 16
def connection
  @connection ||= Excon.new(
    'https://api.nfe.io/'
  )
end
create_invoice(params) click to toggle source
# File lib/nfeiow/client.rb, line 22
def create_invoice(params)
  safe_http_call do
    connection.post(
      path: "/v1/companies/#{company_id}/serviceinvoices",
      headers: headers,
      body: params.to_json
    )
  end
end
download_invoice_pdf(invoice_id) click to toggle source
# File lib/nfeiow/client.rb, line 41
def download_invoice_pdf(invoice_id)
  safe_http_call(true) do
    connection.get(
      path: "/v1/companies/#{company_id}/serviceinvoices/#{invoice_id}/pdf",
      headers: headers
    )
  end
end
send_invoice_via_email(invoice_id) click to toggle source
# File lib/nfeiow/client.rb, line 50
def send_invoice_via_email(invoice_id)
  safe_http_call do
    connection.put(
      path: "/v1/companies/#{company_id}/serviceinvoices/#{invoice_id}/sendemail",
      headers: headers
    )
  end
end

Private Instance Methods

parse_payload(body) click to toggle source
# File lib/nfeiow/client.rb, line 75
def parse_payload(body)
  return "Executed" if body == ""
  JSON.parse(body)
end
safe_http_call(download=false) { || ... } click to toggle source
# File lib/nfeiow/client.rb, line 61
def safe_http_call(download=false)
  response = yield

  raise response.body unless success_http_status(response.status)

  result(
    true,
    nil, 
    download ? response.headers['Location'] : parse_payload(response.body)
  )
rescue StandardError => e
  result(false, e.message, nil)
end
success_http_status(status) click to toggle source
# File lib/nfeiow/client.rb, line 80
def success_http_status(status)
  [200, 201, 202, 302].include?(status)
end