Class: Knu

Inherits:
Object
  • Object
show all
Defined in:
lib/knu/knu.rb,
lib/knu/version.rb

Constant Summary collapse

WEBSERVICE_URL =
"https://c.knu.com.br/webservice"
RETURN_FORMATS =
{:json => 2, :xml => 1}
STATUS_OK =
"0"
FUNCTIONS =
%w(
  receitaCPF
  receitaCNPJ
  receitaSimples
  fgtsCNPJ
  ibgeCodigo
  ibgeMunicipio
  consultarEndereco
  receitaCCD
  cadespCNPJ
  consultarNfeHtml
  sintegraAC_CNPJ
  sintegraAP_CNPJ
  sintegraAM_CNPJ
  sintegraBA_CNPJ
  sintegraDF_CNPJ
  sintegraCE_CNPJ
  sintegraMS_CNPJ
  sintegraPE_CNPJ
  sintegraPB_CNPJ
  sintegraRR_CNPJ
  sintegraTO_CNPJ
  sintegraRJ_CNPJ
  sintegraSE_CNPJ
  sintegraSP_CNPJ
  sintegraSU
  denatran
  detranSP
)
VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password, format = :json) ⇒ Knu

Returns a new instance of Knu.



41
42
43
44
45
# File 'lib/knu/knu.rb', line 41

def initialize(user, password, format = :json)
  @user     = user
  @password = password
  @format   = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



39
40
41
# File 'lib/knu/knu.rb', line 39

def format
  @format
end

#passwordObject (readonly)

Returns the value of attribute password.



39
40
41
# File 'lib/knu/knu.rb', line 39

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



39
40
41
# File 'lib/knu/knu.rb', line 39

def user
  @user
end

Instance Method Details

#build_request_xml(function, param, extra = nil) ⇒ Object

builds the standard request xml



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/knu/knu.rb', line 55

def build_request_xml(function, param, extra = nil)
  xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  xml << "<dados>\n"
  xml << "  <usuario>#{@user}</usuario>\n"
  xml << "  <senha>#{@password}</senha>\n"
  xml << "  <funcao>#{function}</funcao>\n"
  xml << "  <param>#{param}</param>\n"
  xml << "  <param2>#{extra}</param2>\n" if extra
  xml << "  <retorno>#{RETURN_FORMATS[@format]}</retorno>\n"
  xml << "</dados>"
  xml
end

#handle_response(response) ⇒ Object



82
83
84
85
86
# File 'lib/knu/knu.rb', line 82

def handle_response(response)
  data = parse_response(response)
  validate_data!(data)
  data["dados"]["consulta"]["root"]
end

#parse_response(response, format = @format) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/knu/knu.rb', line 102

def parse_response(response, format = @format)
  case format
    when :json
      Crack::JSON.parse(response)
    when :xml
      Crack::XML.parse(response)
  end
end

#request(xml) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/knu/knu.rb', line 68

def request(xml)
  uri = URI.parse(WEBSERVICE_URL)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = xml

  response = http.request(request)
  handle_response(response.body)
end

#validate_data!(data) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/knu/knu.rb', line 88

def validate_data!(data)
  dados = data["dados"]
  unless (status = dados["status"]) == STATUS_OK
    raise "Error #{status}: " + dados["desc"]
  else
    root = dados["consulta"]["root"]
    if error = root["cod_erro"]
      raise "Error #{error}: " + root["desc_erro"]
    end
  end
rescue => ex
  raise "Invalid response: #{ex.message}"
end