Class: Knu
- Inherits:
-
Object
- Object
- Knu
- Defined in:
- lib/knu/knu.rb,
lib/knu/version.rb
Defined Under Namespace
Classes: InvalidQuery, InvalidRequest
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.4"
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#build_request_xml(function, param, extra = nil) ⇒ Object
builds the standard request xml.
- #handle_response(response) ⇒ Object
-
#initialize(user, password, format = :json) ⇒ Knu
constructor
A new instance of Knu.
- #parse_response(response, format = @format) ⇒ Object
- #request(xml) ⇒ Object
- #validate_data!(data) ⇒ Object
Constructor Details
#initialize(user, password, format = :json) ⇒ Knu
Returns a new instance of Knu.
51 52 53 54 55 |
# File 'lib/knu/knu.rb', line 51 def initialize(user, password, format = :json) @user = user @password = password @format = format end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
49 50 51 |
# File 'lib/knu/knu.rb', line 49 def format @format end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
49 50 51 |
# File 'lib/knu/knu.rb', line 49 def password @password end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
49 50 51 |
# File 'lib/knu/knu.rb', line 49 def user @user end |
Instance Method Details
#build_request_xml(function, param, extra = nil) ⇒ Object
builds the standard request xml
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/knu/knu.rb', line 65 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
92 93 94 95 96 97 |
# File 'lib/knu/knu.rb', line 92 def handle_response(response) data = parse_response(response) validate_data!(data) data["dados"]["consulta"]["root"] end |
#parse_response(response, format = @format) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/knu/knu.rb', line 113 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
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/knu/knu.rb', line 78 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
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/knu/knu.rb', line 99 def validate_data!(data) dados = data["dados"] unless (status = dados["status"]) == STATUS_OK raise InvalidRequest.new(status, dados["desc"]) else root = dados["consulta"]["root"] if error = root["cod_erro"] raise InvalidQuery.new(error, root["desc_erro"]) end end rescue => ex raise "Invalid response: #{ex.}" end |