How to Parse Json with Bearer Token in Ruby - ruby-on-rails

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

Related

Why am I receiving a 500 status when trying to connect with ASANA api?

I am using this example: https://github.com/ajimix/asana-api-php-class/blob/master/examples/oauth.php to learn how to use the asana's oauth. I already registered my app on the asana's developers webpage. But, when I get the asana's response with my 'redirect_url'?code=0%XXXXXXX, localhost gives me a 500 status. I don't know why, does anyone know how I can solve it?
I tried using the other examples in there: https://github.com/ajimix/asana-api-php-class/blob/master/examples/, but I wasn't able to make them run either, I always get a 500 status. I think that the problem must be in my server, but I don't how solve it. Maybe changing something on the .htaccess file, but I don't really know what to put in there.
If you're looking to use the Asana API from PHP may I recommend the official first-party client: https://github.com/Asana/php-asana
It also contains examples of how to use with OAuth and is officially supported by us at Asana :-)

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.

REST request with oauth gem

I am missing something that should be very easy!
I am using oauth-plugin gem to access jira. I could easily build the consumer part, but now I don't know how to make a rest request.
It says in documentation:
The gem's author assumes that it is self-explanatory how to make a get/post-request. But I cannot figure it out.
I've tried to use Net::HTTP as follows:
uri = URI.parse('/oauth_consumers/jira/client/rest/api/2/project')
request = Net::HTTP.get(uri,{})
And I am getting Connection refused - connect(2) error.
I thought it could work, if I use current_user and his token (jira in my case):
current_user.jira.get('/oauth_consumers/jira/client/rest/api/2/project')
And RoR says that jira doesn't have such a method.
I've tried to search online, but there are very few projects that use oauth-plugin gem.
Thank you in advance.
You are almost right with current_user.jira.get.
Just add client after jira and change path a little bit:
current_user.jira.client.get('/rest/api/2/project')
JiraToken doesn't have get method, while AccessToken (client) does.

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

How do I send in a REST request (to use an API)?

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.

Resources