Class: Pincerna::CurrencyConversion

Inherits:
Base
  • Object
show all
Defined in:
lib/pincerna/currency_conversion.rb

Overview

Converts a value from a currency to another.

Constant Summary

MATCHER =

The expression to match.

/^
  (?<value>([+-]?)(\d+)([.,]\d+)?)
  \s
  (?<from>\S{1,3})
  \s+
  (to\s+)?
  (?<to>\S{1,3})
  (?<rate>\swith\srate)?
$/mix
RELEVANT_MATCHES =

Relevant groups in the match.

{
  "value" => ->(context, value){ context.round_float(value.gsub(",", ".").to_f) },
  "from" => ->(_, value) { value.upcase },
  "to" => ->(_, value) { value.upcase },
  "rate" => ->(_, value) { !value.nil? } # If show conversion rate
}
ICON =

The icon to show for each feedback item.

Pincerna::Base::ROOT + "/images/currency.png"
URL =

The URL of the webservice.

"http://rate-exchange.appspot.com/currency"
SYMBOLS =

A list of symbols and their associated ISO codes

{
  "Lek" => "ALL",
  "؋" => "AFN",
  "$" => "USD",
  "ƒ" => "ANG",
  "ман" => "AZN",
  "p." => "BYR",
  "BZ$" => "BZD",
  "$b" => "BOB",
  "KM" => "BAM",
  "P" => "BWP",
  "лв" => "UZS",
  "R$" => "BRL",
  "" => "KHR",
  "¥" => "JPY",
  "" => "CRC",
  "kn" => "HRK",
  "" => "PHP",
  "" => "CZK",
  "kr" => "SEK",
  "RD$" => "DOP",
  "£" => "GBP",
  "" => "EUR",
  "¢" => "GHC",
  "Q" => "GTQ",
  "L" => "HNL",
  "Ft" => "HUF",
  "" => "TRY",
  "Rp" => "IDR",
  "" => "YER",
  "" => "ILS",
  "J$" => "JMD",
  "" => "KRW",
  "" => "LAK",
  "Ls" => "LVL",
  "Lt" => "LTL",
  "ден" => "MKD",
  "RM" => "MYR",
  "" => "LKR",
  "" => "MNT",
  "MT" => "MZN",
  "C$" => "NIO",
  "" => "NGN",
  "B/." => "PAB",
  "Gs" => "PYG",
  "S/." => "PEN",
  "" => "PLN",
  "lei" => "RON",
  "руб" => "RUB",
  "Дин." => "RSD",
  "S" => "SOS",
  "R" => "ZAR",
  "CHF" => "CHF",
  "NT$" => "TWD",
  "฿" => "THB",
  "TT$" => "TTD",
  "" => "TRL",
  "" => "UAH",
  "$U" => "UYU",
  "Bs" => "VEF",
  "" => "VND",
  "Z$" => "ZWD"
}

Constants inherited from Base

Base::CACHE_ROOT, Base::FULL_NAME, Base::ROOT, Base::TYPES, Base::WORKFLOW_ROOT

Instance Attribute Summary

Attributes inherited from Base

#format, #format_content_type, #output

Instance Method Summary (collapse)

Methods inherited from Base

#add_feedback_item, execute!, #filter, #format_float, #initialize, #output_feedback, #round_float

Constructor Details

This class inherits a constructor from Pincerna::Base

Instance Method Details

- (Hash|NilClass) perform_filtering(value, from, to, with_rate)

Converts a value from a currency to another.

Parameters:

  • value (Float)

    The value to convert.

  • from (String)

    The origin currency.

  • to (String)

    The target currency.

  • with_rate (Boolean)

    If to return the conversion rate in the results.

Returns:

  • (Hash|NilClass)

    The converted data or nil if the conversion failed.



107
108
109
110
111
112
# 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

- (Array) process_results(results)

Processes items to obtain feedback items.

Parameters:

  • results (Hash)

    The item to process.

Returns:

  • (Array)

    The feedback items.



118
119
120
121
122
123
# 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