JSONCanvas Implementation for Ruby.
To install JsonCanvas gem, add this line to your application's Gemfile:
gem 'json_canvas'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install json_canvas
To use JSONCanvas, include the gem in your Ruby script:
require 'json_canvas'
Create a new canvas using:
jc = JsonCanvas.create
Add text nodes to the canvas:
text_node = jc.add_text(text: "Hi")
You can customize the text node with parameters like id, position, size, content and color:
custom_text = jc.add_text(id: "unique_id", x: 50, y: 100, width: 200, height: 50, text: "Hello World!", color: "2")
Add file and link nodes with specific attributes:
file_node = jc.add_file(file: "path/to/file")
link_node = jc.add_link(url: "https://example.com")
group_node = jc.add_group(label: "Test Group")
Connect nodes with edges:
jc = JsonCanvas.create
start = jc.add_text(id: "START", text: "start")
goal = jc.add_text(id: "GOAL", x: 400, text: "goal")
jc.add_edge(id: "edge1", fromNode: start.id, toNode: goal.id)
jc.add_edge(id: "edge2", fromNode: start.id, fromSide: "top", fromEnd: "arrow", toNode: goal.id, toSide: "bottom", toEnd: "arrow", color: "2", label: "HELLO")
To convert your canvas instance into a JSON string, you can use the to_json
method.
jc.to_json #=> '{"nodes":[{"id":"START","x":0,"y":0,"width":250,"height":60,"type":"text","text":"start"},{"id":"GOAL","x":400,"y":0,"width":250,"height":60,"type":"text","text":"goal"}],"edges":[{"id":"edge1","fromNode":"START","toNode":"GOAL","fromSide":"right","toSide":"left"},{"id":"edge2","fromNode":"START","toNode":"GOAL","fromSide":"top","fromEnd":"arrow","toSide":"bottom","toEnd":"arrow","color":"2","label":"HELLO"}]}'
Save the canvas to a file or load from it:
# Save to "sample.canvas" file.
jc.save("sample.canvas")
# Load to canvas object.
loaded_canvas = JsonCanvas.parse(File.read("sample.canvas"))
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test-unit
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/ongaeshi/json_canvas.
The gem is available as open source under the terms of the MIT License.