Crossbar HTTP
Module that provides methods for accessing Crossbar.io HTTP Bridge Services
Revision History
- v0.1.3:
- Removed dependency on Ruby 2.3
- v0.1.2:
- Added the ability to override the date serialization
- v0.1.1:
- Fixed typos in README
- Added support for https
- v0.1.0:
- Initial version
Installation
gem 'crossbar-http'
Usage
Call
To call a Crossbar HTTP bridge, do the following
client = Crossbar::HTTP::Client.new("http://127.0.0.1/call")
result = client.call("com.example.add", 2, 3, offset: 10)
This will call the following method
def onJoin(self, details):
def add_something(x, y, offset=0):
print "Add was called"
return x+y+offset
self.register(add_something, "com.example.add")
Publish
To publish to a Crossbar HTTP bridge, do the following
client = Crossbar::HTTP::Client.new("http://127.0.0.1/publish")
result = client.publish("com.example.event", event: "new event")
The receiving subscription would look like
def onJoin(self, details):
def subscribe_something(event=None, **kwargs):
print "Publish was called with event %s" % event
self.subscribe(subscribe_something, "com.example.event")
Key/Secret
For bridge services that have a key and secret defined, simply include the key and secret in the instantiation of the client.
client = Crossbar::HTTP::Client.new("http://127.0.0.1/publish", key: "key", secret: "secret")
Pre-Serialize Callback
The library provides a "pre-serialize" callback which will allow the user to modify certain object types before the payload is serialized. This can be used for things like changing the date/time to a custom value. See an example of doing this below
pre_serialize = lambda { |value|
if value.is_a? Date
return value.strftime("%Y/%m/%d %H:%M:%S.%L %Z")
end
}
client = Crossbar::HTTP::Client.new("http://127.0.0.1/publish", pre_serialize: pre_serialize)
Contributing
To contribute, fork the repo and submit a pull request.
Testing
To run the unit tests, execute the following
%> rake spec
License
MIT