开发者

Shortened link with Bitly API, how do I remove the 'http://'?

开发者 https://www.devze.com 2023-03-29 19:35 出处:网络
I\'m currently using the ruby-bitly gem to shorten links in a Rails app; however, the result has an \"http://\" in front of the bit.ly link. This is not friendly for tweets/other length sensitive post

I'm currently using the ruby-bitly gem to shorten links in a Rails app; however, the result has an "http://" in front of the bit.ly link. This is not friendly for tweets/other length sensitive posts. How do I automatically remove that?

Link controller action:

 def shorten_with_bitly(url)
    link = Link.find(params[:id]) 
    bitly = Bitly.shorten(url, "MY_ID", "MY_API_KEY")
    bitly.url = link.shortened_link开发者_运维技巧
    link.save 
 end

Thank you very much for your help!!!


Use gsub, for instance:

"http://google.com".gsub("http://", "")

or use slice:

"http://google.com".slice!(0..6) 

Keep in mind that the ! will modify the original string, not just give you a modified copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号