Class: DolarRealbr::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/dolar_realbr.rb

Instance Method Summary collapse

Constructor Details

#initializeConvert

Returns a new instance of Convert.



7
8
9
10
11
12
13
14
# File 'lib/dolar_realbr.rb', line 7

def initialize()
	@url_service = 'https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl'
   @url_service2 = 'https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl'
   @CC = {'sale' => {'dollar' => 1, 'euro' => 21619, 'yen' => 21621, 'pound_sterling' => 21623, 'swiss_franc' => 21625, 'danish_krone' => 21627, 'norwegian_krone' => 21629, 'swedish_krona' => 21631, 'australian_dollar' => 21633, 'canadian_dollar' => 21635} , 'buy' => {'dollar' => 10813, 'euro' => 21620, 'yen' => 21622, 'pound_sterling' => 21624, 'swiss_franc' => 21626, 'danish_krone' => 21628, 'norwegian_krone' => 21630, 'swedish_krona' => 21632, 'australian_dollar' => 21634, 'canadian_dollar' => 21636} } 
	@CC.default(nil)
	@value = @day = @name = @code = @unit = nil
	@cli = Savon.client(wsdl: @url_service)
end

Instance Method Details

#check_date(day = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dolar_realbr.rb', line 16

def check_date(day = nil)
  unless day.nil?
    dt = DateTime.new(day[6,4].to_i, day[3,2].to_i, day[0,2].to_i)
    dt =  dt.prev_day while dt.sunday? or dt.saturday?
    @day = "#{dt.day.to_s.rjust(2,'0')}/#{dt.month.to_s.rjust(2,'0')}/#{dt.year}"
  end 
end

#convert_currency2realBR(params) ⇒ Object

params 1.00, currency: ‘dollar’, type: ‘buy’, date: ‘DD/MM/YYYY’



66
67
68
69
# File 'lib/dolar_realbr.rb', line 66

def convert_currency2realBR(params)
	self.value_currency2realBR(params)
	@value *= params[:value].to_f unless @value.nil? or params[:value].nil?
end

#convert_realBR2currency(params) ⇒ Object

params 1.00, currency: ‘dollar’, type: ‘buy’, date: ‘DD/MM/YYYY’



72
73
74
75
# File 'lib/dolar_realbr.rb', line 72

def convert_realBR2currency(params)
	self.value_currency2realBR(params)
	@value = params[:value].to_f/@value unless @value.nil? or params[:value].nil? or @value == 0
end

#get_dayObject



85
86
87
# File 'lib/dolar_realbr.rb', line 85

def get_day
	@day
end

#get_last_value_errorObject



47
48
49
50
51
# File 'lib/dolar_realbr.rb', line 47

def get_last_value_error
  @cli.call(:get_ultimo_valor_xml, message: {'in0' => @code})
rescue  IOError => e
  #erro na conexao
end

#get_nameObject



77
78
79
# File 'lib/dolar_realbr.rb', line 77

def get_name
	@name
end

#get_unit_currencyObject



89
90
91
# File 'lib/dolar_realbr.rb', line 89

def get_unit_currency
	@unit
end

#get_valueObject



81
82
83
# File 'lib/dolar_realbr.rb', line 81

def get_value
	@value
end

#get_value_error(params) ⇒ Object



53
54
55
56
57
# File 'lib/dolar_realbr.rb', line 53

def get_value_error(params)
  @cli.call(:get_valor, message: {'in0' => @code, 'in1' => params})
rescue  IOError => e
  #erro na conexao
end

#value_currency2realBR(params) ⇒ Object

params ‘dollar’, type: ‘buy’, date: ‘DD/MM/YYYY’



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dolar_realbr.rb', line 25

def value_currency2realBR(params)
	@code = @CC.fetch("#{params[:type]}").fetch("#{params[:currency]}") if @CC.key?("#{params[:type]}") && @CC["#{params[:type]}"].key?("#{params[:currency]}")
	unless @cli.nil? or @code.nil?
		@value = @day = @name = @unit = nil
		if params[:date].nil? 
       op = get_last_value_error
			op  = op.body.to_h[:get_ultimo_valor_xml_response][:get_ultimo_valor_xml_return]  unless op.body.nil?
			if op.class == Nori::StringWithAttributes ##Savon::Response
  		xml_file = Nokogiri::HTML(op.force_encoding("UTF-8"))
  		@unit = xml_file.css('unidade').text
  		@name = xml_file.css('nome').text
 			@day = "#{xml_file.css('ano').text}/#{xml_file.css('mes').text.rjust(2,'0')}/#{xml_file.css('dia').text.rjust(2,'0')}"
				@value = xml_file.css('valor').text.gsub(',','.').to_f
			end
		else
       self.check_date(params[:date])
			op = get_value_error(@day)
			@value = op.body.to_h[:multi_ref].to_f if op.class == Savon::Response
		end
	end
end

#value_realBR2currency(params) ⇒ Object

params ‘dollar’, type: ‘buy’, date: ‘DD/MM/YYYY’



60
61
62
63
# File 'lib/dolar_realbr.rb', line 60

def value_realBR2currency(params)
		self.value_currency2realBR(params)
		@value = @value**(-1) unless @value.nil? or @value.zero?
end