sii_chile 0.1.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/sii_chile/consulta.rb +48 -0
- data/lib/sii_chile/rut.rb +50 -0
- data/lib/sii_chile/version.rb +3 -0
- data/lib/sii_chile.rb +6 -0
- data/sii_chile.gemspec +22 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module SIIChile
|
|
2
|
+
class Consulta
|
|
3
|
+
attr_reader :rut
|
|
4
|
+
|
|
5
|
+
def initialize(rut)
|
|
6
|
+
@rut = Rut.new(rut)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def resultado
|
|
10
|
+
@resultado ||= self.fetch!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
protected
|
|
14
|
+
XPATH_RAZON_SOCIAL = '/html/body/center[2]/table[1]/tr[1]/td[2]/font'
|
|
15
|
+
XPATH_ACTIVIDADES = '/html/body/center[2]/table[4]/tr/td[2]/table/tr'
|
|
16
|
+
|
|
17
|
+
def fetch!
|
|
18
|
+
raise 'Rut invalido' unless @rut.valid?
|
|
19
|
+
|
|
20
|
+
response = Faraday.post('https://zeus.sii.cl/cvc_cgi/stc/getstc', {
|
|
21
|
+
'RUT' => @rut.number,
|
|
22
|
+
'DV' => @rut.code,
|
|
23
|
+
'PRG' => 'STC',
|
|
24
|
+
'OPC' => 'NOR'
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
data = Nokogiri::HTML(response.body)
|
|
28
|
+
|
|
29
|
+
{
|
|
30
|
+
:rut => @rut.format,
|
|
31
|
+
:razon_social => data.xpath(XPATH_RAZON_SOCIAL).text.strip,
|
|
32
|
+
:actividades => data.xpath(XPATH_ACTIVIDADES)[1..-1].map do |node|
|
|
33
|
+
{
|
|
34
|
+
:giro => node.xpath('./td[1]/font').text.strip,
|
|
35
|
+
:codigo => node.xpath('./td[2]/font').text.strip.to_i,
|
|
36
|
+
:categoria => node.xpath('./td[3]/font').text.strip,
|
|
37
|
+
:afecta => node.xpath('./td[4]/font').text.strip == 'Si'
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
}
|
|
41
|
+
rescue Error => e
|
|
42
|
+
{
|
|
43
|
+
:rut => @rut.format,
|
|
44
|
+
:error => e.mensage
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module SIIChile
|
|
2
|
+
# "Stealed" from https://github.com/iortega/rut_validator/blob/master/lib/rut_validator/rut.rb
|
|
3
|
+
class Rut
|
|
4
|
+
attr_reader :number
|
|
5
|
+
attr_reader :code
|
|
6
|
+
|
|
7
|
+
def initialize(rut)
|
|
8
|
+
@rut = rut.to_s.strip
|
|
9
|
+
@number = @rut.gsub(/[^0-9K]/i,'')[0...-1]
|
|
10
|
+
@code = @rut[-1]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def valid?
|
|
14
|
+
size_valid? && code_valid?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def size_valid?
|
|
18
|
+
@number.size <= 8
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def code_valid?
|
|
22
|
+
@code == calculated_code
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def calculated_code
|
|
26
|
+
all_codes = (0..9).to_a + ['K']
|
|
27
|
+
reverse_digits = @number.split('').reverse
|
|
28
|
+
factors = (2..7).to_a * 2
|
|
29
|
+
partial_sum = reverse_digits.zip(factors).inject(0) do |r, a|
|
|
30
|
+
digit, factor = *a
|
|
31
|
+
r += digit.to_i * factor
|
|
32
|
+
end
|
|
33
|
+
all_codes[11 - partial_sum%11].to_s
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def number_with_delimiter(delimiter='.')
|
|
37
|
+
@number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def format(opts={})
|
|
41
|
+
delimiter = opts[:delimiter] || '.'
|
|
42
|
+
with_dash = opts[:with_dash].nil? ? true : opts[:with_dash]
|
|
43
|
+
formatted_rut = number_with_delimiter(delimiter)
|
|
44
|
+
formatted_rut << '-' if with_dash
|
|
45
|
+
formatted_rut << @code
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def to_s; format; end
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/sii_chile.rb
ADDED
data/sii_chile.gemspec
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'sii_chile/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |gem|
|
|
7
|
+
gem.name = "sii_chile"
|
|
8
|
+
gem.version = SIIChile::VERSION
|
|
9
|
+
gem.authors = ["Seba Gamboa"]
|
|
10
|
+
gem.email = ["me@sagmor.com"]
|
|
11
|
+
gem.description = %q{Ruby API to some services from sii.cl}
|
|
12
|
+
gem.summary = %q{Ruby API to some services from sii.cl}
|
|
13
|
+
gem.homepage = ""
|
|
14
|
+
|
|
15
|
+
gem.files = `git ls-files`.split($/) rescue []
|
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
gem.require_paths = ["lib"]
|
|
19
|
+
gem.add_dependency "faraday"
|
|
20
|
+
gem.add_dependency "nokogiri"
|
|
21
|
+
gem.add_development_dependency "rake"
|
|
22
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sii_chile
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Seba Gamboa
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-01-09 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: faraday
|
|
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: nokogiri
|
|
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: rake
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
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
|
+
description: Ruby API to some services from sii.cl
|
|
63
|
+
email:
|
|
64
|
+
- me@sagmor.com
|
|
65
|
+
executables: []
|
|
66
|
+
extensions: []
|
|
67
|
+
extra_rdoc_files: []
|
|
68
|
+
files:
|
|
69
|
+
- .gitignore
|
|
70
|
+
- Gemfile
|
|
71
|
+
- Rakefile
|
|
72
|
+
- lib/sii_chile.rb
|
|
73
|
+
- lib/sii_chile/consulta.rb
|
|
74
|
+
- lib/sii_chile/rut.rb
|
|
75
|
+
- lib/sii_chile/version.rb
|
|
76
|
+
- sii_chile.gemspec
|
|
77
|
+
homepage: ''
|
|
78
|
+
licenses: []
|
|
79
|
+
post_install_message:
|
|
80
|
+
rdoc_options: []
|
|
81
|
+
require_paths:
|
|
82
|
+
- lib
|
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
|
+
none: false
|
|
85
|
+
requirements:
|
|
86
|
+
- - ! '>='
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
segments:
|
|
90
|
+
- 0
|
|
91
|
+
hash: -2214251905855754505
|
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
|
+
none: false
|
|
94
|
+
requirements:
|
|
95
|
+
- - ! '>='
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
segments:
|
|
99
|
+
- 0
|
|
100
|
+
hash: -2214251905855754505
|
|
101
|
+
requirements: []
|
|
102
|
+
rubyforge_project:
|
|
103
|
+
rubygems_version: 1.8.23
|
|
104
|
+
signing_key:
|
|
105
|
+
specification_version: 3
|
|
106
|
+
summary: Ruby API to some services from sii.cl
|
|
107
|
+
test_files: []
|