json_canvas 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbe2e6180658f10ea38a5782675140eb70e38739703bf60b57ece49d166e9e0a
4
+ data.tar.gz: 39cd1729ad8e72ed49d8640efabae67ef5c2310a16b02544c93c3f4292ac725c
5
+ SHA512:
6
+ metadata.gz: 3bfe2c08f98160c0a96b2e08f92f7dfcbab6c13aa69d5f928067fea1b20e3f42d9a650ce22196b0d481afcf742012bb5344366abe46517f66708067697fe7472
7
+ data.tar.gz: f98b2390fc75a8d135e3507825c9ae6a08f9e16ae3d357b8f74c6da52b7442f43a14ccc376efdd0bb56083b000b5e94a5b1bc9bff3ec718c518d86ec493bf29e
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-04-29
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in json_canvas.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_canvas (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.7.2)
11
+ language_server-protocol (3.17.0.3)
12
+ lint_roller (1.1.0)
13
+ parallel (1.24.0)
14
+ parser (3.3.1.0)
15
+ ast (~> 2.4.1)
16
+ racc
17
+ power_assert (2.0.3)
18
+ racc (1.7.3)
19
+ rainbow (3.1.1)
20
+ rake (13.2.1)
21
+ regexp_parser (2.9.0)
22
+ rexml (3.2.6)
23
+ rubocop (1.62.1)
24
+ json (~> 2.3)
25
+ language_server-protocol (>= 3.17.0)
26
+ parallel (~> 1.10)
27
+ parser (>= 3.3.0.2)
28
+ rainbow (>= 2.2.2, < 4.0)
29
+ regexp_parser (>= 1.8, < 3.0)
30
+ rexml (>= 3.2.5, < 4.0)
31
+ rubocop-ast (>= 1.31.1, < 2.0)
32
+ ruby-progressbar (~> 1.7)
33
+ unicode-display_width (>= 2.4.0, < 3.0)
34
+ rubocop-ast (1.31.3)
35
+ parser (>= 3.3.1.0)
36
+ rubocop-performance (1.20.2)
37
+ rubocop (>= 1.48.1, < 2.0)
38
+ rubocop-ast (>= 1.30.0, < 2.0)
39
+ ruby-progressbar (1.13.0)
40
+ standard (1.35.1)
41
+ language_server-protocol (~> 3.17.0.2)
42
+ lint_roller (~> 1.0)
43
+ rubocop (~> 1.62.0)
44
+ standard-custom (~> 1.0.0)
45
+ standard-performance (~> 1.3)
46
+ standard-custom (1.0.2)
47
+ lint_roller (~> 1.0)
48
+ rubocop (~> 1.50)
49
+ standard-performance (1.3.1)
50
+ lint_roller (~> 1.1)
51
+ rubocop-performance (~> 1.20.2)
52
+ test-unit (3.6.2)
53
+ power_assert
54
+ unicode-display_width (2.5.0)
55
+
56
+ PLATFORMS
57
+ x64-mingw-ucrt
58
+ x86_64-linux
59
+
60
+ DEPENDENCIES
61
+ json_canvas!
62
+ rake (~> 13.0)
63
+ standard (~> 1.3)
64
+ test-unit (~> 3.0)
65
+
66
+ BUNDLED WITH
67
+ 2.5.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ongaeshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # JsonCanvas
2
+
3
+ [JSONCanvas](https://jsoncanvas.org/spec/1.0/) Implementation for Ruby.
4
+
5
+ ## Installation
6
+
7
+ To install JsonCanvas gem, add this line to your application's Gemfile:
8
+
9
+ ```bash
10
+ gem 'json_canvas'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install json_canvas
23
+ ```
24
+
25
+ ## Usage
26
+
27
+
28
+ To use JSONCanvas, include the gem in your Ruby script:
29
+
30
+ ```ruby
31
+ require 'json_canvas'
32
+ ```
33
+
34
+ ### Creating a Canvas
35
+
36
+ Create a new canvas using:
37
+
38
+ ```ruby
39
+ jc = JsonCanvas.create
40
+ ```
41
+
42
+ ### Adding Text Nodes
43
+
44
+ Add text nodes to the canvas:
45
+
46
+ ```ruby
47
+ text_node = jc.add_text(text: "Hi")
48
+ ```
49
+
50
+ You can customize the text node with parameters like id, position, size, content and color:
51
+
52
+ ```ruby
53
+ custom_text = jc.add_text(id: "unique_id", x: 50, y: 100, width: 200, height: 50, text: "Hello World!", color: "2")
54
+ ```
55
+
56
+ ![image](https://github.com/ongaeshi/json_canvas/assets/154418/4138e411-3d21-4637-bd1b-3cdebe69a398)
57
+
58
+ ### Advanced Node Types
59
+
60
+ Add file and link nodes with specific attributes:
61
+
62
+ ```ruby
63
+ file_node = jc.add_file(file: "path/to/file")
64
+ link_node = jc.add_link(url: "https://example.com")
65
+ group_node = jc.add_group(label: "Test Group")
66
+ ```
67
+
68
+ ### Edge
69
+
70
+ Connect nodes with edges:
71
+
72
+ ```ruby
73
+ jc = JsonCanvas.create
74
+ start = jc.add_text(id: "START", text: "start")
75
+ goal = jc.add_text(id: "GOAL", x: 400, text: "goal")
76
+ jc.add_edge(id: "edge1", fromNode: start.id, toNode: goal.id)
77
+ jc.add_edge(id: "edge2", fromNode: start.id, fromSide: "top", fromEnd: "arrow", toNode: goal.id, toSide: "bottom", toEnd: "arrow", color: "2", label: "HELLO")
78
+ ```
79
+
80
+ ![image](https://github.com/ongaeshi/json_canvas/assets/154418/820d4709-c9fd-41a6-9548-fe6b52177273)
81
+
82
+ ### Get JSON string
83
+
84
+ To convert your canvas instance into a JSON string, you can use the `to_json` method.
85
+
86
+ ```ruby
87
+ 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"}]}'
88
+ ```
89
+
90
+ ### Saving and Loading
91
+ Save the canvas to a file or load from it:
92
+
93
+ ```ruby
94
+ # Save to "sample.canvas" file.
95
+ jc.save("sample.canvas")
96
+
97
+ # Load to canvas object.
98
+ loaded_canvas = JsonCanvas.parse(File.read("sample.canvas"))
99
+ ```
100
+
101
+ ## Development
102
+
103
+ 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.
104
+
105
+ 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](https://rubygems.org).
106
+
107
+ ## Contributing
108
+
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ongaeshi/json_canvas.
110
+
111
+ ## License
112
+
113
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "json_canvas/node"
5
+ require "json_canvas/edge"
6
+
7
+ module JsonCanvas
8
+ class Canvas
9
+ attr_reader :nodes, :edges
10
+
11
+ def self.parse(json)
12
+ obj = JSON.parse(json)
13
+ nodes = obj["nodes"].map do |x|
14
+ case x["type"]
15
+ when "text"
16
+ TextNode.new(**x.transform_keys(&:to_sym))
17
+ when "file"
18
+ FileNode.new(**x.transform_keys(&:to_sym))
19
+ when "link"
20
+ LinkNode.new(**x.transform_keys(&:to_sym))
21
+ when "group"
22
+ GroupNode.new(**x.transform_keys(&:to_sym))
23
+ else
24
+ raise
25
+ end
26
+ end
27
+ edges = obj["edges"].map { |x| Edge.new(**x.transform_keys(&:to_sym)) }
28
+ Canvas.new(nodes, edges)
29
+ end
30
+
31
+ def initialize(nodes = [], edges = [])
32
+ @nodes = nodes
33
+ @edges = edges
34
+ end
35
+
36
+ def add_text(**)
37
+ node = TextNode.new(**)
38
+ nodes.push(node)
39
+ node
40
+ end
41
+
42
+ def add_file(**)
43
+ node = FileNode.new(**)
44
+ nodes.push(node)
45
+ node
46
+ end
47
+
48
+ def add_link(**)
49
+ node = LinkNode.new(**)
50
+ nodes.push(node)
51
+ node
52
+ end
53
+
54
+ def add_group(**)
55
+ node = GroupNode.new(**)
56
+ nodes.push(node)
57
+ node
58
+ end
59
+
60
+ def add_edge(**)
61
+ edge = Edge.new(**)
62
+ edges.push(edge)
63
+ edge
64
+ end
65
+
66
+ def to_json
67
+ JSON.generate({
68
+ nodes: nodes.map { |x| x.to_hash },
69
+ edges: edges.map { |x| x.to_hash }
70
+ })
71
+ end
72
+
73
+ def save(filename)
74
+ File.write(filename, to_json)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module JsonCanvas
6
+ class Edge
7
+ attr_accessor :id, :from_node, :from_side, :from_end, :to_node, :to_side, :to_end, :color, :label
8
+
9
+ def initialize(**kwargs)
10
+ @id = kwargs[:id] || SecureRandom.uuid.delete("-")[0...16]
11
+ @from_node = kwargs[:fromNode] || raise
12
+ @from_side = kwargs[:fromSide] || "right" # "top" | "right" | "bottom" | "left"
13
+ @from_end = kwargs[:fromEnd] # "none" | "arrow"
14
+ @to_node = kwargs[:toNode] || raise
15
+ @to_side = kwargs[:toSide] || "left"
16
+ @to_end = kwargs[:toEnd]
17
+ @color = kwargs[:color]
18
+ @label = kwargs[:label]
19
+ end
20
+
21
+ def to_hash
22
+ h = {
23
+ "id" => id,
24
+ "fromNode" => from_node,
25
+ "toNode" => to_node
26
+ }
27
+ h["fromSide"] = from_side if from_side
28
+ h["fromEnd"] = from_end if from_end
29
+ h["toSide"] = to_side if to_side
30
+ h["toEnd"] = to_end if to_end
31
+ h["color"] = color if color
32
+ h["label"] = label if label
33
+ h
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module JsonCanvas
6
+ class GenericNode
7
+ attr_accessor :id, :x, :y, :width, :height, :color
8
+
9
+ def initialize(**kwargs)
10
+ @id = kwargs[:id] || SecureRandom.uuid.delete("-")[0...16]
11
+ @x = kwargs[:x] || 0
12
+ @y = kwargs[:y] || 0
13
+ @width = kwargs[:width] || default_width
14
+ @height = kwargs[:height] || default_height
15
+ @color = kwargs[:color] # Optional
16
+ end
17
+
18
+ def default_width = is_a?(TextNode) ? 250 : 400
19
+
20
+ def default_height = is_a?(TextNode) ? 60 : 400
21
+
22
+ def to_hash_common(type)
23
+ h = {
24
+ "id" => id,
25
+ "x" => x,
26
+ "y" => y,
27
+ "width" => width,
28
+ "height" => height
29
+ }
30
+ h["color"] = color if color
31
+ h["type"] = type
32
+ h
33
+ end
34
+ end
35
+
36
+ class TextNode < GenericNode
37
+ attr_accessor :type, :text
38
+
39
+ def initialize(**kwargs)
40
+ super(**kwargs)
41
+ @type = "text"
42
+ @text = kwargs[:text] || ""
43
+ end
44
+
45
+ def to_hash
46
+ h = to_hash_common(type)
47
+ h["text"] = text
48
+ h
49
+ end
50
+ end
51
+
52
+ class FileNode < GenericNode
53
+ attr_accessor :type, :file, :subpath
54
+
55
+ def initialize(**kwargs)
56
+ super(**kwargs)
57
+ @type = "file"
58
+ @file = kwargs[:file] || ""
59
+ @subpath = kwargs[:subpath]
60
+ end
61
+
62
+ def to_hash
63
+ h = to_hash_common(type)
64
+ h["file"] = file
65
+ h["subpath"] = subpath if subpath
66
+ h
67
+ end
68
+ end
69
+
70
+ class LinkNode < GenericNode
71
+ attr_accessor :type, :url
72
+
73
+ def initialize(**kwargs)
74
+ super(**kwargs)
75
+ @type = "link"
76
+ @url = kwargs[:url] || "https://www.ruby-lang.org"
77
+ end
78
+
79
+ def to_hash
80
+ h = to_hash_common(type)
81
+ h["url"] = url
82
+ h
83
+ end
84
+ end
85
+
86
+ class GroupNode < GenericNode
87
+ attr_accessor :type, :label, :background, :background_style
88
+
89
+ def initialize(**kwargs)
90
+ super(**kwargs)
91
+ @type = "group"
92
+ @label = kwargs[:label]
93
+ @background = kwargs[:background]
94
+ @background_style = kwargs[:backgroundStyle]
95
+ end
96
+
97
+ def to_hash
98
+ h = to_hash_common(type)
99
+ h["label"] = label if label
100
+ h["background"] = background if background
101
+ h["backgroundStyle"] = background_style if background_style
102
+ h
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonCanvas
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json_canvas/canvas"
4
+ require "json_canvas/edge"
5
+ require "json_canvas/node"
6
+ require "json_canvas/version"
7
+
8
+ module JsonCanvas
9
+ def self.create
10
+ Canvas.new
11
+ end
12
+
13
+ def self.parse(json)
14
+ Canvas.parse(json)
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ module JsonCanvas
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_canvas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ongaeshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: JsonCanvas is a Ruby gem that provides a robust implementation of the
14
+ JSONCanvas specification. This gem enables developers to easily create and manipulate
15
+ JSONCanvas format. It supports functionalities such as adding text, files, links,
16
+ and grouping nodes with customizable attributes like position, size, and identifiers.
17
+ Additional features include connecting nodes with edges that have customizable attributes,
18
+ and the ability to save and load canvas states from JSON files. This gem facilitates
19
+ the easy integration of the JSONCanvas format into Ruby applications, enhancing
20
+ development efficiency and user experience.
21
+ email:
22
+ - ongaeshi0621@gmail.com
23
+ executables: []
24
+ extensions: []
25
+ extra_rdoc_files: []
26
+ files:
27
+ - ".standard.yml"
28
+ - CHANGELOG.md
29
+ - Gemfile
30
+ - Gemfile.lock
31
+ - LICENSE.txt
32
+ - README.md
33
+ - Rakefile
34
+ - lib/json_canvas.rb
35
+ - lib/json_canvas/canvas.rb
36
+ - lib/json_canvas/edge.rb
37
+ - lib/json_canvas/node.rb
38
+ - lib/json_canvas/version.rb
39
+ - sig/json_canvas.rbs
40
+ homepage: https://github.com/ongaeshi/json_canvas
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/ongaeshi/json_canvas
45
+ source_code_uri: https://github.com/ongaeshi/json_canvas
46
+ changelog_uri: https://github.com/ongaeshi/json_canvas/blob/master/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 3.0.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.5.10
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: JSONCanvas Implementation for Ruby.
66
+ test_files: []