Ccyan
Ccyan

Reputation: 1057

Rails saving external event ID from external API for subsequent calls

I've been developing on Ruby on Rails for a while and this is the first time I ran into this problem. We're using an external API to allow users to open an account with them. They have an API endpoint for us to use and check account statuses and it has a limit of only 1000 events that will be sent back to us. In order to avoid that limit, we'd have to send over the last event ID that we processed so they will only send back events after that event.

For example, we just processed event with ID 10,000. Sending that as a param to their API endpoint will give us back events with ID 10,001 or greater.

What is the best way to keep track of this ID? The only way I can think of is to make a model with column external_id and keep updating that one record after everything's been processed. This does not seem like the best way to deal with this to me since I'm creating a new model with only 1 record, which seems like a weird thing to do.

I'm calling their API and processing it in a rake task that runs every 1 minute through a cronjob (using the whenever gem).

I don't mean to be subjective, I just want to know if there are any better way to handle this situation than the one I can think of right now before I go ahead and implement it. Thanks for any suggestions!

EDIT: Here's how their response would look like:

[  
     {  
        "id":1087105,
        "dateTime":"2017-01-03T15:38:58.003Z",
        "payload":"{  
            \"status\":\"PENDING\",
            \"requestId\":\"4ab7f930-cf9f-49c2-9814-3bcc2f29e652\"
        }"
     },
     {  
        "id":1087106,
        "dateTime":"2017-01-03T15:38:58.253Z",
        "payload":"{  
            \"status\":\"INVESTIGATION_SUBMITTED\",
            \"requestId\":\"4ab7f930-cf9f-49c2-9814-3bcc2f29e652\"
        }"
    }
]

The field I'd need to save is the last "id" in that response, in this case that would be 1087106.

Upvotes: 0

Views: 123

Answers (1)

Oranagwa Osmond Oscar
Oranagwa Osmond Oscar

Reputation: 326

Sounds like a great job for a simple key-value store like Redis; particularly it's counting feature, INCRBY. This article http://oldblog.antirez.com/post/take-advantage-of-redis-adding-it-to-your-stack.html explains various scenarios where you would want to use redis. The section on Counting stuff fits your problem definition.

Upvotes: 1

Related Questions