Class: ExchangeRate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency_options) ⇒ ExchangeRate

Returns a new instance of ExchangeRate.



8
9
10
11
12
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 8

def initialize(currency_options)
  @date  = currency_options.first
  @nok   = currency_options.last["NOK"]
  @sek   = currency_options.last["SEK"]
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 6

def date
  @date
end

#nokObject (readonly)

Returns the value of attribute nok.



6
7
8
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 6

def nok
  @nok
end

#sekObject (readonly)

Returns the value of attribute sek.



6
7
8
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 6

def sek
  @sek
end

Class Method Details

.allObject



14
15
16
17
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 14

def self.all
  file_path = File.dirname(__FILE__) + "/data/currency.json"
  file      = process_file(file_path)
end

.at(date, base, counter) ⇒ Object



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
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 19

def self.at(date, base, counter)
  counter_data = []
  @date = date_initialize(date)
  @base         = base
  @counter      = counter.downcase
  file_path = File.dirname(__FILE__) + "/data/currency.json"
  # response_from_file =  process_file('./data/currency.json')
  response_from_file =  process_file(file_path)

  response_from_file.each do |data|
    if data.date == @date
      counter_data << data
    end
  end

  unless counter_data.empty?
    date_r = counter_data.first.date
    nok  = counter_data.first.nok
    sek  = counter_data.first.sek
    case @counter
      when "sek"
         "1 euro = #{sek}"
      when "nok"
         "1 euro = #{nok}"
      else
         "Out of bound"
    end

  else
     "No data found"
  end

end

.date_initialize(date) ⇒ Object



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

def self.date_initialize(date)
  @today_date = Date.today.strftime("%Y-%m-%d")
  @new_date = date.to_s
  if(@new_date == @today_date || @new_date > "2018-05-15")
    @new_date = "2018-05-15"
  else
    @new_date = date.to_s
  end
end

.process_file(file) ⇒ Object



63
64
65
66
67
68
# File 'lib/exchange_rate_freska/exchange_rate.rb', line 63

def self.process_file(file)
  response_data = []
  file          = File.read(file)
  hash_data     = JSON.parse(file)
  hash_data.map{|data| ExchangeRate.new(data)}
end