Skip to content

Commit

Permalink
Fix conversion filter. Why does google sometimes return integers, som…
Browse files Browse the repository at this point in the history
…etime strings?
  • Loading branch information
marcandre committed Dec 18, 2009
1 parent 9c48816 commit 611272b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/googleajax/filters.rb
Expand Up @@ -41,13 +41,20 @@ def initialize(h)
# The ConvertValues filter converts the string values
# to ruby-style values (e.g. Integers, Floats, true, false or Strings)
module ConvertValues
TRUE_OR_FALSE = /true|(false)/i
TRUE_OR_FALSE = /^true|(false)$/i
INTEGER = /^\d+$/
FLOAT = /^\d+\.\d+$/
def initialize(h)
h.each do |key, value|
# Won't use Integer.try_convert for 1.8.6 & 7 compatibility
h[key] = Integer(value) \
rescue Float(value) \
rescue (value =~ TRUE_OR_FALSE ? Regexp.last_match[1].nil? : value)
case value
when INTEGER
h[key] = value.to_i
when FLOAT
h[key] = value.to_f
when TRUE_OR_FALSE
h[key] = Regexp.last_match[1].nil?
end
end
super(h)
end
Expand Down

0 comments on commit 611272b

Please sign in to comment.