Sinatra: can I return response AND post a callback?

Hi everyone,

Using the wonderful Sinatra to create a RESTful Web service. What I want
to do is:

1 receive a POST and return a JSON response.

2 after a short period send a POST to a URL provided in a “callback”
param during 1 above

So far I can receive the client’s POST and eithr return a JSON response,
or post to the callback URL. BUT, I can’t do both.

Have tried to use Threads but no luck.

Now trying event-machine-http but I’m sure I’m missing
something…here’s the code. ANy help much appreciated! Cheers.


require ‘sinatra’
require ‘json’
require ‘ruby-debug’
require ‘singleton’
require ‘em-http-request’

callback test

post ‘/callbacktest’ do
“Callback was #{params[0]}”

OneAPI payment operations

class Receipts
include Singleton
$receipts =
def addReceipt(transId,jsonTrans)
$receipts[“#{transId}”] = jsonTrans
def getReceipt(transId)
return $receipts[“#{transId}”]

receipts = Receipts.instance

post ‘/payment/transactions/amount’ do # => creates a new charge
content_type :json, :charset => ‘utf-8’

class Transaction
def initialize(endUserId, code, referenceCode, callback=false)
@endUserId = endUserId
@code = code
@referenceCode = referenceCode
$callback = callback
$transId = rand(1000)

def transId
  return $transId

def to_json(*a)
  {"amountTransaction" => {
    "endUserId" => @endUserId,
    "paymentAmount" =>{
      "chargingInformation" => {"code" => @code, "description" =>

“totalAmountCharged”=> “0” # the payment is only
‘processing’ at this point so nothing charged
“referenceCode” => @referenceCode,
=>“#{(0…6).map{65.+(rand(25)).chr}.join}”, # just an example, can be
any string
“resourceURL” =>
“transactionOperationStatus” => “Processing”

def jsonResponse
  jsonTrans = self.to_json
  return jsonTrans


def make_request(site=‘http://localhost:4567/callbacktest’, body={})
http = :body => body
http.errback { p ‘request failed’ }
http.callback {
p http.response_header.status
p http.response_header
p http.response

trans =[:endUserId],params[:code],
params[:referenceCode], params[:callback])

OK, send the JSON response…


…and now send the callback. Just testing the mechanism for now

TODO this will be the transaction JSON but with amendments:

transactionOperationStatus changed

and currency/amount added. do
# fire callback
EM.add_timer(5) {make_request “http://localhost:4567/callbacktest”,
:param => ‘hi there’}

RESULT: callback is sent but the original response (trans.to_json)

is not sent!!

On Apr 8, 11:52am, Kevin S. [email protected] wrote:

Hi everyone,

Using the wonderful Sinatra to create a RESTful Web service. What I want
to do is:

You might note this isn’t a sinatra group.

OK, send the JSON response…


Not very familiar with sinatra, but I don’t think this sends anything
at all - sinatra uses the result of the last expression in the block
as its response
If you do something else after this, then that’s what sinatra will use
as its response

Also, beware global variables - if you ever got concurrent requests
your code would do horrible things