gingerhendrix / json_proxy

A json_p server for proxying web api requests.

This URL has Read+Write access

name age message
file .gitignore Mon Dec 29 18:44:45 -0800 2008 Added pkg/ to gitignore [gingerhendrix]
file History.txt Wed May 06 10:26:59 -0700 2009 Updated todo [gingerhendrix]
file PLANNING Sat Aug 15 05:19:34 -0700 2009 Update planning and todo [gingerhendrix]
file README.rdoc Mon Sep 29 14:59:27 -0700 2008 Fixing rdoc formatting [gingerhendrix]
file Rakefile Wed May 06 06:36:26 -0700 2009 Updated rake task [gingerhendrix]
file TODO Sat Aug 15 05:19:34 -0700 2009 Update planning and todo [gingerhendrix]
directory bin/ Mon Dec 29 18:41:28 -0800 2008 Added exception handling to json_proxy script. ... [gingerhendrix]
directory lib/ Sat Sep 26 11:34:17 -0700 2009 Added database method to route handler for use ... [Gareth Andrew]
directory tasks/ Fri Aug 14 10:45:19 -0700 2009 Now uses couchrest instead of couchserver [gingerhendrix]
directory test/ Sat Aug 15 05:03:17 -0700 2009 Added access to underlying cache object via req... [gingerhendrix]
directory website/ Mon Dec 29 19:40:37 -0800 2008 Removing generated file [gingerhendrix]
README.rdoc

Readme

Json Proxy is a small server/dsl intending for serving JSON documents, and in particular JSON documents created from external webservices. JsonProxy supports

  • Caching - with either CouchDb or a simple Filesystem based cache
  • Asynchronous Operation - Json Proxy supports asynchronous data loading via a HTTP polling mechanism

Prequisites

  • Couchdb (For caching, alternative FileCache exists)
  • Starling (For queue)
  • Rack (For server)

DSL

The JsonProxy DSL is used for creating new services

  np_namespace "echo" do |ns|
    ns.route 'echo', [:message] do |message|
      {:message => message}
    end
  end

Each route should return an object with a #to_json method.

Cache

All queries are cached, the cache can be controlled by setting the cache_expiry and cache_key options.

JSON Envelope

The response is always a JSON object, this object is an envelope around the data. The data can be accessed through the data property of the response, the other properties of the response are used for meta-information such as the response code and response message.

  http://localhost/echo/echo?message=blah&jsonp=callback
    => {status : 200, statusMessage : "Ok", data : {message : "blah" } }

JSON-P

If the request has an argument ‘jsonp’ the server will prefix the response with the argument value, allowing the response to be used to trigger callbacks.

  http://localhost/echo/echo?message=blah&jsonp=callback
    => callback({status : 200, statusMessage : "Ok", data : {message : "blah" } })

Asynchronous Operation

JSON Proxy supports asynchronous operation by returning immediately if the data is not in the cache. The server returns a 202 status code in this scenario. The operation is then performed in the background, while the client polls the server. Once the operation has completed the data will be available in the cache and will be sent to the client on the next request.

  Loop while status is 202
    http://localhost/echo/echo?message=blah
      => {status : 202, statusMessage : "Processing. Please retry shortly" }
  end
  http://localhost/echo/echo?message=blah
    => {status : 200, statusMessage : "Ok", data : {message : "blah" } }

Background processing is performed by using a shared queue (implemented using starling). When a cache miss occurs the server adds the current url to a queue including an extra force parameter. The QueueDaemon gets urls from the queue and fetches them. The force parameter ensures that the operation is processed synchronously.