knu 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +36 -0
- data/README.md +19 -0
- data/Rakefile +7 -0
- data/knu.gemspec +30 -0
- data/lib/knu.rb +5 -0
- data/lib/knu/knu.rb +108 -0
- data/lib/knu/version.rb +3 -0
- data/spec/knu/knu_spec.rb +113 -0
- data/spec/spec_helper.rb +3 -0
- metadata +168 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
knu (0.0.1)
|
5
|
+
crack
|
6
|
+
httpi
|
7
|
+
nokogiri
|
8
|
+
rack
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
crack (0.3.2)
|
14
|
+
diff-lcs (1.1.3)
|
15
|
+
httpi (2.0.2)
|
16
|
+
rack
|
17
|
+
nokogiri (1.5.6)
|
18
|
+
rack (1.5.2)
|
19
|
+
rake (10.0.3)
|
20
|
+
rspec (2.12.0)
|
21
|
+
rspec-core (~> 2.12.0)
|
22
|
+
rspec-expectations (~> 2.12.0)
|
23
|
+
rspec-mocks (~> 2.12.0)
|
24
|
+
rspec-core (2.12.2)
|
25
|
+
rspec-expectations (2.12.1)
|
26
|
+
diff-lcs (~> 1.1.3)
|
27
|
+
rspec-mocks (2.12.1)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
knu!
|
34
|
+
nokogiri
|
35
|
+
rake
|
36
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
KNU
|
2
|
+
=============
|
3
|
+
|
4
|
+
Client de consulta do KNU via WebService. Mais informações: https://knu.com.br/ajuda/documentacao/webservice
|
5
|
+
|
6
|
+
Instalação
|
7
|
+
------------
|
8
|
+
|
9
|
+
gem install knu
|
10
|
+
|
11
|
+
Utilização
|
12
|
+
----------
|
13
|
+
|
14
|
+
require "knu"
|
15
|
+
|
16
|
+
knu = Knu.new("usuario@server.com", "senhaknu")
|
17
|
+
knu.receitaCPF("12345678900")
|
18
|
+
|
19
|
+
# {"@metodo"=>"receitaCPF", "nome"=>"FULANO DE TAL", "situacao"=>"REGULAR"}
|
data/Rakefile
ADDED
data/knu.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require "knu/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "knu"
|
8
|
+
gem.version = Knu::VERSION
|
9
|
+
gem.date = "2012-03-12"
|
10
|
+
gem.summary = "Client Knu Webservice"
|
11
|
+
gem.description = "Client Knu Webservice"
|
12
|
+
gem.authors = ["Rodrigo Panachi"]
|
13
|
+
gem.email = "rpanachi@gmail.com"
|
14
|
+
gem.files = ["lib/knu.rb"]
|
15
|
+
gem.homepage = 'https://github.com/rpanachi/knu'
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split("\n")
|
18
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
|
22
|
+
gem.add_runtime_dependency "rack"
|
23
|
+
gem.add_runtime_dependency "httpi"
|
24
|
+
gem.add_runtime_dependency "nokogiri"
|
25
|
+
gem.add_runtime_dependency "crack"
|
26
|
+
|
27
|
+
gem.add_development_dependency "rake"
|
28
|
+
gem.add_development_dependency "rspec"
|
29
|
+
gem.add_development_dependency "nokogiri"
|
30
|
+
end
|
data/lib/knu.rb
ADDED
data/lib/knu/knu.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
|
3
|
+
class Knu
|
4
|
+
|
5
|
+
WEBSERVICE_URL = "https://c.knu.com.br/webservice"
|
6
|
+
RETURN_FORMATS = {:json => 2, :xml => 1}
|
7
|
+
STATUS_OK = "0"
|
8
|
+
|
9
|
+
FUNCTIONS = %w(
|
10
|
+
receitaCPF
|
11
|
+
receitaCNPJ
|
12
|
+
receitaSimples
|
13
|
+
fgtsCNPJ
|
14
|
+
ibgeCodigo
|
15
|
+
ibgeMunicipio
|
16
|
+
consultarEndereco
|
17
|
+
receitaCCD
|
18
|
+
cadespCNPJ
|
19
|
+
consultarNfeHtml
|
20
|
+
sintegraAC_CNPJ
|
21
|
+
sintegraAP_CNPJ
|
22
|
+
sintegraAM_CNPJ
|
23
|
+
sintegraBA_CNPJ
|
24
|
+
sintegraDF_CNPJ
|
25
|
+
sintegraCE_CNPJ
|
26
|
+
sintegraMS_CNPJ
|
27
|
+
sintegraPE_CNPJ
|
28
|
+
sintegraPB_CNPJ
|
29
|
+
sintegraRR_CNPJ
|
30
|
+
sintegraTO_CNPJ
|
31
|
+
sintegraRJ_CNPJ
|
32
|
+
sintegraSE_CNPJ
|
33
|
+
sintegraSP_CNPJ
|
34
|
+
sintegraSU
|
35
|
+
denatran
|
36
|
+
detranSP
|
37
|
+
)
|
38
|
+
|
39
|
+
attr_reader :user, :password, :format
|
40
|
+
|
41
|
+
def initialize(user, password, format = :json)
|
42
|
+
@user = user
|
43
|
+
@password = password
|
44
|
+
@format = format
|
45
|
+
end
|
46
|
+
|
47
|
+
FUNCTIONS.each do |function|
|
48
|
+
define_method(function.to_sym) do |param, extra = nil|
|
49
|
+
data = build_request_xml(function, param, extra)
|
50
|
+
response = request(data)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# builds the standard request xml
|
55
|
+
def build_request_xml(function, param, extra = nil)
|
56
|
+
xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
|
57
|
+
xml << "<dados>\n"
|
58
|
+
xml << " <usuario>#{@user}</usuario>\n"
|
59
|
+
xml << " <senha>#{@password}</senha>\n"
|
60
|
+
xml << " <funcao>#{function}</funcao>\n"
|
61
|
+
xml << " <param>#{param}</param>\n"
|
62
|
+
xml << " <param2>#{extra}</param2>\n" if extra
|
63
|
+
xml << " <retorno>#{RETURN_FORMATS[@format]}</retorno>\n"
|
64
|
+
xml << "</dados>"
|
65
|
+
xml
|
66
|
+
end
|
67
|
+
|
68
|
+
def request(xml)
|
69
|
+
request = HTTPI::Request.new(WEBSERVICE_URL)
|
70
|
+
request.body = xml
|
71
|
+
request.open_timeout = 10 # sec
|
72
|
+
request.read_timeout = 30 # sec
|
73
|
+
request.auth.ssl.verify_mode = :none
|
74
|
+
|
75
|
+
response = HTTPI.post(request)
|
76
|
+
handle_response(response.body)
|
77
|
+
end
|
78
|
+
|
79
|
+
def handle_response(response)
|
80
|
+
data = parse_response(response)
|
81
|
+
validate_data!(data)
|
82
|
+
data["dados"]["consulta"]["root"]
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_data!(data)
|
86
|
+
dados = data["dados"]
|
87
|
+
unless (status = dados["status"]) == STATUS_OK
|
88
|
+
raise "Error #{status}: " + dados["desc"]
|
89
|
+
else
|
90
|
+
root = dados["consulta"]["root"]
|
91
|
+
if error = root["cod_erro"]
|
92
|
+
raise "Error #{error}: " + root["desc_erro"]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
rescue => ex
|
96
|
+
raise "Invalid response: #{ex.message}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_response(response, format = @format)
|
100
|
+
case format
|
101
|
+
when :json
|
102
|
+
Crack::JSON.parse(response)
|
103
|
+
when :xml
|
104
|
+
Crack::XML.parse(response)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/lib/knu/version.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Knu do
|
6
|
+
|
7
|
+
subject { Knu.new("user@server.com", "1234") }
|
8
|
+
|
9
|
+
it { should respond_to(:user) }
|
10
|
+
it { should respond_to(:password) }
|
11
|
+
it { should respond_to(:format) }
|
12
|
+
|
13
|
+
it "format json as default" do
|
14
|
+
subject.format.should == :json
|
15
|
+
end
|
16
|
+
|
17
|
+
context "request" do
|
18
|
+
|
19
|
+
context "build xml" do
|
20
|
+
before do
|
21
|
+
request_xml = subject.build_request_xml("function", "000")
|
22
|
+
@doc = Nokogiri.XML(request_xml)
|
23
|
+
end
|
24
|
+
it { @doc.xpath("//dados/usuario").first.content.should == subject.user }
|
25
|
+
it { @doc.xpath("//dados/senha").first.content.should == subject.password }
|
26
|
+
it { @doc.xpath("//dados/funcao").first.content.should == "function" }
|
27
|
+
it { @doc.xpath("//dados/param").first.content.should == "000" }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "handle" do
|
31
|
+
|
32
|
+
let :valid_response do
|
33
|
+
%(
|
34
|
+
<?xml version="1.0" encoding="utf-8"?>
|
35
|
+
<dados>
|
36
|
+
<status>0</status>
|
37
|
+
<desc></desc>
|
38
|
+
<consulta>
|
39
|
+
<root metodo="receitaCNPJ" arg="00000000000191">
|
40
|
+
<numero_inscricao>00.000.000/0001-91</numero_inscricao>
|
41
|
+
<data_abertura>01/08/1966</data_abertura>
|
42
|
+
<nome_empresarial>BANCO DO BRASIL SA</nome_empresarial>
|
43
|
+
</root>
|
44
|
+
</consulta>
|
45
|
+
</dados>
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
let :invalid_response do
|
50
|
+
%(
|
51
|
+
<?xml version="1.0" encoding="utf-8"?>
|
52
|
+
<dados>
|
53
|
+
<status>55</status>
|
54
|
+
<desc>Xml inválido</desc>
|
55
|
+
<consulta></consulta>
|
56
|
+
</dados>
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
let :invalid_response_content do
|
61
|
+
%(
|
62
|
+
<?xml version="1.0" encoding="utf-8"?>
|
63
|
+
<dados>
|
64
|
+
<status>0</status>
|
65
|
+
<desc></desc>
|
66
|
+
<consulta>
|
67
|
+
<root metodo="receitaCNPJ" arg="00000000000190">
|
68
|
+
<cod_erro>6</cod_erro>
|
69
|
+
<desc_erro>Erro codigo 6: Parametro invalido.</desc_erro>
|
70
|
+
</root>
|
71
|
+
</consulta>
|
72
|
+
</dados>
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
context "parse_response" do
|
77
|
+
it "xml" do
|
78
|
+
data = subject.parse_response(valid_response, :xml)
|
79
|
+
data["dados"].should_not be_empty
|
80
|
+
end
|
81
|
+
|
82
|
+
it "json" do
|
83
|
+
data = subject.parse_response(valid_response, :json)
|
84
|
+
data["dados"].should_not be_empty
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "validate data" do
|
89
|
+
it "successfull" do
|
90
|
+
data = Crack::XML.parse(valid_response)
|
91
|
+
expect { subject.validate_data!(data) }.to_not raise_error
|
92
|
+
end
|
93
|
+
it "error category 1" do
|
94
|
+
data = Crack::XML.parse(invalid_response)
|
95
|
+
expect { subject.validate_data!(data) }.to raise_error
|
96
|
+
end
|
97
|
+
it "error category 2" do
|
98
|
+
data = Crack::XML.parse(invalid_response_content)
|
99
|
+
expect { subject.validate_data!(data) }.to raise_error
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "response" do
|
104
|
+
subject { Knu.new("email@server.com", "1234", :xml) }
|
105
|
+
it "successfull" do
|
106
|
+
result = subject.handle_response(valid_response)
|
107
|
+
result["numero_inscricao"].should == "00.000.000/0001-91"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rodrigo Panachi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httpi
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: nokogiri
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: crack
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: nokogiri
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: Client Knu Webservice
|
127
|
+
email: rpanachi@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- Gemfile
|
134
|
+
- Gemfile.lock
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- knu.gemspec
|
138
|
+
- lib/knu.rb
|
139
|
+
- lib/knu/knu.rb
|
140
|
+
- lib/knu/version.rb
|
141
|
+
- spec/knu/knu_spec.rb
|
142
|
+
- spec/spec_helper.rb
|
143
|
+
homepage: https://github.com/rpanachi/knu
|
144
|
+
licenses: []
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ! '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
157
|
+
requirements:
|
158
|
+
- - ! '>='
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
requirements: []
|
162
|
+
rubyforge_project:
|
163
|
+
rubygems_version: 1.8.24
|
164
|
+
signing_key:
|
165
|
+
specification_version: 3
|
166
|
+
summary: Client Knu Webservice
|
167
|
+
test_files: []
|
168
|
+
has_rdoc:
|