Module: AZNConverter::Convert

Included in:
Float, Integer
Defined in:
lib/azn_converter/convert.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_currency(int, currency) ⇒ Object



51
52
53
54
# File 'lib/azn_converter/convert.rb', line 51

def self.get_currency(int, currency)
  page = Net::HTTP.get(URI("https://coinmill.com/AZN_#{currency.to_s.upcase}.html?#{currency.to_s.upcase}=#{int}"))
  return Nokogiri::HTML(page).xpath('//*[@id="currencyBox1"]/input')[0].to_a.last.last.to_f
end

.has_connection?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/azn_converter/convert.rb', line 45

def self.has_connection?
  return true if open('https://coinmill.com/')
rescue
  false
end

Instance Method Details

#to_azn(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
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
# File 'lib/azn_converter/convert.rb', line 9

def to_azn(options = {})
  options[:current] ||= true
  int = self

  if options[:current] == true
    if options[:from] == :azn
      value = int
    else
      if AZNConverter::Convert::has_connection?
        value = AZNConverter::Convert::get_currency(int, options[:from] || :usd)
      else
        return 'No Connection'
      end
    end
  elsif options[:current] > 0
    value = int * options[:current]
  else
    return nil
  end

  if options[:words]
    if value.is_a? Integer
      value = value.humanize(locale: :az) + ' manat'
    elsif value.is_a? Float
      value = "%.2f" % value
      value = begin
        w = value.to_i.humanize(locale: :az) + ' manat '
        w += (value.to_s[(value.to_s.index('.') + 1)..(value.to_s.index('.') + 2)]).to_i.humanize(locale: :az) + ' qəpik'
      end
    end
  end

  value = value.to_s + " \u20BC" if options[:sign] && !(value.is_a? String)
  return value
end