Class: SbifChile

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sbif_chile.rb,
lib/sbif_chile/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = '') ⇒ SbifChile

Returns a new instance of SbifChile.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sbif_chile.rb', line 12

def initialize(token = '')
  @api_key = ENV['SBIF_API_KEY'] || token
  @date_current = {
    date: '2017-02-26 14:33:13 -0300',
    dolar: 0.0,
    euro: 0.0,
    ipc: 0.0,
    tad: 0.0,
    tmc: [
      {
        date: '2017-02-26 14:33:13 -0300',
        title: 'no title',
        subtitle: 'no subtitle',
        value: 0.0,
        type: 0,
      }
    ],
    uf: 0.0,
    utm: 0.0,
  }
end

Instance Attribute Details

#date_currentObject (readonly)

Returns the value of attribute date_current.



8
9
10
# File 'lib/sbif_chile.rb', line 8

def date_current
  @date_current
end

Instance Method Details

#current_date(resource = 'uf') ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/sbif_chile.rb', line 52

def current_date(resource = 'uf')
  response = self.class.get("/#{resource.downcase}?apikey=#{@api_key}&formato=json")
  if response.code.equal?(200)
    process_data(resource, response)
  elsif response.code >= 400
    raise "Bad Request for #{resource.downcase} : http_code: #{response.code} - http_message: #{response.message} - http_description: #{response['Mensaje']}"
  end
rescue Exception => ex
  puts "ERROR - #{ex.message}"
end

#date_range(resource = 'uf', first_date = Time.now, second_date = Time.now) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sbif_chile.rb', line 63

def date_range(resource = 'uf' ,first_date = Time.now, second_date = Time.now)
  if first_date <= second_date
    response = self.class.get("/#{resource.downcase}/periodo/#{first_date.year}/#{first_date.month}/#{second_date.year}/#{second_date.month}?apikey=#{@api_key}&formato=json")
    if response.code.equal?(200)
      process_data(resource, response)
    elsif response.code >= 400
      raise "Bad Request for #{resource.downcase} : http_code: #{response.code} - http_message: #{response.message} - http_description: #{response['Mensaje']}"
    end
  else
    raise 'First date can\'t be greater than second'
  end
rescue Exception => ex
  puts "ERROR - #{ex.message}"
end

#indicators_currentObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sbif_chile.rb', line 34

def indicators_current
  dolar = current_date('dolar')
  euro = current_date('euro')
  ipc = current_date('ipc')
  tab = current_date('tab')
  tmc = current_date('tmc')
  uf = current_date
  utm = current_date('utm')
  @date_current[:date] = Time.now
  @date_current[:dolar] = dolar[0][:value] unless dolar.nil?
  @date_current[:euro] = euro[0][:value] unless euro.nil?
  @date_current[:ipc] = ipc[0][:value] unless ipc.nil?
  @date_current[:tad] = tab[0][:value] unless tab.nil?
  @date_current[:tmc] = tmc unless tmc.nil?
  @date_current[:uf] = uf[0][:value] unless uf.nil?
  @date_current[:utm] = utm[0][:value] unless utm.nil?
end