Module: ExchangeParty

Defined in:
lib/exchange_party.rb,
lib/exchange_party/version.rb,
lib/exchange_party/currency.rb

Defined Under Namespace

Classes: Currency, Error

Constant Summary collapse

URL =
"https://www.tcmb.gov.tr/kurlar"
VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.currencies(date = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/exchange_party.rb', line 14

def self.currencies(date = nil)
  url = generate_url(date)
  doc = ""

  begin
    doc = Nokogiri.XML(::OpenURI.open_uri(url), nil, "UTF-8").remove_namespaces!
  rescue StandardError
    raise StandardError if doc.empty?
  else
    generate_currencies(doc)
  end
end

.generate_currencies(doc) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/exchange_party.rb', line 39

def self.generate_currencies(doc)
  currency_elements = doc.elements.first.elements
  attributes = doc.elements.first.attributes
  currency_attributes = attributes.transform_values(&:value)
  currencies = []

  currency_elements.each do |currency_element|
    currencies << ExchangeParty::Currency.new(currency_element, currency_attributes)
  end

  currencies
end

.generate_url(date) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/exchange_party.rb', line 27

def self.generate_url(date)
  if date.nil?
    "#{URL}/today.xml"
  else
    year = date.split("-").first
    month = date.split("-")[1]
    day = date.split("-").last

    "#{URL}/#{year}#{month}/#{day}#{month}#{year}.xml"
  end
end