alexrabarts / iso_country_codes

A Ruby library that provides ISO 3166-1 country codes/names and ISO 4217 currencies
http://github.com/alexrabarts/iso-country-codes
163 stars 62 forks source link

The #search_by_name method should never raise a RegexpError #21

Closed joshuaflanagan closed 9 years ago

joshuaflanagan commented 9 years ago

Invalid input data should not raise an error, it should just return the normal failure/fallback result. The fact that the input is used to create a regular expression is an implementation detail, that shouldn't cause observable behavior. This change will make sure all user input is escaped so it is not interpreted as a regular expression.

If you do want to allow regular expression searches, it should require the user passing a regular expression, and the method should check for it explicitly:

if str.is_a?(Regexp)
  instances = all.select { |c| c.name.match(str) }
else
 ...