knu 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,16 @@
2
2
 
3
3
  class Knu
4
4
 
5
+ class InvalidRequest < Exception
6
+ attr_reader :code
7
+ def initialize(code, message)
8
+ @code = code
9
+ super(message)
10
+ end
11
+ end
12
+
13
+ class InvalidQuery < InvalidRequest; end
14
+
5
15
  WEBSERVICE_URL = "https://c.knu.com.br/webservice"
6
16
  RETURN_FORMATS = {:json => 2, :xml => 1}
7
17
  STATUS_OK = "0"
@@ -82,17 +92,18 @@ class Knu
82
92
  def handle_response(response)
83
93
  data = parse_response(response)
84
94
  validate_data!(data)
95
+
85
96
  data["dados"]["consulta"]["root"]
86
97
  end
87
98
 
88
99
  def validate_data!(data)
89
100
  dados = data["dados"]
90
101
  unless (status = dados["status"]) == STATUS_OK
91
- raise "Error #{status}: " + dados["desc"]
102
+ raise InvalidRequest.new(status, dados["desc"])
92
103
  else
93
104
  root = dados["consulta"]["root"]
94
105
  if error = root["cod_erro"]
95
- raise "Error #{error}: " + root["desc_erro"]
106
+ raise InvalidQuery.new(error, root["desc_erro"])
96
107
  end
97
108
  end
98
109
  rescue => ex
@@ -1,3 +1,3 @@
1
1
  class Knu
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -92,11 +92,30 @@ describe Knu do
92
92
  end
93
93
  it "error category 1" do
94
94
  data = Crack::XML.parse(invalid_response)
95
- expect { subject.validate_data!(data) }.to raise_error
95
+ expect { subject.validate_data!(data) }.to raise_error(Knu::InvalidRequest)
96
96
  end
97
97
  it "error category 2" do
98
98
  data = Crack::XML.parse(invalid_response_content)
99
- expect { subject.validate_data!(data) }.to raise_error
99
+ expect { subject.validate_data!(data) }.to raise_error(Knu::InvalidQuery)
100
+ end
101
+ it "unknow error" do
102
+ data = {"invalid" => "response"}
103
+ expect { subject.validate_data!(data) }.to raise_error(RuntimeError)
104
+ end
105
+
106
+ it "InvalidResquest contains code and message" do
107
+ data = Crack::XML.parse(invalid_response)
108
+ expect { subject.validate_data!(data) }.to raise_error { |ex|
109
+ ex.code.should == "55"
110
+ ex.message.should == "Xml inválido"
111
+ }
112
+ end
113
+ it "InvalidQuery contains code and message" do
114
+ data = Crack::XML.parse(invalid_response_content)
115
+ expect { subject.validate_data!(data) }.to raise_error { |ex|
116
+ ex.code.should == "6"
117
+ ex.message.should == "Erro codigo 6: Parametro invalido."
118
+ }
100
119
  end
101
120
  end
102
121
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: