9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/mula/helpers.rb', line 9
def convert(amount=1, current_currency='USD', target_currency='GBP')
uri = URI.parse("#{API_BASE}?hl=en&q=#{amount}#{current_currency}=?#{target_currency}")
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(Net::HTTP::Get.new(uri.request_uri))
fixed_json_str = response.body.gsub!(/(['"])?([a-zA-Z0-9_]+)(['"])?:/, '"\2":')
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(fixed_json_str + ' ')[0..-2]
parsed_response = ActiveSupport::JSON.decode(valid_string)
return parsed_response["rhs"].split(" ")[0]
end
|