Forum: Ruby on Rails Sinatra: can I return response AND post a callback?

39e45b584e1a4b7e9a47c48cbe13d6bb?d=identicon&s=25 Kevin Smith (kevsy)
on 2011-04-08 12:52
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]}"

# 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!!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2011-04-08 13:30
(Received via mailing list)
On Apr 8, 11:52am, Kevin Smith <> 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...
>  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

Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.