简体   繁体   中英

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

  1. Add it to your Gemfile
  2. Install using bundle
  3. Fetches API as below:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
  1. Parse JSON body:
JSON.parse(response.body)

You can read the gem docs for more information. Also, feel free to connect me via LinkedIn

It's typically terrible exercise and can reason overall performance issues. We just had to get rid of a group of these on a mission because the processor become locking up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM