Been a While…
So…October already and I have dropped a few balls when it comes to keeping up on writing. Had to make it a point to get back here and at least break the cycle so I don’t come back in a year and have no idea where I’ve been. With plenty of ‘life’ happening, it has been very busy but I have stayed the course on the Web Development path. Yes, it has been tough but also fun, which is great, since this only means I am pursuing what keeps me motivated and intrigued.
Thinking back to my last post, I was working on Minesweeper. Since then, it was a look into files, data structures and algorithms. Here was a taste of file I/O, recursion, regexes and the Big O. Oh, there was also trees and graphs and ways to search through them. Yes, it was painful at first, but I did make it through. Toward the end, it was a DOM parser to get a taste of where I am at today, hitting the web!
Comb the Desert!
The recent projects leading up to today have been regarding getting into the web, digging through html and pulling information into my own apps to do what I need. Before getting into Rails, I have been using Sinatra. This is a great way to get web applications running quickly.
In a previous project, I had created a web scrapers and API calls that could pull specific info. A job scraper that could tell me all the jobs in a specific city or a weather app that used API’s to get local forecasts. Taking these ideas into Sinatra, now is the time to put a few together and make something slightly more complicated.
Then end game? An application that pulls job data from one site, cross references the reviews from another site and puts it on the web page. There are few other tricks to throw in there, but one thing at a time.
Get the IP?!
The one thing that I ran into that was horribly frustrating was grabbing an IP address of the user, that wasn’t my localhost. Sinatra uses a local server I can access with something like localhost:4567. The idea is I try to get an IP address not related to this, but to my actual IP. Checking Stack Overflow, it seemed so easy, just use request.ip.
get '/' do
ip = request.ip
Only problem, which I still haven’t figured out, is I kept getting the loopback ip, ‘::1’. I couldn’t do anything with that, that I knew of. It drove me nuts, because when using an API like freegeoip.net from my terminal, it gave me exactly what I needed. But, that was in my terminal. I needed a way to transfer that to my app.
Many more hours of Googling later, I finally found another way. Using the open-uri gem. Call it a work around, but the idea was mimicking the terminal in my Ruby app. Store that response in a string ( which is a JSON object ) and then dig through that to get what I needed. I put this in a mini class that was called in my main app.
location_info = open( "https://www.freegeoip.net/json/" ).read
This pinged the API and the response was stored in the location_info variable. Since it is a JSON object I parsed it in the app to get the hash I needed to access everything I needed.
loc = GeoLocation.new
loc_hash = JSON.parse( loc.location_info )
Accessing the specific keys was done as expected…
This is what worked for me, but I know I was just doing something wrong with my previous tries, which I plan to figure out as well. But, for anyone looking for an alternative, try this.
With the location settings figured out, its on to the next API, which is pulling job reviews, salaries, etc from another site. That’s all great, but apparently in my initial attempts to ping the site, I have gotten myself banned…oops. Better send an email and ask for forgiveness. Trial and error, right? Hopefully, it’s something I can fix easily, but we will see!