class Dolarblue::XChange

Base class for Blue, Official and Bolsa used to hold sell/buy values functionality

@abstract

Attributes

buy[R]
sell[R]

Public Class Methods

new(config = Configuration.instance) click to toggle source

Create a new Blue / Official / Bolsa instance to work later on

@param config [Configuration] the configuration instance

@return [self] new instance

# File lib/dolarblue/xchange.rb, line 17
def initialize(config = Configuration.instance)
  @config = config.defaults
  self
end

Public Instance Methods

buy_output() click to toggle source

Return a formatted string suitable for user output with current buy value

@return [String] formatted output buy exchange value

# File lib/dolarblue/xchange.rb, line 47
def buy_output
  '%.2f' % buy
end
cname() click to toggle source

Return the demodulized class name

@return [String] demodulized class name string

# File lib/dolarblue/xchange.rb, line 25
def cname
  Inflector.demodulize(self.class.name)
end
extract_values(doc) click to toggle source

Performs buy and sell values extraction from a Nokogiri::HTML Document

@param [Nokogiri::HTML] doc the html document to extract values from

# File lib/dolarblue/xchange.rb, line 39
def extract_values(doc)
  @buy  = extract_val(doc, 'buy')
  @sell = extract_val(doc, 'sell')
end
name() click to toggle source

Return downcased demodulized class name

@return [String] downcased demodulized class name string

# File lib/dolarblue/xchange.rb, line 32
def name
  cname.downcase
end
output() click to toggle source

Return a formatted string suitable for user output with current buy and sell values

@return [String] formatted output with buy and sell exchange values

# File lib/dolarblue/xchange.rb, line 61
def output
  t = cname.ljust(10, '.')
  b = buy_output.rjust(5)
  s = sell_output.rjust(5)
  %Q{- Dollar #{t}..: #{b} / #{s}}
end
sell_output() click to toggle source

Return a formatted string suitable for user output with current sell value

@return [String] formatted output sell exchange value

# File lib/dolarblue/xchange.rb, line 54
def sell_output
  '%.2f' % sell
end

Protected Instance Methods

extract_val(doc, type) click to toggle source

Extract individual buy/sell values from a Nokogiri::HTML Document

@param doc [Nokogiri::HTML] the html document to extract values from @param type [String] the dollar type, can be ‘blue’, ‘official’, ‘bolsa’

@raise [RuntimeError] if unable to proper buy/sell value for current dollar type

@private

# File lib/dolarblue/xchange.rb, line 78
def extract_val(doc, type)
  xpath = @config[name][type].xpath
  value = doc.xpath(xpath).text.gsub(',', '.')
  fail RuntimeError, "Failed to capture #{name} #{type} value" if value.empty?
  value.to_f.round(2)
end