Best way to parse JSON in Rails for Twitter API - ruby-on-rails

I'm working on a rails app that searches the Twitter API stream for keywords and then records those tweets.
I've used Hpricot before to parse XML but I was wondering if someone could recommend a gem (or even a best practice) to parse JSON from the Twitter search API?

If you use the newly rewritten Twitter gem, you can make a query like this:
Twitter.user_timeline("sferik").first
The response would be a Hashie::Mash object corresponding to the first status. To access the information, you just call the attribute like it was a method:
Twitter.user_timeline("sferik").first.text
The response from this is just text. You don't have to directly deal with JSON.

I believe there are many twitter gems that should work. Github has the twitter gem as the most popular.
I have used it 1 1/2 yrs ago, but it should do what you want and have support to back it up.

I use the json gem to parse Twitter responses. To access the api I use grackle and sometimes curb to do api requests in parallel (Curl::Multi).

Related

Parsing usernames/links in a tweet in Rails and twitter gem

I've got a Rails app in which I'm building an interface for a user to send a tweet with text, a link, and/or an image. I'm trying to figure out how to handle parsing links into a t.co format and what to do with usernames, but I'm coming up short on any clear explanation of what's required.
I'm using the twitter and omniauth-twitter gems and I've also found the twitter-text gem. What I need to know is, am I required to parse links into a t.co format or do anything to usernames in my app before posting to the API? If so, how can I do that in Ruby/Rails? The documentation for the twitter-text gem, which seems to be the official way to handle this function, is nonexistent.
Thanks!

Display all tweets with a certain hashtag with simple design

I'm developing some website using Rails. I want to add "our users' tweets" part to the main page. I need an advice how I can do it better. I hoped to get standard way to do it, may be some Twitter widget or something else. I used Google, but I've found nothing. Please, point me to the right path. Sorry if my questions is very simple, but I don't really know how to do it. I hope that I needn't parse JSON and add styles independently; I need simple design from Twitter :)
To answer your [ambiguous] question, there are a number of things to consider:
How will you retrieve the tweets?
How will you store / access them?
How will the data be displayed on front-end?
The two methods you have are either to use the Twitter gem, or the TwitterFetcher JS plugin:
Gem
The Twitter gem uses the Twitter API to pull data from the official Twitter API. This means you've got the throttling & authentication to build into your app
The benefits of using this gem is it gives you a HUGE amount of flexibility with the data. You can pull as much data as you need / want, in whatever format you want - all formatted in JSON & can be displayed on your site
This gem is best suited to storing your tweets, either in a DB or in Redis etc, otherwise you'll have massive synchronous dependency on Twitter's API - which is never good for performance
JS
The TwitterFetcher JS plugin is epic - basically takes a Twitter widget & strips out the HTML, allowing you to style it how you like
This is the most effective way to retrieve Twitter data on-the-fly, as it's asynchronous, relies on Twitter's widget system (far more robust than API), and stores no data locally

How to read text from a url and parse it in Ruby on Rails?

In php I am using curl but being new to Ruby and Rails, I am little lost about the best way to do this,
So I have http://example.com/api?=topmovies&ratings which returns list of 10 movies with their ratings. Unfortunately it is not in json/xml but just text so I was wondering,
How to read/retrieve the text from url in rails
How to parse/put the data in variables (it has 2 columns and 10 rows)
Thanks
You could use curl with ruby too, but the preferred way to interact with websites is with the mechanize gem. Take a look at the mechanize examples page to see how to get the page from a website and parse through the content.
You can use RestClient gem to get content of another website.

What is Twitter API?

I would like to apologize first if the question is a total newbie question, but I really am a total newbie on this.
I'm a student and I recently have joined a project that involves studying (mining) tweets. The project head asked me to use the Twitter API to extract tweets. What exactly is Twitter API and how can I use it? What do I need to know to start using it?
Twitter allows you to interact with its data ie tweets & several attributes about tweets using Twitter APIs. You'd need to know a server side scripting language like php, python or ruby to make requests to twitter api and results would be in JSON format that can be easily read by your program.
A good starting point would be reading the official documentation at https://dev.twitter.com/ itself.
Throw you can use and show some functionality in your website

Integrating Facebook,twitter,google plus into another app

Integrating facebook,twitter and google plus into another web app, so that the posts in that app are posted write away as status in facebook,tweets in twitter and status in google plus.I am developing this using ruby on rails.I searched a lot about this but didn't find anything which would work for me.Can post some links or ideas which would be helpful for this.
For Facebook I must suggest you to use Koala Gem...
For Twitter
Twitter
https://twitter.com/about/resources/buttons
A twitter button can be easily generated here and placed on your web site..Its working great. You can even customize everything in twitter.. Its so user friendly.
For Google plus
Google Plus
I am just working on Google plus..Facing some difficulties as its having so many restrictions.
Still I would Like to Share a link you can follow that..
How to share content from our site to google plus
I hope it will work fine...
I'd suggest to use Koala ( https://github.com/arsduo/koala/wiki ) to interface with Facebook. It's by far the best maintained fb library out there.
Otherwise, you should learn one thing or two about OAuth2:
https://github.com/intridea/oauth2
https://developers.facebook.com/docs/authentication/
https://developers.google.com/accounts/docs/OAuth2
Use https://github.com/sferik/twitter to interact with twitter.
For google, I've found that implementing the XML chat manually is easier than using any existing API wrapper. To convert XML responses into data, you an use a combination of the Response#parsed method of oauth2 gem and Array.wrap of ActiveSupport (there is no way for an XML parser to tell a single node to a possibly repeated node (so an array) that appears just once, avery big drawback of XML in my point of view)
Or you can directly ask google for JSON, so you already have the data correctly structured.
And, as #brendan-benson very correctly says, do only API calls in background workers, never in your normal request/responses cycle.
There are plenty of gems available to access these APIs:
Twitter API Wrapper
Facebook API Wrapper
Google Plus API Wrapper
Since APIs are flaky, it's best to use a queueing system like resque to queue the calls in Post#after_create, and then have a resque worker execute the call asynchronously.

Resources