statosio 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/statosio.rb ADDED
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'boilerplate'
3
+ require_relative "statosio/version"
4
+ require 'puppeteer'
5
+
6
+ module Statosio
7
+ class Error < StandardError; end
8
+
9
+
10
+ class Generate < Boilerplate
11
+ def svg( dataset: nil, x: nil, y: nil, options: nil, silent: false )
12
+ valid = values_validation( dataset: dataset, x: x, y: y, options: options, allow_list: @options_allow_list, silent: silent )
13
+ if valid
14
+ values = {}
15
+ values[:dataset] = dataset
16
+ values[:x] = x
17
+ values[:y] = y
18
+ values[:options] = options
19
+ render_svg( values )
20
+ end
21
+ end
22
+
23
+
24
+ def get_options_allow_list
25
+ @options_allow_list
26
+ end
27
+
28
+
29
+ def render_prepare( values )
30
+ values[:y].class.to_s == 'String' ? values[:y] = [ values[:y] ] : ''
31
+ self.set_markers_value( values )
32
+ self.set_boilerplate
33
+ html = self.get_boilerplate
34
+ end
35
+
36
+
37
+ def render_svg( values )
38
+ html = self.render_prepare( values )
39
+ g = {}
40
+ id = 'd3_statosio'
41
+ g[:start] = '<div id="' + id + '">'
42
+ g[:end] = '</div>'
43
+
44
+ svg = Puppeteer.launch do | browser |
45
+ page = browser.pages.last || browser.new_page
46
+ page.set_content( html )
47
+ html = page.evaluate( 'document.body.innerHTML' )
48
+ tmp = html[ html.index( g[:start] ) + g[:start].length, html.length]
49
+ tmp = tmp[ 0, tmp.index( g[:end] ) ]
50
+ end
51
+ return svg
52
+ end
53
+
54
+
55
+ def values_validation( dataset: nil, x: nil, y: nil, options: nil, allow_list: nil, silent: false )
56
+ def check_dataset( dataset, messages, errors )
57
+ if !dataset.nil?
58
+ if dataset.class.to_s == 'Array'
59
+ if dataset[ 0 ].class.to_s == 'Hash' or dataset[ 0 ].class.to_s == 'ActiveSupport::HashWithIndifferentAccess'
60
+ if dataset[ 0 ].keys.length > 1
61
+ search = dataset[ 0 ].keys
62
+ keys = dataset
63
+ .map { | a | a.keys }
64
+ .flatten
65
+ .to_set
66
+ .to_a
67
+ if keys.eql? search
68
+ else
69
+ errors.push( messages[:dataset][ 4 ] )
70
+ end
71
+ else
72
+ errors.push( messages[:dataset][ 3 ] )
73
+ end
74
+ else
75
+ errors.push( messages[:dataset][ 2 ] )
76
+ end
77
+ else
78
+ errors.push( messages[:dataset][ 1 ] )
79
+ end
80
+ else
81
+ errors.push( messages[:dataset][ 0 ] )
82
+ end
83
+ return errors
84
+ end
85
+
86
+
87
+ def check_x( x, messages, errors )
88
+ if !x.nil?
89
+ if x.class.to_s == 'String'
90
+ else
91
+ errors.push( messages[:x][ 1 ] )
92
+ end
93
+ else
94
+ errors.push( messages[:x][ 0 ] )
95
+ end
96
+ return errors
97
+ end
98
+
99
+
100
+ def check_y( y, messages, errors )
101
+ if !y.nil?
102
+ if y.class.to_s == 'String' or y.class.to_s == 'Array'
103
+ else
104
+ errors.push( messages[:y][ 1 ] )
105
+ end
106
+ else
107
+ errors.push( messages[:y][ 0 ] )
108
+ end
109
+ return errors
110
+ end
111
+
112
+
113
+ def check_options( options, messages, allow_list, errors )
114
+ def str_difference( a, b )
115
+ a = a.to_s.downcase.split( '_' ).join( '' )
116
+ b = b.to_s.downcase.split( '_' ).join( '' )
117
+ longer = [ a.size, b.size ].max
118
+ same = a
119
+ .each_char
120
+ .zip( b.each_char )
121
+ .select { | a, b | a == b }
122
+ .size
123
+ ( longer - same ) / a.size.to_f
124
+ end
125
+
126
+
127
+ if !options.nil?
128
+ if options.class.to_s == 'Hash'
129
+ options.keys.each do | key |
130
+ if allow_list.include?( key.to_s )
131
+ else
132
+ tmp = messages[:options][ 2 ][ 0 ]
133
+ tmp = tmp.gsub( '<--key-->', key.to_s)
134
+
135
+ nearest = allow_list
136
+ .map { | word | { score: str_difference( key, word ), word: word } }
137
+ .min_by { | item | item[:score] }
138
+ tmp = tmp.gsub( '<--similar-->', nearest[:word] )
139
+ errors.push( [ tmp, messages[:options][ 2 ][ 1 ] ] )
140
+ end
141
+ end
142
+ else
143
+ errors.push( messages[:options][ 1 ] )
144
+ end
145
+ else
146
+ errors.push( messages[:options][ 0 ] )
147
+ end
148
+
149
+ return errors
150
+ end
151
+
152
+
153
+ def check_silent( silent, messages, errors )
154
+ value = silent.class.to_s
155
+ if value == 'FalseClass' or value == 'TrueClass'
156
+ else
157
+ errors.push( messages[:silent][ 0 ] )
158
+ end
159
+ return errors
160
+ end
161
+
162
+
163
+ messages = {
164
+ dataset: [
165
+ [ "dataset:\t is empty. Expect: \"Array\" [{},{}]", :d0 ],
166
+ [ "dataset:\t is not class \"Array\"", :d1 ],
167
+ [ "dataset:\t is not class \"Hash\"", :d2 ],
168
+ [ "dataset:\t \"Hash\" has less then 2 keys", :d3 ],
169
+ [ "dataset:\t have diffrent keys", :d4 ]
170
+ ],
171
+ x: [
172
+ [ "x:\t\t is empty. Expect: \"String\"", :x0 ],
173
+ [ "x:\t\t is not Class \"String\"", :x1 ]
174
+ ],
175
+ y: [
176
+ [ "y:\t\t is empty. Expect: \"String\"", :y0 ],
177
+ [ "y:\t\t is not Class \"String\"", :y1 ]
178
+ ],
179
+ options: [
180
+ [ "options:\t is empty. Expect: \"Hash\"", :p0 ],
181
+ [ "options:\t is not Class \"Hash\"", :p1 ],
182
+ [ "options:\t key: \"<--key-->\" is not a valid parameter, did you mean: \"<--similar-->\"? For more Information visit: https://docs.statosio.com/options/<--similar-->", :p2 ]
183
+ ],
184
+ silent: [
185
+ [ "silent:\t is not Class \"Hash\"", :s0 ]
186
+ ]
187
+ }
188
+
189
+ errors = []
190
+ errors = check_dataset( dataset, messages, errors )
191
+ errors = check_y( y, messages, errors )
192
+ errors = check_x( x, messages, errors )
193
+ errors = check_options( options, messages, allow_list, errors )
194
+ errors = check_silent( silent, messages, errors )
195
+
196
+ if silent == false
197
+ if errors.length != 0
198
+ puts errors.length.to_s + ' Errors found: '
199
+ errors.each { | error | puts( ' - ' + error[ 0 ] ) }
200
+ end
201
+ end
202
+
203
+ return errors.length == 0
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statosio
4
+ VERSION = "0.3.4"
5
+ end
data/statosio.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/statosio/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "statosio"
7
+ spec.version = Statosio::VERSION
8
+ spec.authors = ["a6b8"]
9
+ spec.email = ["hello13plus4.com"]
10
+
11
+ spec.summary = "Statosio generate charts in a .svg format."
12
+ spec.description = "Statosio generate charts in a .svg format. Works with prawn-svg to generate .pdf documents."
13
+ spec.homepage = "https://d3.statoiso.com"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://d3.statoiso.com"
21
+ spec.metadata["changelog_uri"] = "https://d3.statoiso.com"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+
29
+
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ spec.add_dependency "json", "~> 2.5.1"
36
+ spec.add_dependency "puppeteer-ruby", "~> 0.32.3"
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statosio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.4
5
+ platform: ruby
6
+ authors:
7
+ - a6b8
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: puppeteer-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.32.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.32.3
41
+ description: Statosio generate charts in a .svg format. Works with prawn-svg to generate
42
+ .pdf documents.
43
+ email:
44
+ - hello13plus4.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".ipynb_checkpoints/[Ruby] statosio to svg-checkpoint.ipynb"
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - "[Ruby] statosio to svg.ipynb"
59
+ - assets/statosio.rb.png
60
+ - bin/console
61
+ - bin/setup
62
+ - jupyter/0-templates/d3.js
63
+ - jupyter/0-templates/dataset.json
64
+ - jupyter/0-templates/html.txt
65
+ - jupyter/0-templates/ruby.txt
66
+ - jupyter/0-templates/statosio.js
67
+ - jupyter/1-test/boilerplate-test.html
68
+ - jupyter/1-test/module-test-generate-svg.pdf
69
+ - jupyter/1-test/module-test-generate.pdf
70
+ - jupyter/1-test/module-test-html.html
71
+ - jupyter/2-examples/0-example1.svg
72
+ - jupyter/2-examples/1-example2.pdf
73
+ - lib/boilerplate.rb
74
+ - lib/statosio.rb
75
+ - lib/statosio/version.rb
76
+ - statosio.gemspec
77
+ homepage: https://d3.statoiso.com
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ homepage_uri: https://d3.statoiso.com
83
+ source_code_uri: https://d3.statoiso.com
84
+ changelog_uri: https://d3.statoiso.com
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.4.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.2.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Statosio generate charts in a .svg format.
104
+ test_files: []