Building A Graph Traversal API using Sinatra, Pacer and Neo4j

Brought to you by
Graph Traversal API
If you are building a graph-based web application, you will (most likely) a web API that exposes your graph to the world.
Let's see how to do that with Pacer and Sinatra...
Simple Demo
$ gem install sinatra
$ gem install trinidad
$ ruby sinatra_simple_demo.rb
Run the simple demo (in the root of our repo)
Make an API call
$ curl http://localhost:4567/customers/CENTC
{"phone":"(5) 555-3392",
"companyName":"Centro comercial Moctezuma",
"customerID":"CENTC","fax":"(5) 555-7293","type":"Customer"}
How does it work?
require 'sinatra'
# ...
@@g = Pacer.tg # Global graph object
# ...
get '/customers/:customer_id' do
content_type :json
id = params[:customer_id]
customer = @@g.v(NorthWind::Customer, customerID: id).first
if customer
customer.properties.to_json
else
status 404
end
end
Exposing Traversals
get '/customers/:customer_id/purchases' do
content_type :json
id = params[:customer_id]
products = @@g.v(NorthWind::Customer, customerID: id)
.products.uniq
if products
products.properties.to_a.to_json
else
status 404
end
end
http://localhost:4567/customers/CENTC/purchases
Allows to send GET requests to
Pacer NorthWind - Demo
Building a graph traversal API using

Setup
Step 1:
gem install pacer
gem install pacer-neo4j
Step 2:
gem install sinatra
gem install trinidad
Step 3:
Running the demo
cd pacer-northwind
ruby run_demo.rb ~/pacer-northwind-data
- The path ~/pacer-northwind-data specifies where to store the Neo4j data files.
- If you do not specify a path, the demo will run with TinkerGraph, the built-in, in-memory graph we have been using all day.




That's it, folks
Graph Training - 6
By XN Logic Corporation
Graph Training - 6
A demo of a web application that uses Sinatra, Pacer and Neo4j to create a graph traversal API.
- 907