Skip to content

Commit a18e0ea

Browse files
committed
Move afip module to a class
1 parent af3d125 commit a18e0ea

File tree

3 files changed

+94
-88
lines changed

3 files changed

+94
-88
lines changed

lib/afip.rb

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -15,104 +15,110 @@
1515

1616
# AfipSDK is the easyest way to connect with AFIP
1717
module Afip
18-
@config = Afip::AfipConfiguration.new
19-
20-
@ElectronicBilling = Afip::WebServices::ElectronicBilling.new(self)
21-
@RegisterInscriptionProof = Afip::WebServices::RegisterInscriptionProof.new(self)
22-
@RegisterScopeTen = Afip::WebServices::RegisterScopeTen.new(self)
23-
@RegisterScopeThirteen = Afip::WebServices::RegisterScopeThirteen.new(self)
24-
25-
class << self
26-
extend Forwardable
27-
28-
attr_reader :ElectronicBilling,
29-
:RegisterInscriptionProof,
30-
:RegisterScopeTen,
31-
:RegisterScopeThirteen,
32-
:config
33-
34-
# User configurable options
35-
def_delegators :@config, :CUIT, :CUIT=
36-
def_delegators :@config, :cert, :cert=
37-
def_delegators :@config, :key, :key=
38-
def_delegators :@config, :production, :production=
39-
def_delegators :@config, :access_token, :access_token=
18+
def self.new(options)
19+
Afip::Instance.new(options)
4020
end
4121

42-
# Gets token authorization for an AFIP Web Service
43-
#
44-
# If force is true it forces to create a new TA
45-
def self.getServiceTA(service, force = false)
46-
url = URI("https://app.afipsdk.com/api/v1/afip/auth")
47-
48-
https = Net::HTTP.new(url.host, url.port)
49-
https.use_ssl = true
50-
51-
request = Net::HTTP::Post.new(url)
52-
request["Content-Type"] = "application/json"
53-
request["sdk-version-number"] = Afip::VERSION
54-
request["sdk-library"] = "ruby"
55-
request["sdk-environment"] = @config.production == true ? "prod" : "dev"
56-
request["Authorization"] = "Bearer #{@config.access_token}" if @config.access_token
57-
58-
data = {
59-
"environment": @config.production == true ? "prod" : "dev",
60-
"tax_id": @config.CUIT,
61-
"wsid": service,
62-
"force_create": force
63-
}
64-
65-
data["cert"] = @config.cert if @config.cert
66-
data["key"] = @config.key if @config.key
67-
68-
request.body = JSON.dump(data)
69-
response = https.request(request)
70-
71-
unless response.is_a? Net::HTTPSuccess
72-
begin
73-
raise JSON.parse(response.read_body)
74-
rescue
75-
raise response.read_body
76-
end
22+
class Instance
23+
attr_accessor :CUIT,
24+
:cert,
25+
:key,
26+
:production,
27+
:access_token,
28+
:ElectronicBilling,
29+
:RegisterInscriptionProof,
30+
:RegisterScopeTen,
31+
:RegisterScopeThirteen
32+
33+
def initialize(options)
34+
raise "CUIT field is required in options" unless options.key?(:CUIT)
35+
36+
self.CUIT = options[:CUIT]
37+
self.production = options.key?(:production) ? options[:production] : false
38+
self.cert = options[:cert]
39+
self.key = options[:key]
40+
41+
self.ElectronicBilling = Afip::WebServices::ElectronicBilling.new(self)
42+
self.RegisterInscriptionProof = Afip::WebServices::RegisterInscriptionProof.new(self)
43+
self.RegisterScopeTen = Afip::WebServices::RegisterScopeTen.new(self)
44+
self.RegisterScopeThirteen = Afip::WebServices::RegisterScopeThirteen.new(self)
7745
end
7846

79-
JSON.parse(response.read_body)
80-
end
47+
# Gets token authorization for an AFIP Web Service
48+
#
49+
# If force is true it forces to create a new TA
50+
def getServiceTA(service, force = false)
51+
url = URI("https://app.afipsdk.com/api/v1/afip/auth")
52+
53+
https = Net::HTTP.new(url.host, url.port)
54+
https.use_ssl = true
55+
56+
request = Net::HTTP::Post.new(url)
57+
request["Content-Type"] = "application/json"
58+
request["sdk-version-number"] = Afip::VERSION
59+
request["sdk-library"] = "ruby"
60+
request["sdk-environment"] = production == true ? "prod" : "dev"
61+
request["Authorization"] = "Bearer #{access_token}" if access_token
62+
63+
data = {
64+
"environment": production == true ? "prod" : "dev",
65+
"tax_id": self.CUIT,
66+
"wsid": service,
67+
"force_create": force
68+
}
69+
70+
data["cert"] = cert if cert
71+
data["key"] = key if key
72+
73+
request.body = JSON.dump(data)
74+
response = https.request(request)
75+
76+
unless response.is_a? Net::HTTPSuccess
77+
begin
78+
raise JSON.parse(response.read_body)
79+
rescue
80+
raise response.read_body
81+
end
82+
end
83+
84+
JSON.parse(response.read_body)
85+
end
8186

82-
# Get last request and last response XML
83-
def self.getLastRequestXML
84-
url = URI("https://app.afipsdk.com/api/v1/afip/requests/last-xml")
87+
# Get last request and last response XML
88+
def getLastRequestXML
89+
url = URI("https://app.afipsdk.com/api/v1/afip/requests/last-xml")
8590

86-
https = Net::HTTP.new(url.host, url.port)
87-
https.use_ssl = true
91+
https = Net::HTTP.new(url.host, url.port)
92+
https.use_ssl = true
8893

89-
request = Net::HTTP::Get.new(url)
90-
request["sdk-version-number"] = Afip::VERSION
91-
request["sdk-library"] = "ruby"
92-
request["sdk-environment"] = @config.production == true ? "prod" : "dev"
93-
request["Authorization"] = "Bearer #{@config.access_token}" if @config.access_token
94+
request = Net::HTTP::Get.new(url)
95+
request["sdk-version-number"] = Afip::VERSION
96+
request["sdk-library"] = "ruby"
97+
request["sdk-environment"] = production == true ? "prod" : "dev"
98+
request["Authorization"] = "Bearer #{access_token}" if access_token
9499

95-
data["cert"] = @config.cert if @config.cert
96-
data["key"] = @config.key if @config.key
100+
data["cert"] = cert if cert
101+
data["key"] = key if key
97102

98-
response = https.request(request)
103+
response = https.request(request)
99104

100-
unless response.is_a? Net::HTTPSuccess
101-
begin
102-
raise JSON.parse(response.read_body)
103-
rescue
104-
raise response.read_body
105+
unless response.is_a? Net::HTTPSuccess
106+
begin
107+
raise JSON.parse(response.read_body)
108+
rescue
109+
raise response.read_body
110+
end
105111
end
106-
end
107112

108-
JSON.parse(response.read_body)
109-
end
113+
JSON.parse(response.read_body)
114+
end
110115

111-
# Create generic Web Service
112-
def self.webService(service, options = {})
113-
options[:service] = service
114-
options[:generic] = true
116+
# Create generic Web Service
117+
def webService(service, options = {})
118+
options[:service] = service
119+
options[:generic] = true
115120

116-
Afip::WebService.new(self, options)
121+
Afip::WebService.new(self, options)
122+
end
117123
end
118124
end

lib/afip/electronic_billing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def createPDF(data)
2222

2323
request = Net::HTTP::Post.new(url)
2424
request["Content-Type"] = "application/json"
25-
request["sdk-version-number"] = afip::VERSION
25+
request["sdk-version-number"] = Afip::VERSION
2626
request["sdk-library"] = "ruby"
2727
request["sdk-environment"] = afip.production == true ? "prod" : "dev"
2828
request["Authorization"] = "Bearer #{afip.access_token}" if afip.access_token
@@ -217,7 +217,7 @@ def getWSInitialRequest(operation)
217217
if operation == "FEDummy"
218218
{}
219219
else
220-
ta = afip.getServiceTA("wsfe")
220+
ta = getTokenAuthorization
221221

222222
{
223223
"Auth": {

lib/afip/web_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def executeRequest(method, params = {})
3939

4040
request = Net::HTTP::Post.new(url)
4141
request["Content-Type"] = "application/json"
42-
request["sdk-version-number"] = afip::VERSION
42+
request["sdk-version-number"] = Afip::VERSION
4343
request["sdk-library"] = "ruby"
4444
request["sdk-environment"] = afip.production == true ? "prod" : "dev"
4545
request["Authorization"] = "Bearer #{afip.access_token}" if afip.access_token

0 commit comments

Comments
 (0)