I am creating a Twitter clone in Ruby On Rails, how do I code it so that the '@...''s in the 'tweets' turn into links?
def linkup_mentions_and_hashtags(text)
text.gsub!(/@([\w]+)(\W)?/, '<a href="http://twitter.com/\1">@\1</a>\2')
text.gsub!(/#([\w]+)(\W)?/, '<a href="http://twitter.com/search?q=%23\1">#\1</a>\2')
text
end
I found this example here: http://github.com/jnunemaker/twitter-app
The link to the helper method: http://github.com/jnunemaker/twitter-app/blob/master/app/helpers/statuses_helper.rb
Turn URLs and @* into links
def link_urls_and_users s
#regexps
url = /( |^)http:\/\/([^\s]*\.[^\s]*)( |$)/
user = /@(\w+)/
#replace @usernames with links to that user
while s =~ user
s.sub! "@#{$1}", "<a href='http://twitter.com/#{$1}' >#{$1}</a>"
end
#replace urls with links
while s =~ url
name = $2
s.sub! /( |^)http:\/\/#{name}( |$)/, " <a href='http://#{name}' >#{name}</a> "
end
s
end
puts link_urls_and_users(tweet.text)
This works, so long as URLs are padded by spaces or are at the beginning and/or end of the tweet.
How to share a post in twitter using ruby on rails?
You can use the pretty-social jquery plugin.
Add this in a javascript file:
$('.prettySocial').prettySocial();
To include the plugin, you can paste the plugin file in your assets or vendor folder.
And this on your posts:
<div class="prettySocial" data-type="twitter" data-url="<%= request.original_url %>" data-description = "<%= @post.content %>" data-media = "<%= @post.main_photo %>"><%= image_tag("twitter.png")%>
You'll get a twitter icon, which will trigger a share onClick.
Ruby on Rails - Converting Twitter @mentions, #hashtags and URLs within a string
The twitter-text gem has pretty much all the work covered for you. Install it manually (gem install twitter-text
, use sudo if needed) or add it to your Gemfile (gem 'twitter-text'
) if you are using bundler and do bundle install
.
Then include the Twitter auto-link library (require 'twitter-text'
and include Twitter::Autolink
) at the top of your class and call the method auto_link(inputString)
with the input string as the parameter and it will give you the auto linked version
Full code:
require 'twitter-text'
include Twitter::Autolink
myString = "I like using @twitter, because I learn so many new things! [line break]
Read my blog: http://www.myblog.com #procrastination"
linkedString = auto_link(myString)
If you output the contents of linkedString, you get the following output:
I like using @<a class="tweet-url username" href="https://twitter.com/twitter" rel="nofollow">twitter</a>, because I learn so many new things! [line break]
Read my blog: <a href="http://www.myblog.com" rel="nofollow">http://www.myblog.com</a> <a class="tweet-url hashtag" href="https://twitter.com/#!/search?q=%23procrastination" rel="nofollow" title="#procrastination">#procrastination</a>
Twitter share button not using custom url or text
The code provided by you is perfectly fine and should work as expected.
Many site issues can be caused by corrupt cookies or cache. Try to clear both cookies and the cache. I would suggest you to look into the following link to see why it is not working in firefox
Twitter share button not using custom url or text
The code provided by you is perfectly fine and should work as expected.
Many site issues can be caused by corrupt cookies or cache. Try to clear both cookies and the cache. I would suggest you to look into the following link to see why it is not working in firefox
How Do I search Twitter for a word with Ruby?
The Twitter API suggests the URI your should be using for global search is https://api.twitter.com/1.1/search/tweets.json
and this means:
- Your
base_url
component would behttps://api.twitter.com
- Your
path
component would be/1.1/search/tweets.json
- Your
query
component would be the text you are searching for.
The query
part takes a lot of values depending upon the API spec. Refer to the specification and you can change it as per your requirement.
Tip: Try to use irb
(I'd recommend pry
) REPL which makes it a lot easier to explore APIs. Also, checkout the Faraday gem which can be easier to use than the default HTTP library in Ruby IMO.
Related Topics
Which Ruby Rest API Client for Neo4J
Form_For with Multiple Controller Actions for Submit
Checking If Activerecord Find Returns a Result
Update Rails to a Specific Version
Gem And/Or Ruby on Rails Broke All of a Sudden, Anyone Know What Happened
Deadlock Detected with Capybara-Webkit
How to Create Pretty JSON in Chef (Ruby)
Trying to Get Svn2Git Working on Windows
Turn a Ruby Hash into HTML List
Properly Using Log4R in Ruby Application
Groups in a Gemfile in Rails 3
Factory_Girl + Rspec Doesn't Seem to Roll Back Changes After Each Example
How to Do Complex Querying with Logical Operations by Using Searchkick
Postgresql Ilike with Multiple Matches in Rails Activerecord
Ruby on Rails Collection Select - How to Pre-Select the Right Value
Ruby Group Hashes by Value of Key
Package Configuration for Libffi Is Not Found in MACos While Installing Travis-Cli