Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

self tenancy example in ruby

  • Loading branch information...
commit f8827b281a6ced6baf9443720763b2327f65f17f 1 parent 279e09d
Saranyan Vigraham authored
View
24 selftenancy/langs/ruby/README.md
@@ -0,0 +1,24 @@
+Ruby Example
+====
+
+This example demonstrates how a capability can use the self token to send a message. Remember that a relationship is needed between the sender and the receiver. When you are using self-tenancy tokens, you should ensure that listener authorizes the sender capability. Here, I am demonstrating using a single capability and different routes. Sinatra is used as the wesbserver. Before running this example -
+
+1. gem install sinatra
+2. gem install httparty
+3. gem install avro
+
+You need to update the bearer tokens to reflect your credentials. To run this example, type
+
+ sinatra pingpong.rb
+
+If you want to modify the code and look at changes immediately during development without having to restart the server everytime, install the
+awesome shotgun gem
+ gem install shotgun
+ shotgun pingpong.rb
+
+Update the capability endpoint based on whatever your webserver's public ip address.
+
+ 1. Navigate to /test
+ 2. The logs will show the message being sent to the fabric at /marketplace/profile/get, which gets routed back to your application
+ 3. The important thing here is the use of self token to send messages
+
View
64 selftenancy/langs/ruby/test_tenancy.rb
@@ -0,0 +1,64 @@
+require 'rubygems'
+require 'sinatra'
+require 'httparty'
+require 'avro'
+require 'pp'
+
+
+class Fabric
+ include HTTParty
+ base_uri 'https://localhost:8080'
+end
+
+helpers do
+ def request_headers
+ env.inject({}){|acc, (k,v)| acc[$1] = v if k =~ /^http_(.*)/i; acc}
+ end
+
+ def fabric_token
+ #change this to your capability specific fabric bearer token
+ "Bearer z0d3WyWNb6MsrSh077FIs8vL0V2Yrt+Cy5hvkqw2+VJuITuL9H+MtavgA0BuCwcMdTe48Whl" #fabric bearer token
+ end
+end
+
+get '/' do
+ "go to your url/test and look at your server log"
+end
+
+#Example uses self token to send messages
+#Intent is to demonstrate self tenancy. That is listener has to have a relationship
+#with the sender.
+#Here we are using the same sender and receiver, and they have an inherent relationship
+
+get '/test' do
+ #get schema for ping
+ file = HTTParty.get("https://api.x.com/ocl/marketplace/profile/get/1.0.0")
+ #ruby hackery before parsing the schema. ensures that avro lib cleanly parses this schema
+ schema = Avro::Schema.parse(file.parsed_response.to_s.gsub(/\=\>/,':').gsub(/nil/,"\"null\""))
+ #test message
+ message = {"xProfileId" => "123"}
+ #into avro binary
+ stringwriter = StringIO.new
+ datumwriter = Avro::IO::DatumWriter.new(schema)
+ encoder = Avro::IO::BinaryEncoder.new(stringwriter)
+ datumwriter.write(message,encoder)
+
+ #send to fabric using self token
+ response = Fabric.post("/marketplace/profile/get", \
+ {:body => stringwriter.string, :headers => {'Content-Type' => 'avro/binary', \
+ 'Authorization' => "Bearer Zn9EgRFGJ6Wb8YWjIAadLUqgpk0lW7jA7RcGLKaBEd9tdPn7GXu/Q2bNKXGeKpPcgtEv4Pzo", \
+ 'X-XC-SCHEMA-VERSION' => "1.0.0"}})
+
+ "#{response.code}, #{response.headers.inspect}"
+end
+
+
+post '/marketplace/profile/get' do
+ puts "\nmessage received on /marketplace/profile/get\n-----\n"
+ message_body = request.env["rack.input"].read
+ headers = request_headers
+ puts "headers\n--------\n"
+ puts headers
+ #verify that this is from fabric
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.