consulta_sunat 0.1.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 60b9f11fdf4f099835c34e1191976413c42a523c350c05fe958d948efa9398e2
4
+ data.tar.gz: 801e0c02ddd81470f93c8ec2a12c68d9d95e098c4919ef81121c9539dc976028
5
+ SHA512:
6
+ metadata.gz: bb1aa26c6b3c3df800ecd5155b5855b593a3edf325a723427d41a365d62228a29a230d1ba5ad929dcf91b245aa4f2bb42cd82e1acd678798fd40965a19cf1dfd
7
+ data.tar.gz: fe9aeaf14cb48dbff82a9e4e59cc1a0f0ed7a0513294004f142945e19823e40d74a6a24be3d7bcffb6fee70935f5d5fe99ff4027ad18ef1bb7baedab43e32935
@@ -0,0 +1,40 @@
1
+ require "consulta_sunat/version"
2
+ require "consulta_sunat/http_request_sunat"
3
+ require "consulta_sunat/parse_content"
4
+
5
+ module ConsultaSunat
6
+ def self.by_dni document
7
+ document = dni_to_ruc document
8
+ return search(document)
9
+ end
10
+
11
+ def self.by_ruc document
12
+ return search(document)
13
+ end
14
+
15
+ private
16
+ def self.dni_to_ruc dni
17
+ dni = "10#{dni}"
18
+ digits = dni.split("").map(&:to_i)
19
+ array_mod_11 = [5,4,3,2,7,6,5,4,3,2]
20
+ sum_digits = digits.zip(array_mod_11).map{|i,j| i*j}.inject(0, :+)
21
+ mod_11 = sum_digits % 11
22
+ check_digit = 11 - mod_11
23
+
24
+ if check_digit == 11
25
+ check_digit = "0"
26
+ elsif check_digit == 10
27
+ check_digit = "1"
28
+ end
29
+
30
+ return "#{dni}#{check_digit.to_s}"
31
+ end
32
+
33
+ def self.search document
34
+ request = ConsultaSunat::HttpRequestSunat.new
35
+ page = request.request_page document
36
+ sunat_data = ConsultaSunat::ParseContent.parse_page page
37
+
38
+ sunat_data
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ module ConsultaSunat
4
+ class HttpRequestSunat
5
+
6
+ def initialize
7
+ @uri_random_number = URI.parse('http://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/captcha?accion=random')
8
+ @uri_consulta = URI.parse('http://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS00Alias')
9
+ @user_agent = 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0'
10
+ @action_request = 'consPorRuc'
11
+ @random_number, @session_cookie = random_session
12
+ end
13
+
14
+ def request_page document
15
+ data_form = {'nroRuc'=> document, 'accion' => @action_request, 'numRnd' => @random_number}
16
+ http_request = Net::HTTP::Post.new(@uri_consulta.path)
17
+ http_request.set_form_data(data_form)
18
+ http_request['Cookie'] = @session_cookie
19
+ http_request['User-Agent'] = @user_agent
20
+ page = Net::HTTP.start(@uri_consulta.hostname, @uri_consulta.port) do |http|
21
+ http.request(http_request).body
22
+ end
23
+ return page
24
+ end
25
+
26
+ private
27
+ def random_session
28
+ response = Net::HTTP.get_response(@uri_random_number)
29
+ if response.is_a? Net::HTTPOK
30
+ return response.body, response['set-cookie'].split(";")[0]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'yaml'
2
+ module ConsultaSunat
3
+ module ParseContent
4
+ def self.parse_page page
5
+ page = clean_page page
6
+ file_path = File.join(File.dirname(__FILE__), "sunat_match_data.yaml")
7
+ sunat_match_data = YAML.load_file(file_path)
8
+ sunat_data = {}
9
+ sunat_match_data.each do |key, expr|
10
+ sunat_data[key] = if result = page.match(expr)
11
+ result[1]
12
+ else
13
+ "-"
14
+ end
15
+ end
16
+
17
+ return sunat_data
18
+ end
19
+
20
+ def self.clean_page page
21
+ page.encode!('UTF-8', 'UTF-8', :invalid => :replace)
22
+ page.gsub(/\r\n/, "\n")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ ---
2
+ :razon_social: !ruby/regexp /<input type="hidden" name="desRuc" value="(.*)">/
3
+ :condicion: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Condici&oacute;n del Contribuyente:\s*<\/td>\n\s*<td\s*class="bg"\s*
4
+ colspan=[1|3]+>[\n\s]*(.*)[\n\s]*<\/td>/
5
+ :telefono: !ruby/regexp /<td class="bgn" colspan=1>Tel&eacute;fono\(s\):<\/td>\s*-->\n<!--\s*<td\s*class="bg"
6
+ colspan=1>(.*)<\/td>/
7
+ :nombre_comercial: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Nombre Comercial:\s*<\/td>\n\s*<td
8
+ class="bg" colspan=[1|3]>(.*)<\/td>/
9
+ :tipo: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Tipo Contribuyente:\s*<\/td>\n\s*<td
10
+ class="bg" colspan=[1|3]>(.*)<\/td>/
11
+ :fecha_inscripcion: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Fecha de Inscripci&oacute;n:\s*<\/td>\n\s*<td
12
+ class="bg" colspan=[1|3]>(.*)<\/td>/
13
+ :estado: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Estado del Contribuyente:\s*<\/td>\n\s*<td
14
+ class="bg" colspan=[1|3]>(.*)<\/td>/
15
+ :direccion: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Direcci&oacute;n del Domicilio
16
+ Fiscal:\s*<\/td>\n\s*<td class="bg" colspan=[1|3]>(.*)<\/td>/
17
+ :sistema_emision: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Sistema de Emisi&oacute;n
18
+ de Comprobante:\s*<\/td>\n\s*<td class="bg" colspan=[1|3]>(.*)<\/td>/
19
+ :actividad_exterior: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Actividad de Comercio
20
+ Exterior:\s*<\/td>\n\s*<td class="bg" colspan=[1|3]>(.*)<\/td>/
21
+ :sistema_contabilidad: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Sistema de Contabilidad:\s*<\/td>\n\s*<td
22
+ class="bg" colspan=[1|3]>(.*)<\/td>/
23
+ :oficio: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Profesi&oacute;n u Oficio:\s*<\/td>\n\s*<td
24
+ class="bg" colspan=[1|3]>(.*)<\/td>/
25
+ :actividad_economica: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Actividad(es) Econ&oacute;mica(s):\s*<\/td>\n\s*<td
26
+ class="bg" colspan=[1|3]>(.*)<\/td>/
27
+ :emision_electronica: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Emisor electr&oacute;nico
28
+ desde:\s*<\/td>\n\s*<td class="bg" colspan=[1|3]>(.*)<\/td>/
29
+ :ple: !ruby/regexp /<td class="bgn"\s*colspan=1\s*>Afiliado al PLE desde:\s*<\/td>\n\s*<td
30
+ class="bg" colspan=[1|3]>(.*)<\/td>/
@@ -0,0 +1,3 @@
1
+ module ConsultaSunat
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consulta_sunat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Jharol Salvador Rivera
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - jharol.salvador@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/consulta_sunat.rb
63
+ - lib/consulta_sunat/http_request_sunat.rb
64
+ - lib/consulta_sunat/parse_content.rb
65
+ - lib/consulta_sunat/sunat_match_data.yaml
66
+ - lib/consulta_sunat/version.rb
67
+ homepage: https://github.com/jsalvad0r/consulta_sunat
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.3.8
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.7.8
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: RubyGem para realizar consultas a Sunat Perú a traves del número de dni o
91
+ ruc
92
+ test_files: []