Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:23:25+00:00 2026-06-14T08:23:25+00:00

Newbie question again… I already have the following code for making links shortened using

  • 0

Newbie question again… I already have the following code for making links shortened using Bitly.

def bitly_links(url)                                     
 bitly ||= begin                                            
  Bitly.use_api_version_3                                     
  Bitly.new('username', 'key')
  bitly.shorten(url)
 end
end

What I’d like to do this time is to search for and shorten all links in a comment after it’s been created. This is similar to this blog post Using bitly in rails 3, but found it a bit confusing because of the use of for loop instead of the do…end block. Also, I notice that the method was not even called.

Sample comment in the website:

Hey, you should check out the post http://vox4life.blogspot.com/2012/11/4Life-2012-Growth-is-strong.html and this one.. http://vox4life.blogspot.com/2012/11/dengue-outbreak-peoples-journal.html Also this
http://vox4life.blogspot.com/2012/02/transfer-factor-immunotherapy.html

Would turn into:

Hey, you should check out the post http://bit.ly/ZHdLTa and this one.. http://bit.ly/TZ6Nrj Also this http://bit.ly/ZrnWMj

Thank you in advance.

Similar question to this, but in Java

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:23:26+00:00Added an answer on June 14, 2026 at 8:23 am

    If you do not want a for loop I guess you could modify the method as such. I’m figuring that body is the entire text with URLs.

    def bitly_body(body)
        matches = body.scan(/((http|https):\/\/(\&|\=|\_|\?|\w|\.|\d|\/|-)+(:\d+(\&|\=|\?|\w|\.|\d|\/|-)+)?)/)
        Bitly.use_api_version_3
    
        bitly = Bitly.new("thealey", "bitly_api_key")
    
        (0..matches.length).each do |i|    # <-- changed here.
          if matches[i].to_s.size > 0
            logger.info("url " + matches[i][0])
            if matches[i][0].include? "bit.ly"
              logger.info("already a bitly url " + matches[i][0])
            else
              u = bitly.shorten(matches[i][0])
              body[matches[i][0]] = u.short_url
            end
          end
        end
        body
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have two c# classes - a code class (say, CodeClass) and
newbie question in css: I have the following style defined: TABLE.tabulardata th { font:
Another newbie question: In the following code, what if the thread is preempted after
Newbie question, but I have this code: import java.util.*; import java.io.*; public class Welcome1
Newbie question... The objective: I intend to have an HTML text input field as
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question since I'm not up to speed using maven at all. I'm trying
another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
And again a newbie question. ;-) I am setting my View Model based on
This sounds like a newbie question, but I have a couple of Applications where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.