B
B
bezgerts2014-04-09 14:40:22
ruby
bezgerts, 2014-04-09 14:40:22

How to properly encode url with russian characters in Rails 4?

Hello!
I write on RoR 4 recently. I have also been doing programming recently, in connection with this there is not enough knowledge to solve this problem, namely:
when you click on this link:
http://maps.yandex.ru/?text=sberbank&sll=50.186334...
this appears error:
bad URI(is not URI?): http://maps.yandex.ru/?text=sberbank&sll=50.186334...
If I change "sberbank" to "sberbank" everything works fine.

def yandex_map(number)
      require 'open-uri'
      require 'rubygems'
      require 'nokogiri' #-парсинг HTML
      require 'json' #-парсинг json
      require 'uri' #-открытие по URL



      @html = open("http://maps.yandex.ru/?text=сбербанк&sll=50.18633499999997%2C53.216120999997734&sspn=0.377655%2C0.123853&z=12&results=20&ll=50.186333%2C53.216119&spn=0.377655%2C0.123853&l=map")
      @nokogiri = Nokogiri::HTML(@html)
      @may = @nokogiri.css('#vpage').inner_text
      @j_parse = JSON.parse(@may)

      json_companys = Array.new(number)
      json_telefons = Array.new(number)


      number.times do |i|
        json_companys[i] = @j_parse['vpage']['data']['businesses']['GeoObjectCollection']['features'][i]['properties']['name']
      end
      return json_companys 
    end

How to fix this error and use links with Russian characters?
Tried URI.encode, didn't work.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Arkady Butermanov, 2014-04-09
@Arkadey

open(URI.escape(" http://.... "))

F
FanKiLL, 2014-04-10
@FanKiLL

So what do you need? change russian characters to eng?

F
FanKiLL, 2014-04-10
@FanKiLL

require 'uri'
=> true
irb(main):002:0> URI.escape("http://maps.yandex.ru/?text=сбербанк&sll=50.18633499999997%2C53.216120999997734&sspn=0.377655%2C0.123853&z=12&results=20&ll=50.186333%2C53.216119&spn=0.377655%2C0.123853&l=map")
=> "http://maps.yandex.ru/?text=%D1%81%D0%B1%D0%B5%D1%80%D0%B1%D0%B0%D0%BD%D0%BA&sll=50.18633499999997%252C53.216120999997734&sspn=0.377655%252C0.123853&z=12&results=20&ll=50.186333%252C53.216119&spn=0.377655%252C0.123853&l=map"

It's strange that it URI.escapeworks fine for me.

F
floydback, 2015-09-08
@floydback

gem 'unicode'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question