Using Ruby’s succ Method to Get Around API Restrictions

I’m tasked with pulling movie info from an API, but the API has a restriction: it only allows one movie query at a time. How can I efficiently put together a movie database while only grabbing one movie at a time?

Image for post
Image for post

One possible solution: .succ

Ruby’s .succ method can be attached to a string to increment the last character by one (either alphabetically or numerically). That is, it converts a string into the successive string.

Some examples:

“1”.succ #=> “2”/ “34390909”.succ #=> “34390910”/ “abc”.succ #=> “abd”

Check out the Ruby APIdock for more examples.

Image for post
Image for post

So how does this help me?

The API’s query URLs end in a numeric values corresponding to their order of entry in the database (fortunately). In the code below, I use .succ to grab 500 movies queries from the API:

Image for post
Image for post

As you can see, the while loop counts to 500 adding each successive URL to an array of URLs. After some iteration, Net::HTTP, and finesse, I have an array of hashes containing the necessary information for my movie database.

Thanks for reading my blog. If you don’t like it, here’s a suggestion: you should.succ it:

jasons_succy_blog = “https://medium.com/@jasonmelton_/using-rubys-succ-method-to-get-around-api-restrictions-1cdb47829d62”

jasons_succy_blog.succ

Written by

Software dev. Love root beer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store