Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Home

lp edited this page · 31 revisions
Clone this wiki locally

Gem Dependencies:

  • rack
  • json
  • aws-sdb

Setup:

  • gem install ‘simple-json’
  • create a rackup file inside your Rack home directory:
# config.ru
require 'rack'
require 'simple_json'

SimpleJSON.bootstrap(
  'AMAZON_DOMAIN' => 'your_amazon_domain',
  'AMAZON_ACCESS_KEY_ID' => 'your_amazon_access_key_id',
  'AMAZON_SECRET_ACCESS_KEY' => 'your_amazon_secret_access_key'
)
run Rack::URLMap.new(
  '/echo'		=> SimpleJSON.echo,
  '/set' 		=> SimpleJSON.set,
  '/add'		=> SimpleJSON.add,
  '/delete'          => SimpleJSON.delete,
  '/get' 		=> SimpleJSON.get,
  '/query' 		=> SimpleJSON.query
)

Basic Usage:

SimpleJSON understand five basic methods, mapped to HTTP paths in the default (above) rackup file. All database logic is modeled after Amazon’s SimpleDB, enhanced with some data filtering goddies. And like SimpleDB, you must take care of generating unique IDs inside your client application, as SimpleJSON won’t bother enforcing unique ID presence politics. Please refer to Amazons’s documentation for further informations. After getting acquainted with SimpleDB’s logic (or aws-sdb for an even shorter introduction), the only other important fact to know about SimpleJSON, is that all interractions with the db logic is done through JSON trees of hashes.

/get

all of a single entry’s data:

{ "id":true }
  • returns:
{ "id":{ "key":["value"], "other_key":["other_value"] } }

all of multiple entries’s data:

{ "id":true, "other_id":true }
  • returns:
{ "id":{ "key":["value"], "other_key":["other_value"] }, "other_id":{ "key":["value"], "other_key":["other_value"] } }

only the specified “keys” for single or multiple entries:

{ "id":{ "key":true, "other_key":true }, "other_id":{ "key":true } }
  • returns:
{ "id":{ "key":["value"], "other_key":["value"] }, "other_id":{ "key":["value"] } }

/set

set the specified keys for single or multiple entries:

{ "id":{ "key":"value", "other_key":"other_value" }, "other_id":{ "key":"value" } }
  • returns:
{ "id":true } or { "id":{ "error":"error message" }

/add

append to the specified keys for single or multiple entries:

{ "id":{ "key":"value", "other_key":"other_value" }, "other_id":{ "key":"value" } }
  • returns:
{ "id":true } or { "id":{ "error":"error message" }

/query

Query the database using SimpleDB querying syntax, but with expansion and filtering over the results of the initial query.
Refer to both SimpleDB’s documentation and to Ruby’s “aws_sdb” gem to learn more about the possibilities of the syntax.

to get all informations from the resulting records:

{ "['city' = 'Montreal' and 'age' >= '18']":true }
  • returns:
{ "['city' = 'Geneva' and 'age' >= '100']":{ '0025':{ 'first':'Albert', 'last':'Hoffman', 'age':'104', 'city':'Geneva', 'title':'Dr.' } } }

to get only the id from the resulting records:

{ "['city' = 'Geneva' and 'age' >= '100']":null }
  • returns:
{ "['city' = 'Geneva' and 'age' >= '100']":{ '0025':null } }

to get only some informations from the resulting records:

{ "['city' = 'Geneva' and 'age' >= '100']":{ 'last':true, 'title':true } }
  • returns:
{ "['city' = 'Geneva' and 'age' >= '100']":{ '0025':{ 'last':'Hoffman', 'title':'Dr.' } }

/delete

deleting single or multiple entries:

{ 'id':null }
  • returns:
{ 'id':true } or { 'id':{ 'error':'message' } }

Advanced Operations:

filtering returned list items

Amazon SimpleDB has arrays instead of database fields, meaning you can append to those arrays, as stated above with the “/add” method. SimpleJSON also allow you to get and query these database arrays almost as if they where real arrays. The same filtering logic applies to both “/get” and “/query” methods.

supported array operations:

  • index specified as positive and negative integers
  • list slice specified as a string representation of 2 integers joined by 2 or 3 dots (“1..3” = index 1-2-3, “1…3” = index 1-2)
  • the string “first”
  • the string “last”
  • example data:
{ '2323':{ 'shop':'fruit market', 'basket':[ 'apple', 'banana', 'cherry' ] } }
  • /get
{ '2323':{ 'basket':0 } }
returns { '2323':{ 'basket':'apple' } }
  • /get
{ '2323':{ 'basket':'last' } }
returns { '2323':{ 'basket':'cherry' } }
  • /query
{ "[ 'shop' = 'fruit market' ]":{ 'basket':'1..-1' } }
returns { "[ 'shop' = 'fruit market' ]":{ '2323':{ 'basket':[ 'banana', 'cherry' ] } } }
  • /query
{ "[ 'shop' = 'fruit market' ]":{ 'basket':'first' } }
returns { "[ 'shop' = 'fruit market' ]":{ '2323':{ 'basket':'apple' } } }
Something went wrong with that request. Please try again.