How do I send in a REST request (to use an API)? - ruby-on-rails

I'm writing a small web app for myself in Rails and I want it to use Last.fm's API. I'm incredibly new to web development in general, so I'm not sure where to even begin.
http://www.last.fm/api/rest
I'm not asking for a step-by-step tutorial or anything like that, but a nudge in the right direction would be awesome. Or even just a little example code.

Installing HTTParty will get you started, you might also want to make some calls to the API from the command line using cURL just to understand how it works. Just type cURL and the restful URL on the commandline:
prompt> curl http://www.whatever.com/resource for a get
prompt> curl -d '{ "arg" : "value" }' http://www.whatever.com/ for posts etc.
Often understanding what comes back in each of the calls will help you structure your app. Have fun.

Dupe of this 748614
Simply speaking - use the HTTParty gem, it does literally everything for you.

Related

How to Parse Json with Bearer Token in Ruby

I have recently graduated a Bootcamp and trying my luck in some projects and applying for jobs.
Got a test, for this potential job, that I don't know how to tackle exactly. At this stage, kind of just wondering how to solve the test.
I need to create a website and display a list of all of the buildings, from this real estate company via their API. They have provided me with the token for the access already.
However, I'm not entirely sure how to connect with their API via Ruby. They gave me an example of how to connect but I haven't seen this before.
curl -H "Authorization: Bearer token_they_provided" https://theirsite/api/buildings.
Any assistance to point me in the right direction will be appreciated.
Thank you all!
What you are trying to achieve is pretty much simple. You can just add HTTP gem (there are other options, like Faraday gem) to your project, which turns everything even simpler.
Using HTTP Gem
Add it to your Gemfile
Install using bundle
Fetches API as below:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
Parse JSON body:
JSON.parse(response.body)
You can read the gem docs for more information. Also, feel free to connect me via LinkedIn

http POST method for web server

I'm looking at HTTP POST method to create a web page.
I'm a tad unfamiliar with it, and was looking for some explanation on how this works.
I'm simply wanting to use their seemingly simplistic page for my own server needs.
My question is this, how does this curl link really work?
curl --data-binary #audio2.flac --header 'Content-type: audio/x-flac; rate=16000' 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=0&maxresults=1&lang="en-US"'
So I got it to work by using curl, by sending a flac file as you can see, and it responds with
this JSon stuff
{"status":0,"id":"3b7853241a0dded048f84744cc1ab896-1","hypotheses":[{"utterance":"ice cream","confidence":0.88215643}]}
Please correct me if I'm wrong:
So I believe this is a post method as it has the ? mark with it to send the name value pair.
I have no idea how they got the link https://www.google.com/speech-api/v1/recognize
to only accept POST requests without having a script name like this at the end
https://www.google.com/speech-api/v1/recognize/scriptName.pl
How do I write something to accept a post request at that given page without a .pl or .py extension? How can I put it in a certain directory on the server.
In addition for my purposes can I also send a file back to them once I process / zip?
Any ideas on how to do that?
Thanks so much for your time and generous help!
It seems that if you use --data/--data-binary/--form that the request is POSTed.
I first totally misunderstood your question and though you wanted to know how to use the interface, but you got that working.
The serverside can be achieved by multiple means, totally depending on the infrastructure and software used.
With the Webserver apache for example, you could use Rewrite or something in a HtAccess file to obfuscate/change the requested file. So looking at the extension (or the absence of it) is not helpful.
Please also see various options mentioned here
Running PHP without extension without using mod_rewrite?
or
ForceType/htaccess file extension question - extensionless files?
to see how you could achieve the same.
Only accepting POST data is then again dependant on the programming/scripting technique you employ at your server side, PHP e.g. distinguishes between multiple superglobals like $_GET and $_POST.

How do I execute this command (curl) in rails? That is, what library contains a tool that allows me to pass an API key this way?

Here's the command I need to execute: (using the example API key)
curl -v -u 1971800d4d82861d8f2c1651fea4d212:api_token \
-X GET https://www.toggl.com/api/v8/workspaces/777/tasks
I am writing a dashing/sinatra based dashboard that shows a summary of the information from our Toggl account. My question is, how to I open this url in sinatra using something like
open('http://...) do |f|
A successful curl returns an array (json) that I can parse for the info I need.
I'm not entirely sure how to tackle this. I have some ideas but they aren't well formed enough to properly comment on them. I though I'd bounce this off of you all for inspiration and information.
I'm all ears.
Check out HTTParty. The example in the README shows basically exactly what you're looking to do using auth headers:
https://github.com/jnunemaker/httparty

Remote login into Rails 2 application

I am working on signing in a Rails 2 app remotely. Given that Rails methods are RESTful by default, I am assuming all I have to do would be POSTing form data from the remote application (a Facebook App actually). For some reason, this doesn't seem to work, I've tried:
curl http://www.zouz.com/session -d user[email]=testing#blabla.com -d user[password]=whocares
which returns and empty result. I suspected Rails' built in forgery protection might be the cause, deactivating it won't make it work.
Is there something wrong with my method ?
EDIT : If it's any help, processing the same POST query with HTTPRequester does work, although I provided the same input.
I managed to get it working, thanks to the -u flag.
curl --user testing#blabla.com:whocares http://192.168.1.19:3000
This is still cryptic to me, as I still don't know how it operates under the hood. Explanations are still welcome.

Rails: How to process a file that was sent with curl

I'm trying to create a restful api capable of receiving a file.
http://leejava.wordpress.com/2009/07/30/upload-file-from-rest-in-ruy-on-rail-with-json-format/
talks of how to accomplish this simply.
Not being very used to rails or curl, I don't know how to formulate my curl call.
Could anyone help me. Explain how to use curl to send a file, and how to use rails to accept and store the file.
Note.. the file is a 10 meg zip file. For testing I called it.
C:\test.zip..
I've looked at tutorials, but they all seem to be web page based. They also all degrade to resizing pictures.. Like uploading and presenting images are the same topic.
You need to know a bit about how your server-side is expecting the data. If it is a http POST with multipart/form-data (which I think it is), then this will probably help:
curl -F "fileParam=#c:\test.zip" -F "param1=xxx" -F "param2=yyyy" http://my.server/post-service
This will use curl to post form-data (including a file) to the given url with the given parameters. You need to know the parameter names for the file and the other optional parameters, and you have to obviously get the URL correct to reach your service.
Hope that helps.

Resources