Skip to content

Instantly share code, notes, and snippets.

@delba
Created April 22, 2014 14:46
Show Gist options
  • Save delba/11182034 to your computer and use it in GitHub Desktop.
Save delba/11182034 to your computer and use it in GitHub Desktop.
Polling bank account status
class Account < ActiveRecord::Base
enum status: %i(processing processed errored)
end
class BankAccountsController < ApplicationController
def status
@account = Account.select(:id, :status).find(params[:id])
render json: { status: @account.status }
end
end
<% if Rails.env.development? %>
<%= link_to "Poll Account Status", '#', id: 'status', data: {
status_url: status_path @account
} %>
<% end %>
class Poller
@request: (url) ->
new Poller(url).request()
constructor: (@url) ->
request: =>
$.getJSON @url, @response
response: (json) =>
switch json.status
when 'processing' then do @processing
when 'processed' then do @processed
when 'errored' then do @errored
processing: ->
console.log 'processing'
do @poll
processed: ->
console.log 'processed'
errored: ->
console.log 'errored'
poll: ->
setTimeout @request, 1000
$(document).on 'click', '#status', ->
Poller.request @dataset.statusUrl
false
Started GET "/bank_account/status?id=11" for 127.0.0.1 at 2014-04-08 10:40:21 -0700
Processing by BankAccountsController#status as JSON
Parameters: {"id"=>"11"}
BankAccount Load (0.2ms) SELECT "bank_accounts"."id", "bank_accounts"."status" FROM "bank_accounts" WHERE "bank_accounts"."id" = $1 LIMIT 1 [["id", 11]]
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.2ms)
Started GET "/bank_account/status?id=11" for 127.0.0.1 at 2014-04-08 10:40:22 -0700
Processing by BankAccountsController#status as JSON
Parameters: {"id"=>"11"}
BankAccount Load (0.2ms) SELECT "bank_accounts"."id", "bank_accounts"."status" FROM "bank_accounts" WHERE "bank_accounts"."id" = $1 LIMIT 1 [["id", 11]]
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.2ms)
Started GET "/bank_account/status?id=11" for 127.0.0.1 at 2014-04-08 10:40:23 -0700
Processing by BankAccountsController#status as JSON
Parameters: {"id"=>"11"}
BankAccount Load (0.2ms) SELECT "bank_accounts"."id", "bank_accounts"."status" FROM "bank_accounts" WHERE "bank_accounts"."id" = $1 LIMIT 1 [["id", 11]]
Completed 200 OK in 2ms (Views: 0.2ms | ActiveRecord: 0.2ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment