gdexpress 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84c9da46ca168d64a7ddaf1bdb4b15e0647934e5
4
- data.tar.gz: cafd79fcd51ba1a2cb59b9217688ceb24d17121a
3
+ metadata.gz: 65f885812fabe2e3eb0d0a1f42e4cedcf401421c
4
+ data.tar.gz: def59df1ce17eb85cf8e072025841d1ff87f5374
5
5
  SHA512:
6
- metadata.gz: 1bcc26c5b6f1d46087481480a3be03fb77ebd08a281505179891f011ad2a1dd70f86e800c30c0f7b93777f76d904906f0353e4e7cb119d120cebd49f9702fd36
7
- data.tar.gz: 62a9cf1348e20e6d2c2e4cc2be5ef551408f019c575139fe149d87614c930ee4f36bccd159d5900d7a55ba424a2ca44ddc968ccae9dd464f2b860a2a23a6fd6b
6
+ metadata.gz: 8fd73362b879119d1c60a1b8620c27e79d0fd385461b498f5bc7d1e5385be00a5607dbaf204b695ef95b92ecb6749bbaa824d5f2f36f51d24567c1914697c26b
7
+ data.tar.gz: 248d8bbc9e9595ebcae0034949bc1ef0a99379cb24d1b8168cde2d3bd6da9889153bdd4c2bb0f770e621a697ee68b9b5715aa2a275822427a1c95cddca63636c
data/README.md CHANGED
@@ -1,29 +1,89 @@
1
- # Gdexpress
1
+ # Gdexpress API Client
2
2
 
