consulta_sunat 0.1.2 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60b9f11fdf4f099835c34e1191976413c42a523c350c05fe958d948efa9398e2
4
- data.tar.gz: 801e0c02ddd81470f93c8ec2a12c68d9d95e098c4919ef81121c9539dc976028
3
+ metadata.gz: c8fe479498bedece9ff4f7322075db8737346aabf39b5268bb5ef30a3efbbc8e
4
+ data.tar.gz: 13ccb591aa10f4c5b1039fa27f633d7bf3a3682dc1b3b7ab1a104e25ec1e13a1
5
5
  SHA512:
6
- metadata.gz: bb1aa26c6b3c3df800ecd5155b5855b593a3edf325a723427d41a365d62228a29a230d1ba5ad929dcf91b245aa4f2bb42cd82e1acd678798fd40965a19cf1dfd
7
- data.tar.gz: fe9aeaf14cb48dbff82a9e4e59cc1a0f0ed7a0513294004f142945e19823e40d74a6a24be3d7bcffb6fee70935f5d5fe99ff4027ad18ef1bb7baedab43e32935
6
+ metadata.gz: b9bd9c10fc38be3552df3a549f23278e0ae71a25861026818dcbb3fe85dd4ff5d8841a0ec1290052ced2593786ea1415315f7b013d23c226bfb71f3bcd56137a
7
+ data.tar.gz: 687f87ca235fa99a134b70f7258c05925e91ca965c1cb299a97ffc0a436b81a04fec3b37efd5ddf96b16097807c2acaa891b5ebf666acecc6c9fd7468f852900
@@ -3,38 +3,55 @@ require "consulta_sunat/http_request_sunat"
3
3
  require "consulta_sunat/parse_content"
4
4
 
5
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
6
+ def self.by_dni document
7
+ if document =~ /^[0-9]{8}$/
8
+ document = dni_to_ruc document
9
+ return search(document)
10
+ else
11
+ return {error: "invalid document"}
12
+ end
13
+ end
14
+
15
+ def self.by_ruc document
16
+ if document =~ /^[0-9]{11}$/
17
+ return search(document)
18
+ else
19
+ return {error: "invalid document"}
20
+ end
21
+ end
22
+
23
+ def self.find_ruc document
24
+ if document =~ /^[0-9]{8}$/
25
+ return dni_to_ruc(document)
26
+ else
27
+ return {error: "invalid document"}
28
+ end
29
+ end
30
+
31
+ def self.dni_to_ruc dni
32
+ dni = "10#{dni}"
33
+ digits = dni.split("").map(&:to_i)
34
+ array_mod_11 = [5,4,3,2,7,6,5,4,3,2]
35
+ sum_digits = digits.zip(array_mod_11).map{|i,j| i*j}.inject(0, :+)
36
+ mod_11 = sum_digits % 11
37
+ check_digit = 11 - mod_11
38
+
39
+ if check_digit == 11
40
+ check_digit = "0"
41
+ elsif check_digit == 10
42
+ check_digit = "1"
43
+ end
44
+
45
+ return "#{dni}#{check_digit.to_s}"
46
+ end
47
+
48
+ def self.search document
49
+ request = ConsultaSunat::HttpRequestSunat.new
50
+ page = request.request_page document
51
+ sunat_data = ConsultaSunat::ParseContent.parse_page page
52
+
53
+ sunat_data
54
+ end
55
+
56
+ private_class_method :search, :dni_to_ruc
40
57
  end
@@ -1,34 +1,34 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
3
  module ConsultaSunat
4
- class HttpRequestSunat
4
+ class HttpRequestSunat
5
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
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
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
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
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
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
34
  end
@@ -1,25 +1,25 @@
1
1
  require 'yaml'
2
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
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
16
 
17
- return sunat_data
18
- end
17
+ return sunat_data
18
+ end
19
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
20
+ def self.clean_page page
21
+ page.encode!('UTF-8', 'iso-8859-1')
22
+ page.gsub(/\r\n/, "\n")
23
+ end
24
+ end
25
25
  end
@@ -1,3 +1,3 @@
1
1
  module ConsultaSunat
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consulta_sunat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
- - Jharol Salvador Rivera
7
+ - Jharol Salvador
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-08 00:00:00.000000000 Z
11
+ date: 2019-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: 2.3.8
79
+ version: 2.2.2
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="