class Pincerna::CurrencyConversion

Converts a value from a currency to another.

Constants

ICON

The icon to show for each feedback item.

MATCHER

The expression to match.

RELEVANT_MATCHES

Relevant groups in the match.

SYMBOLS

A list of symbols and their associated ISO codes

URL

The URL of the webservice.

Public Instance Methods

perform_filtering(value, from, to, with_rate) click to toggle source

Converts a value from a currency to another.

@param value [Float] The value to convert. @param from [String] The origin currency. @param to [String] The target currency. @param with_rate [Boolean] If to return the conversion rate in the results. @return [Hash|NilClass] The converted data or `nil` if the conversion failed.

# File lib/pincerna/currency_conversion.rb, line 107
def perform_filtering(value, from, to, with_rate)
  from = replace_symbol(from)
  to = replace_symbol(to)
  response = fetch_remote_resource(URL, {q: value, from: from, to: to})
  {value: value, from: from, to: to, result: round_float(response["v"]), rate: round_float(response["rate"]), with_rate: with_rate}
end
process_results(results) click to toggle source

Processes items to obtain feedback items.

@param results [Hash] The item to process. @return [Array] The feedback items.

# File lib/pincerna/currency_conversion.rb, line 118
def process_results(results)
  title = "%s %s = %s %s" % [format_float(results[:value]), results[:from], format_float(results[:result]), results[:to]]
  title << " (1 %s = %s %s)" % [results[:from], format_float(results[:rate]), results[:to]] if results[:with_rate]

  [{title: title, arg: format_float(results[:value]), subtitle: "Action this item to copy the converted amount on the clipboard.", icon: self.class::ICON}]
end

Private Instance Methods

replace_symbol(symbol) click to toggle source

Replaces a currency symbol with its corresponding ISO code.

@param symbol [String] The symbol to replace. @return [String] The corresponding code. If none is found, the original symbol is returned.

# File lib/pincerna/currency_conversion.rb, line 130
def replace_symbol(symbol)
  SYMBOLS.fetch(symbol, symbol)
end