Integration with twitter-text-rb

326 views
Skip to first unread message

Matthew Fordham

unread,
Nov 20, 2012, 6:55:21 PM11/20/12
to ruby-twi...@googlegroups.com
I am attempting to get the Tweet entities (provided by "include_entities => true") processed by twitter-text-rb. I can't seem to get the entities passed into it in a way that works. Is there any documentation around this that anyone is aware of?

Thanks!

sferik

unread,
Nov 20, 2012, 7:01:33 PM11/20/12
to ruby-twi...@googlegroups.com
On Tuesday, November 20, 2012 3:55:21 PM UTC-8, Matthew Fordham wrote:
I am attempting to get the Tweet entities (provided by "include_entities => true") processed by twitter-text-rb. I can't seem to get the entities passed into it in a way that works. Is there any documentation around this that anyone is aware of?

If you're using the latest major version of the gem (4.x) entities should be included by default. You can request them explicitly like so:

results = Twitter.search("#test", :include_entities => true).results
results.first.hashtags
results.first.urls
results.first.user_mentions

Matthew Fordham

unread,
Nov 21, 2012, 12:17:52 AM11/21/12
to ruby-twi...@googlegroups.com
Yeah, I saw that... but what I can't figure out is how to get the entities in a format that twitter-text-rb will like. I feel like I must be missing something obvious... wouldn't almost anyone using the Twitter API need an easy way to process the entities? 

Thanks! :)

sferik

unread,
Nov 21, 2012, 12:21:41 AM11/21/12
to ruby-twi...@googlegroups.com
On Tuesday, November 20, 2012 9:17:52 PM UTC-8, Matthew Fordham wrote:
Yeah, I saw that... but what I can't figure out is how to get the entities in a format that twitter-text-rb will like. I feel like I must be missing something obvious... wouldn't almost anyone using the Twitter API need an easy way to process the entities?

What exactly are you trying to do? Most people use twitter-text-rb to parse the entities out of full tweet text. If you use the twitter gem and include entities, this should not be necessary. I think you're confused about something.

Matthew Fordham

unread,
Nov 21, 2012, 12:29:55 AM11/21/12
to ruby-twi...@googlegroups.com
I am trying to present the Tweets with clickable links and the original URLs displayed (not the t.co shortened links). Seems like twitter-text-rb's autolink method is what I am needing, but I can figure out how to feed in the entities in the right format. I would expect to do something like this:

autolink(tweet.text, tweet.entities)

Thanks again for the help!

sferik

unread,
Nov 21, 2012, 12:32:23 AM11/21/12
to ruby-twi...@googlegroups.com
On Tuesday, November 20, 2012 9:29:55 PM UTC-8, Matthew Fordham wrote:
I am trying to present the Tweets with clickable links and the original URLs displayed (not the t.co shortened links). Seems like twitter-text-rb's autolink method is what I am needing, but I can figure out how to feed in the entities in the right format. I would expect to do something like this:

autolink(tweet.text, tweet.entities)

Just pass in the tweet text. No need to pass in the entities. Have you look at the README? https://github.com/twitter/twitter-text-rb#auto-linking-examples

Matthew Fordham

unread,
Nov 21, 2012, 12:41:37 AM11/21/12
to ruby-twi...@googlegroups.com
Yeah, I have looked at those docs. Autolinking without passing in the entities works for making things clickable, but it doesn't use the original link, it just makes the t.co link clickable. Without passing in the entities, how could the autolink method know the original link? 

sferik

unread,
Nov 21, 2012, 12:59:31 AM11/21/12
to ruby-twi...@googlegroups.com
On Tuesday, November 20, 2012 9:41:37 PM UTC-8, Matthew Fordham wrote:
Yeah, I have looked at those docs. Autolinking without passing in the entities works for making things clickable, but it doesn't use the original link, it just makes the t.co link clickable. Without passing in the entities, how could the autolink method know the original link?

Oh, I see. You'll want to use the auto_link_entities method for that:

require 'twitter'
require 'twitter-text'

include Twitter::Autolink
tweet = Twitter.status(271129717111943168, :include_entities => true)
auto_link_entities(tweet.text, tweet.urls)

Matthew Fordham

unread,
Nov 21, 2012, 1:16:46 AM11/21/12
to ruby-twi...@googlegroups.com
That's great... it gets me a lot closer. 

The only trouble with that approach, though, is that I can't get the rest of the entities linked, such as tags and user names. Once auto_link_entities runs on the text, the regular auto_link can't be used afterwards, as the indices are no longer accurate. The only solution I can think of is to pass in *all* the entities to the auto_link method, not just the URLs, but I can't seem to make that work.

So sorry top drag this out. Your wisdom is so appreciated!!! 

sferik

unread,
Nov 21, 2012, 1:38:43 AM11/21/12
to ruby-twi...@googlegroups.com
On Tuesday, November 20, 2012 10:16:46 PM UTC-8, Matthew Fordham wrote:
That's great... it gets me a lot closer. 

The only trouble with that approach, though, is that I can't get the rest of the entities linked, such as tags and user names. Once auto_link_entities runs on the text, the regular auto_link can't be used afterwards, as the indices are no longer accurate. The only solution I can think of is to pass in *all* the entities to the auto_link method, not just the URLs, but I can't seem to make that work.

Try this:

auto_link_with_json(tweet.text, tweet.to_hash[:entities])

If that doesn't work, I'm afraid I'm not going to help you anymore. This forum exists to provide support for the twitter gem. At this point, you're really about the twitter-text gem, which is completely different. I'd encourage you to read the source code, in particular autolink.rbhttps://github.com/twitter/twitter-text-rb/blob/master/lib/twitter-text/autolink.rb as well as the specs for that class: https://github.com/twitter/twitter-text-rb/blob/master/spec/autolinking_spec.rb

If you have more questions, I'd encourage you to post them on a site like Stack Overflow or a forum that is specific to twitter-text. It's possible that Twitter will provide support via their developer forums: https://dev.twitter.com/discussions

Matthew Fordham

unread,
Nov 21, 2012, 11:53:01 AM11/21/12
to ruby-twi...@googlegroups.com
That worked perfectly, thanks! 

I realize this wasn't entirely an issue with the Twitter gem... I really appreciate you going out of your way to help me find a solution. Many, many thanks :)

Matthew Fordham

unread,
Nov 21, 2012, 11:53:05 AM11/21/12
to ruby-twi...@googlegroups.com
That worked perfectly, thanks! 

I realize this wasn't entirely an issue with the Twitter gem... I really appreciate you going out of your way to help me find a solution. Many, many thanks :)




On Tuesday, November 20, 2012 10:38:43 PM UTC-8, sferik wrote:
Reply all
Reply to author
Forward
0 new messages