forked from vurbia/bravo
-
Notifications
You must be signed in to change notification settings - Fork 12
/
auth_data.rb
65 lines (54 loc) · 1.99 KB
/
auth_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Bravo
# This class handles authorization data
#
class AuthData
class << self
attr_accessor :environment, :todays_data_file_name
# Fetches WSAA Authorization Data to build the datafile for the day.
# It requires the private key file and the certificate to exist and
# to be configured as Bravo.pkey and Bravo.cert
#
def fetch
unless File.exists?(Bravo.pkey)
raise "Archivo de llave privada no encontrado en #{ Bravo.pkey }"
end
unless File.exists?(Bravo.cert)
raise "Archivo certificado no encontrado en #{ Bravo.cert }"
end
unless File.exists?(todays_data_file_name)
Bravo::Wsaa.login
end
YAML.load_file(todays_data_file_name).each do |k, v|
Bravo.const_set(k.to_s.upcase, v) unless Bravo.const_defined?(k.to_s.upcase)
end
end
# Returns the authorization hash, containing the Token, Signature and Cuit
# @return [Hash]
#
def auth_hash
fetch unless Bravo.constants.include?(:TOKEN) && Bravo.constants.include?(:SIGN)
{ 'Token' => Bravo::TOKEN, 'Sign' => Bravo::SIGN, 'Cuit' => Bravo.cuit }
end
# Returns the right wsaa url for the specific environment
# @return [String]
#
def wsaa_url
raise 'Environment not sent to either :test or :production' unless Bravo::URLS.keys.include? environment
Bravo::URLS[environment][:wsaa]
end
# Returns the right wsfe url for the specific environment
# @return [String]
#
def wsfe_url
raise 'Environment not sent to either :test or :production' unless Bravo::URLS.keys.include? environment
Bravo::URLS[environment][:wsfe]
end
# Creates the data file name for a cuit number and the current day
# @return [String]
#
def todays_data_file_name
@todays_data_file ||= "/tmp/bravo_#{ Bravo.cuit }_#{ Time.new.strftime('%Y_%m_%d') }.yml"
end
end
end
end