Once we established the connection with the website and authenticated, we then needed to take the cookie that we got back and save it so that we could call it in the future. I did a bunch of research and looked at a bunch of different options. Quickly, I narrowed it down to a few possibilities. I was considering HTTParty which I had heard good things about and seemed to have good reviews from people about being easy to use and implement. The other option was Faraday which also seems to be pretty popular and was super easy to set up and get running, at least to establish the connection. I also researched an option that included using curl commands in order to save and then reaccess the cookie. There were a few faraday middleware gems that I experimented with but in the end didn’t need to use them. I ended up with this:
@authentication_cookie = response.env[:response_headers]["set-cookie"]
This line finds the header “set-cookie” in the response and sets it to the instance variable.
Then, I wrote this
response = conn.get do |req|
req.headers['cookie'] = authentication_cookie
req.url endpoint, params
This code makes another request from the same connection established earlier and sets the header cookies to authentication cookie. It then tells the connection to use a specific endpoint (using the API, based on what information you would like) and adds some parameters. This sets the response to whatever is returned from the api and then
response.bodytakes that response and prints out the content of the response but without the headers or other junk we don’t really want to see.