Class: Ifqar::FundQuoter

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

Overview

Fund Quoter is the client that retrieves quotes from cafci.org.ar

Constant Summary collapse

FUND_TYPE =
{
  :EQUITY => { :id => 1, :name => 'Renta Variable' },
  :FIXED_INCOME => { :id => 2, :name => 'Renta Fija' },
  :MONEY_MARKET => { :id => 3, :name => 'Mercado de Dinero' },
  :BALANCED => { :id => 4, :name => 'Renta Mixta' }
}.freeze
HEADERS =
{
  :multipart => true,
  :referer => 'http://www.cafci.org.ar/scripts/cfn_EstadisticasVCP.html'
}.freeze
MAX_RETRIES =
3
SET_PARAMS_URL =
'http://www.cafci.org.ar/Scripts/cfn_EstadisticaVCPXMLSet.asp'.freeze
QUERY_URL =
'http://www.cafci.org.ar/Scripts/cfn_PlanillaDiariaXMLList.asp'.freeze

Instance Method Summary collapse

Constructor Details

#initializeFundQuoter

Returns a new instance of FundQuoter.



25
26
27
# File 'lib/ifqar/fund_quoter.rb', line 25

def initialize
  @daily_stats = {}
end

Instance Method Details

#cached_stats(date, type) ⇒ Object



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

def cached_stats(date, type)
  return nil if @daily_stats[date].nil? || @daily_stats[date][type].nil?
  @daily_stats[date][type]
end

#payload_builder(date, type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ifqar/fund_quoter.rb', line 59

def payload_builder(date, type)
  Nokogiri::XML::Builder.new do |xml|
    xml.Coleccion do
      xml.Parametros do
        xml.TRentaI FUND_TYPE[type][:id]
        xml.TRentaN URI.encode(FUND_TYPE[type][:name])
        xml.Fecha date.strftime('%Y-%m-%d')
        xml.Separador 'P'
      end
    end
  end.doc.root.serialize(save_with: 0)
end

#query_service(date, type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ifqar/fund_quoter.rb', line 40

def query_service(date, type)
  payload = payload_builder(date, type)

  retries = 0
  begin
    response = RestClient.post(SET_PARAMS_URL, { :query => payload }, HEADERS)
    response = RestClient.post(QUERY_URL, nil, HEADERS.merge(:cookies => response.cookies))
  rescue RestClient::ExceptionWithResponse => error
    if retries < MAX_RETRIES
      retries += 1
      retry
    else
      raise error
    end
  end

  StatsDeserializer.deserialze(date, FUND_TYPE[type][:name], response.body)
end

#stats(date, type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/ifqar/fund_quoter.rb', line 29

def stats(date, type)
  date_str = date.strftime('%Y-%m-%d')
  stats = cached_stats(date_str, type)
  if stats.nil?
    stats = query_service(date, type)
    @daily_stats[date_str] = {} if @daily_stats[date_str].nil?
    @daily_stats[date_str][type] = stats
  end
  stats
end