How to get and display API like the facebook graph in rails - ruby-on-rails

I've went through most of the beginner rails books and I want to try creating something on my own. For a start, I just want to create a few pages in rails that will get from Facebook's api and display something like username, likes, post. I've searched around and couldn't find an answer. My friend recommended that I use a gem called fb_graph, but reviewing the documentation, I have no clue how to use it.
Thanks stackoverflow!

You can query the Graph API directly. The responses will be in JSON which you can then parse into Ruby hash. See the Facebook Documentation for more details on to call specific and sample JSON responses. So here is general guide how to you can start playing around with this:-
Make a API Call to Facebook using Graph API
Explorer. Keep playing around with api until you get a response you want. Note the request params you passed to get that response & JSON you received from facebook.
Send a HTTP request containing those same params in rails using koala, 'fb_graph' or just plain NET:HTTP. It doesn't matter what client you use to sent the request, as long as you send same params as in step1, you will get that familar JSON as response.
Now, once you have the json, just have to parse it. Provided, if the client library is not already doing that as most fb gems will turn JSON into ruby objects/hashes. But if they don't, then you have to do it manually, its something like JSON.parse('JSON_RESPONSE_AS_STRING_GOES_HERE'). after this you will have a plain-old ruby hash which is you can save to db, display in view or whatever you want to do.
Hope it helps

Related

Getting direct messages using Twitter REST api

I'm experimenting with twitter REST API with Ruby on Rails.
I'm using twitter gem for the same. I could get the client object using in my code.
client = current_domain.twitter_accounts.first.client
following the documentation given here
The client object works fine. But I couldn't get DirectMessages in the same way.
Also followed this documentation. Here I could not find a way to get DirectMessages. Is there a way in REST API to get twitter direct messages. Or do I need to implement Streaming API.
http://www.rubydoc.info/gems/twitter/Twitter/REST/Client
Methods included from DirectMessages
#create_direct_message, #destroy_direct_message, #direct_message, #direct_messages, #direct_messages_received, #direct_messages_sent
These map to the REST API endpoints
https://dev.twitter.com/rest/reference/get/direct_messages
https://dev.twitter.com/rest/reference/get/direct_messages/sent
n.b. you won't get group messages through this API and will need to rebuild an inbox model e.g. sequence replies of replies between yourself and the recipient.

Using an API key in Rails App

I just got an API key for a database I wish to access and want to start building my Rails app. However I dont know where to begin with the API key. Specifically I want to use the brewerydb data and I am building an app where users can find the closest brewery to their location. Can anyone tell me how to get started? I am new to Rails and have never used an API before. I don't know where to begin. What file should I put it in, etc... I know I should probably update the GEMFILE, where else?
Thanks!
Check the documentation for the API. That's all I can say. (well... not really: )
Most API's rely on REST or SOAP, which is basically making HTTP request to certain URI's. An example may be
http://api.somewebsite.com/beers.json
Which would return, for instance, a JSON array of certain beers and their properties.
Furthermore, more often than not, you can test API's (that do not require certain HTTP headers for authentication, which makes it harder) by manually constructing the URI's and opening them in your browser. This way, you can verify that your request is okay before you try it in your Rails application an cannot figure out why it's not working.
You should use the 'figaro' gem https://github.com/laserlemon/figaro
It creates a "application.yml" file in which you can add your API key.

Twitter single url request

Is it possible to make a request to Twitter API in a single like? (http request?)
In example you can get the JSON in the Facebook API by the following single url:
https://graph.facebook.com/me?access_token=ACCESS_TOKEN
Can something be possible similarly in twitter. E.g.:
https://api.twitter.com/1.1/statuses/user_timeline.json?consumer_key=CONSUMER_KEY&consumer_secret=CONSUMER_SECRET&access_token=ACCESS_TOEKN&token_secret=TOKEN_SECRET
I find the twitter documentation quite stiff. If somebody has another proposal to get me understand how the communication with the API works I would be glad.

POST via json in Ruby on Rails

I'm trying to create record in db via json. The problem is that i don't know how to compose http request in URL bar:
It should be something like:
http://localhost:3000/addnewpost.json?content=sometexthere
Or this is not correct?
It's not correct. Creating a record should be a POST command, not a GET. As such, the data content should go in the POST headers, not in the URL.
Are you sending this POST directly from Ruby? (if so, you'll want to look at Net::HTTP or some other ruby HTTP client). Show us some code and we'll help you improve it.

How do I make an HTTP request in Ruby on Rails to get data from Facebook?

How do I make an HTTP request in ruby on rails to get data from facebook. I am unsure of where to start to get data from the facebook API in json format.
I hope this question even makes sense.
If you are going to be dealing with FB in Rails you will want to take a look at mogli and facebooker2. If you can provide some more detail, people will be able to help you better but at least that's a good place to start.
If you really need JSON data, use rest-client.
https://github.com/archiloque/rest-client
I recommend use this instead of original rest-client gem, since original one doesn't force SSL certificate verification.
https://github.com/nov/restclient_with_cert

Resources