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's the code. ANy help much appreciated! Cheers.

# oneapi.rb

require 'sinatra' require 'json' require 'ruby-debug' require 'singleton' require 'em-http-request'

# callback test post '/callbacktest' do   "Callback was #{params[0]}" end

# OneAPI payment operations class Receipts   include Singleton   $receipts =   def addReceipt(transId,jsonTrans)     $receipts["#{transId}"] = jsonTrans   end   def getReceipt(transId)     return $receipts["#{transId}"]   end end

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)     end

    def transId       return $transId     end

    def to_json(*a)       {"amountTransaction" => {         "endUserId" => @endUserId,         "paymentAmount" =>{           "chargingInformation" => {"code" => @code, "description" => @description},              "totalAmountCharged"=> "0" # the payment is only 'processing' at this point so nothing charged          },          "referenceCode" => @referenceCode,          "serverReferenceCode" =>"#{(0...6).map{65.+(rand(25)).chr}.join}", # just an example, can be any string          "resourceURL" => "http://localhost:4567/payment/#\{@endUserId\}/transactions/amount/\#\{$transId\}",          "transactionOperationStatus" => "Processing"      }}.to_json(*a)     end

    def jsonResponse       jsonTrans = self.to_json       return jsonTrans     end


  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      }   end

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

  # OK, send the JSON response...   trans.to_json

  # ...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'}   end   # RESULT: callback is sent but the original response (trans.to_json) is not sent!! end

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... trans.to_json

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