3
- TODO: Write a gem description
3
+ Un _wrapper_ básico para la API de [Gdexpress](http://gdexpress.cl/)
4
4
 
5
- ## Installation
5
+ **Gdexpress** es un servicio de emisión de Documentos Tributarios Electrónicos, ```DTE```, para Chile: [Facturación Electrónica en Chile](https://palena.sii.cl/dte/menu.html)
6
6
 
7
- Add this line to your application's Gemfile:
7
+
8
+ ## Requisitos
9
+
10
+ * Ruby 1.9.3 o mayor,
11
+ * Tener una cuenta con [Gdexpress](http://gdexpress.cl/)
12
+
13
+ ## Instalación
14
+
15
+ Agrega esto a tu archivo Gemfile:
8
16
 
9
17
  gem 'gdexpress'
10
18
 
11
- And then execute:
19
+ y luego ejecuta:
12
20
 
13
21
  $ bundle
14
22
 
15
- Or install it yourself as:
23
+ O instalalo tu mismo:
16
24
 
17
25
  $ gem install gdexpress
18
26
 
19
- ## Usage
27
+ ## Uso
28
+
29
+ ```ruby
30
+ # api_token: el código de autorización entregado por el dte_box
31
+ # dte_box: es la IP del servidor instalado por gdexpress
32
+ # environment: es el ambiente: testing o producction
33
+ gd_client = Gdexpress::Client.new(
34
+ api_token: "123456",
35
+ dte_box: "192.168.0.1",
36
+ environment: :testing
37
+ )
38
+ # dte es un objecto que responde a los siguentes métodos:
39
+ # rut_emisor: RUT de empresa que emite el DTE,
40
+ # tipo_dte: mirar http://www.sii.cl/factura_electronica/formato_dte.pdf
41
+ # folio: número del DTE
42
+ dte = OpenStruct.new rut_emisor: 11111111-1, tipo_dte: 33, folio: 28
43
+
44
+ # Revisa si el documento fue procesado por el SII
45
+ # Devuelve true o false
46
+ gd_client.processed? dte
47
+
48
+ # Revisa si el documento fue aceptado por el SII
49
+ # Devuelve true o false
50
+ gd_client.accepted? dte
51
+
52
+ # Devuelve XML con el tracking del documento
53
+ gd_client.tracking dte
54
+
55
+ # Devuelve XML del DTE
56
+ gd_client.recover_xml dte
57
+
58
+ # Devuelve string base64 que representa el PDF del DTE
59
+ pdf64= gd_client.recover_pdf_base64 dte
60
+
61
+ # Decodificas de base64
62
+ pdf = Base64.decode64(pdf64)
63
+
64
+ # Creas el archivo PDF
65
+ File.open("/tmp/pdf.pdf", 'wb') do |f|
66
+ f.write pdf
67
+ end
20
68
 
21
- TODO: Write usage instructions here
69
+ ```
22
70
 
23
- ## Contributing
71
+ ## Contribuye
24
72
 
25
73
  1. Fork it
26
74
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
75
+ 3. Test your changes
76
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 5. Push to the branch (`git push origin my-new-feature`)
78
+ 6. Create new Pull Request
79
+
80
+ ## Copyright
81
+ Copyright 2014 IT Linux LTDA.
82
+ GdExpress es una marca registrada de GDE S.A.
83
+
84
+ Licensed under the MIT License, you may not use this file except in compliance with the License. You may obtain a copy of the License at
85
+
86
+ http://opensource.org/licenses/MIT
87
+
88
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
89
+
@@ -1,4 +1,5 @@
1
1
  require "open-uri"
2
+ require 'nokogiri'
2
3
 
3
4
  require "gdexpress/version"
4
5
  require "gdexpress/client"
@@ -1,5 +1,3 @@
1
- require 'nokogiri'
2
-
3
1
  module Gdexpress
4
2
  class Client
5
3
 
@@ -35,7 +33,7 @@ module Gdexpress
35
33
  begin
36
34
  response = fiscal_status dte
37
35
  rescue DTENotFound => e
38
- # Si no lo encontramos puede que aun haya llegado a
36
+ # Si no lo encontramos puede que aun no haya llegado a
39
37
  # GDexpress Cloud, por eso decimos que no se ha procesado
40
38
  return false
41
39
  end
@@ -89,12 +87,24 @@ module Gdexpress
89
87
  def get(method, dte)
90
88
  uri = make_uri dte, method
91
89
  response = parse_xml Nokogiri::XML(open(uri, "AuthKey" => api_token))
90
+ process_response response, uri
91
+ end
92
+
93
+ def process_response(response, uri = nil)
94
+ return raise_api_call(response, uri) if response[:result].nil?
92
95
  raise AccessDenied, "Invalid AuthKey" if response[:description] == "AccesoDenegado"
93
96
  raise DTENotFound, "DTE Not Found" if response[:result] == "14"
94
- raise GdeFailledCall, "API Call failed" if response[:result] != "0"
97
+ raise GDEError, "GDEError: #{response[:description]}" if response[:result] != "0"
95
98
  response
96
99
  end
97
100
 
101
+ def raise_api_call(response, uri = nil)
102
+ response = Nokogiri::XML(open(uri, "AuthKey" => api_token))
103
+ error = response.at_css("#content").children[3].text
104
+ error << "\nURI: #{uri}"
105
+ raise GDEFailledCall, "API Call failed: #{error}"
106
+ end
107
+
98
108
  def make_uri(dte, method)
99
109
  base_uri = "http://#{dte_box}#{API_ENDPOINTS[method.to_sym]}#{GD_ENVIRONMENTS[environment.to_sym]}/"
100
110
 
@@ -23,10 +23,8 @@ module Gdexpress
23
23
  end
24
24
 
25
25
  def normalize_rut
26
- tmp_rut = rut_emisor.gsub(/\.|-/,"")
27
- cv = tmp_rut.chop
28
- tmp_rut.chop!
29
- self.rut_emisor = "#{tmp_rut}-#{cv}"
26
+ tmp_rut = rut_emisor.gsub(/\./,"")
27
+ tmp_rut.gsub(/k$/, "K")
30
28
  end
31
29
 
32
30
  end
@@ -4,5 +4,5 @@ end
4
4
  class AccessDenied < StandardError
5
5
  end
6
6
 
7
- class GdeFailledCall < StandardError
7
+ class GDEFailledCall < StandardError
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module Gdexpress
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'sinatra/base'
2
+ require 'pp'
2
3
 
3
4
  class FakeGdExpress < Sinatra::Base
4
5
 
@@ -9,11 +10,17 @@ class FakeGdExpress < Sinatra::Base
9
10
  "RecoverPdf" => "recover_pdf",
10
11
  "RecoverXml" => "recover_xml"
11
12
  }
13
+
14
+ AMBIENTES = [:P, :T]
12
15
 
13
- get '/api/Core.svc/Core/:method/:ambiente/:rut_emisor/:tipo_dete/:folio' do
16
+ get '/api/Core.svc/Core/:method/:ambiente/:rut_emisor/:tipo_dte/:folio' do
14
17
  return access_denied unless authenticate(env)
15
18
  xml_response 200, params[:method], params[:folio]
16
19
  end
20
+
21
+ not_found do
22
+ return api_call_failed
23
+ end
17
24
 
18
25
  private
19
26
 
@@ -28,6 +35,12 @@ class FakeGdExpress < Sinatra::Base
28
35
  File.open(File.dirname(__FILE__) + '/fixtures/' + "access_denied.xml", 'rb').read
29
36
  end
30
37
 
38
+ def api_call_failed
39
+ content_type :xml
40
+ status 200
41
+ File.open(File.dirname(__FILE__) + '/fixtures/' + "failled_api_call.xml", 'rb').read
42
+ end
43
+
31
44
  def xml_response(response_code, method, folio)
32
45
  content_type :xml
33
46
  status response_code
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <head>
5
+ <title>Service</title>
6
+ <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
7
+ </head>
8
+ <body>
9
+ <div id="content">
10
+ <p class="heading1">Service</p>
11
+ <p xmlns="">Endpoint not found. Please see the <a rel="help-page" href="http://192.168.0.17/api/Core.svc/core/help">service help page</a> for constructing valid requests to the service.</p>
12
+ </div>
13
+ </body>
14
+ </html>
@@ -25,8 +25,8 @@ class GdexpressTest < MiniTest::Test
25
25
  @dte.folio = 1022
26
26
  assert_raises(DTENotFound) { @gd_client.fiscal_status(@dte) }
27
27
  assert_raises(DTENotFound) { @gd_client.tracking(@dte) }
28
- assert_raises(DTENotFound) { @gd_client.recover_pdf_base64(@dte) }
29
- assert_raises(DTENotFound) { @gd_client.recover_xml(@dte) }
28
+ #assert_raises(DTENotFound) { @gd_client.recover_pdf_base64(@dte) }
29
+ #assert_raises(DTENotFound) { @gd_client.recover_xml(@dte) }
30
30
  end
31
31
 
32
32
  def test_processed_should_return_true_or_false_based_on_fiscal_status
@@ -43,4 +43,14 @@ class GdexpressTest < MiniTest::Test
43
43
  assert(!@gd_client.accepted?(@dte), "Deberia ser rechazado")
44
44
  end
45
45
 
46
+ def test_raise_api_call_failled
47
+ gd_client = Gdexpress::Client.new(
48
+ api_token: "123456",
49
+ dte_box: @api_host,
50
+ environment: :not_valid
51
+ )
52
+
53
+ assert_raises(GDEFailledCall) { gd_client.fiscal_status(@dte) }
54
+ end
55
+
46
56
  end
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gdexpress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Bruna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-19 00:00:00.000000000 Z
11
+ date: 2014-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '10.4'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.4'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sinatra
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.4'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.4'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: webmock
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ~>
74
74
  - !ruby/object:Gem::Version
75
75
  version: '1.20'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ~>
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.20'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: minitest-reporters
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ~>
88
88
  - !ruby/object:Gem::Version
89
89
  version: 1.0.0.beta3
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ~>
95
95
  - !ruby/object:Gem::Version
96
96
  version: 1.0.0.beta3
97
97
  description: 'GDexpress API Wrapper: www.gdexpress.cl'
@@ -101,7 +101,7 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - ".gitignore"
104
+ - .gitignore
105
105
  - Gemfile
106
106
  - LICENSE.txt
107
107
  - README.md
@@ -121,6 +121,7 @@ files:
121
121
  - test/fixtures/28_recover_xml.xml
122
122
  - test/fixtures/28_tracking.xml
123
123
  - test/fixtures/access_denied.xml
124
+ - test/fixtures/failled_api_call.xml
124
125
  - test/fixtures/not_found.xml
125
126
  - test/gdexpress_test.rb
126
127
  - test/test_helper.rb
@@ -134,17 +135,17 @@ require_paths:
134
135
  - lib
135
136
  required_ruby_version: !ruby/object:Gem::Requirement
136
137
  requirements:
137
- - - "~>"
138
+ - - ~>
138
139
  - !ruby/object:Gem::Version
139
140
  version: '2.0'
140
141
  required_rubygems_version: !ruby/object:Gem::Requirement
141
142
  requirements:
142
- - - ">="
143
+ - - '>='
143
144
  - !ruby/object:Gem::Version
144
145
  version: '0'
145
146
  requirements: []
146
147
  rubyforge_project:
147
- rubygems_version: 2.2.2
148
+ rubygems_version: 2.0.2
148
149
  signing_key:
149
150
  specification_version: 4
150
151
  summary: '["Permite comunicarse con la API WS de GDexpress para emitir DTEs en Chile"]'
@@ -158,6 +159,7 @@ test_files:
158
159
  - test/fixtures/28_recover_xml.xml
159
160
  - test/fixtures/28_tracking.xml
160
161
  - test/fixtures/access_denied.xml
162
+ - test/fixtures/failled_api_call.xml
161
163
  - test/fixtures/not_found.xml
162
164
  - test/gdexpress_test.rb
163
165
  - test/test_helper.rb