How to use Twitter Ruby Gem and twitter-text-rb to convert entities


I am using the Twitter Gem and twitter-text-rb in my web app. I can’t figure out how to get twitter-text-rb to convert the entities, including the urls (converting links).

Here is the closest I can get… but the output isn’t what is expected:
auto_link(tweet.text, tweet.attrs[:entities])

Any ideas?


I’ve found that you need to get url entities by including entities in the original call (ie Twitter.status(1234567890, {include_entities: "1"})). The Tweet in the response will have a url object, which is an array of Twitter::Entity::Url objects. Each of these objects can be cast to a hash with its attrs object. I assume there’s some mapping/rearranging involved before you can call auto_link(text, urlEntities)


some actual code (I put this in my rake task / data migration):

tweet = Twitter.status(source_id, {include_entities: "1"})

entities = Twitter::Extractor.extract_entities_with_indices(tweet.text)
# returns a bunch of entities
# some of these have a key :url, some have :screen_name, some :hashtag

# replace :url entities with matching entities from tweet.urls
entities.each do |oldEntity|
    next unless oldEntity.keys.include? :url
    tweet.urls.each do |newEntity|
        newEntity = newEntity.attrs
        next unless newEntity["url"] == oldEntity[:url]
        oldEntity[:expanded_url] = newEntity["expanded_url"]
        oldEntity[:display_url] = newEntity["display_url"]
linked_text = Twitter::Autolink.auto_link_entities(tweet.text, entities)

I haven’t ironed out all the bugs yet because I hit the 150 requests/hr limit :confused:


Ok, having configured my api and oath tokens my rate limit is now 350/hr. I had to add a simple begin…rescue…end around the Twitter.status(…) call to catch Twitter::Error::NotFound (for Tweets that were deleted after I stored the Tweet id locally)


For anyone else who stumbles on this thread, the author of the Twitter gem gives the answer here: