Class: Statosio::Generate
- Inherits:
-
Boilerplate
- Object
- Boilerplate
- Statosio::Generate
- Defined in:
- lib/statosio.rb
Instance Method Summary collapse
- #check_dataset(dataset, messages, errors) ⇒ Object
- #check_options(options, messages, allow_list, errors) ⇒ Object
- #check_silent(silent, messages, errors) ⇒ Object
- #check_x(x, messages, errors) ⇒ Object
- #check_y(y, messages, errors) ⇒ Object
- #get_options_allow_list ⇒ Object
- #render_prepare(values) ⇒ Object
- #render_svg(values) ⇒ Object
- #str_difference(a, b) ⇒ Object
- #svg(dataset: nil, x: nil, y: nil, options: nil, silent: false) ⇒ Object
- #values_validation(dataset: nil, x: nil, y: nil, options: nil, allow_list: nil, silent: false) ⇒ Object
Methods inherited from Boilerplate
#get_boilerplate, #get_boilerplate_raw, #get_markers, #initialize, #set_boilerplate, #set_markers_value, #set_options_allow_list
Constructor Details
This class inherits a constructor from Boilerplate
Instance Method Details
#check_dataset(dataset, messages, errors) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/statosio.rb', line 56 def check_dataset( dataset, , errors ) if !dataset.nil? if dataset.class.to_s == 'Array' if dataset[ 0 ].class.to_s == 'Hash' or dataset[ 0 ].class.to_s == 'ActiveSupport::HashWithIndifferentAccess' if dataset[ 0 ].keys.length > 1 search = dataset[ 0 ].keys keys = dataset .map { | a | a.keys } .flatten .to_set .to_a if keys.eql? search else errors.push( [:dataset][ 4 ] ) end else errors.push( [:dataset][ 3 ] ) end else errors.push( [:dataset][ 2 ] ) end else errors.push( [:dataset][ 1 ] ) end else errors.push( [:dataset][ 0 ] ) end return errors end |
#check_options(options, messages, allow_list, errors) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/statosio.rb', line 113 def ( , , allow_list, errors ) def str_difference( a, b ) a = a.to_s.downcase.split( '_' ).join( '' ) b = b.to_s.downcase.split( '_' ).join( '' ) longer = [ a.size, b.size ].max same = a .each_char .zip( b.each_char ) .select { | a, b | a == b } .size ( longer - same ) / a.size.to_f end if !.nil? if .class.to_s == 'Hash' .keys.each do | key | if allow_list.include?( key.to_s ) else tmp = [:options][ 2 ][ 0 ] tmp = tmp.gsub( '<--key-->', key.to_s) nearest = allow_list .map { | word | { score: str_difference( key, word ), word: word } } .min_by { | item | item[:score] } tmp = tmp.gsub( '<--similar-->', nearest[:word] ) errors.push( [ tmp, [:options][ 2 ][ 1 ] ] ) end end else errors.push( [:options][ 1 ] ) end else errors.push( [:options][ 0 ] ) end return errors end |
#check_silent(silent, messages, errors) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/statosio.rb', line 153 def check_silent( silent, , errors ) value = silent.class.to_s if value == 'FalseClass' or value == 'TrueClass' else errors.push( [:silent][ 0 ] ) end return errors end |
#check_x(x, messages, errors) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/statosio.rb', line 87 def check_x( x, , errors ) if !x.nil? if x.class.to_s == 'String' else errors.push( [:x][ 1 ] ) end else errors.push( [:x][ 0 ] ) end return errors end |
#check_y(y, messages, errors) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/statosio.rb', line 100 def check_y( y, , errors ) if !y.nil? if y.class.to_s == 'String' or y.class.to_s == 'Array' else errors.push( [:y][ 1 ] ) end else errors.push( [:y][ 0 ] ) end return errors end |
#get_options_allow_list ⇒ Object
24 25 26 |
# File 'lib/statosio.rb', line 24 def @options_allow_list end |
#render_prepare(values) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/statosio.rb', line 29 def render_prepare( values ) values[:y].class.to_s == 'String' ? values[:y] = [ values[:y] ] : '' self.set_markers_value( values ) self.set_boilerplate html = self.get_boilerplate end |
#render_svg(values) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/statosio.rb', line 37 def render_svg( values ) html = self.render_prepare( values ) g = {} id = 'd3_statosio' g[:start] = '<div id="' + id + '">' g[:end] = '</div>' svg = Puppeteer.launch do | browser | page = browser.pages.last || browser.new_page page.set_content( html ) html = page.evaluate( 'document.body.innerHTML' ) tmp = html[ html.index( g[:start] ) + g[:start].length, html.length] tmp = tmp[ 0, tmp.index( g[:end] ) ] end return svg end |
#str_difference(a, b) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/statosio.rb', line 114 def str_difference( a, b ) a = a.to_s.downcase.split( '_' ).join( '' ) b = b.to_s.downcase.split( '_' ).join( '' ) longer = [ a.size, b.size ].max same = a .each_char .zip( b.each_char ) .select { | a, b | a == b } .size ( longer - same ) / a.size.to_f end |
#svg(dataset: nil, x: nil, y: nil, options: nil, silent: false) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/statosio.rb', line 11 def svg( dataset: nil, x: nil, y: nil, options: nil, silent: false ) valid = values_validation( dataset: dataset, x: x, y: y, options: , allow_list: @options_allow_list, silent: silent ) if valid values = {} values[:dataset] = dataset values[:x] = x values[:y] = y values[:options] = render_svg( values ) end end |
#values_validation(dataset: nil, x: nil, y: nil, options: nil, allow_list: nil, silent: false) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/statosio.rb', line 55 def values_validation( dataset: nil, x: nil, y: nil, options: nil, allow_list: nil, silent: false ) def check_dataset( dataset, , errors ) if !dataset.nil? if dataset.class.to_s == 'Array' if dataset[ 0 ].class.to_s == 'Hash' or dataset[ 0 ].class.to_s == 'ActiveSupport::HashWithIndifferentAccess' if dataset[ 0 ].keys.length > 1 search = dataset[ 0 ].keys keys = dataset .map { | a | a.keys } .flatten .to_set .to_a if keys.eql? search else errors.push( [:dataset][ 4 ] ) end else errors.push( [:dataset][ 3 ] ) end else errors.push( [:dataset][ 2 ] ) end else errors.push( [:dataset][ 1 ] ) end else errors.push( [:dataset][ 0 ] ) end return errors end def check_x( x, , errors ) if !x.nil? if x.class.to_s == 'String' else errors.push( [:x][ 1 ] ) end else errors.push( [:x][ 0 ] ) end return errors end def check_y( y, , errors ) if !y.nil? if y.class.to_s == 'String' or y.class.to_s == 'Array' else errors.push( [:y][ 1 ] ) end else errors.push( [:y][ 0 ] ) end return errors end def ( , , allow_list, errors ) def str_difference( a, b ) a = a.to_s.downcase.split( '_' ).join( '' ) b = b.to_s.downcase.split( '_' ).join( '' ) longer = [ a.size, b.size ].max same = a .each_char .zip( b.each_char ) .select { | a, b | a == b } .size ( longer - same ) / a.size.to_f end if !.nil? if .class.to_s == 'Hash' .keys.each do | key | if allow_list.include?( key.to_s ) else tmp = [:options][ 2 ][ 0 ] tmp = tmp.gsub( '<--key-->', key.to_s) nearest = allow_list .map { | word | { score: str_difference( key, word ), word: word } } .min_by { | item | item[:score] } tmp = tmp.gsub( '<--similar-->', nearest[:word] ) errors.push( [ tmp, [:options][ 2 ][ 1 ] ] ) end end else errors.push( [:options][ 1 ] ) end else errors.push( [:options][ 0 ] ) end return errors end def check_silent( silent, , errors ) value = silent.class.to_s if value == 'FalseClass' or value == 'TrueClass' else errors.push( [:silent][ 0 ] ) end return errors end = { dataset: [ [ "dataset:\t is empty. Expect: \"Array\" [{},{}]", :d0 ], [ "dataset:\t is not class \"Array\"", :d1 ], [ "dataset:\t is not class \"Hash\"", :d2 ], [ "dataset:\t \"Hash\" has less then 2 keys", :d3 ], [ "dataset:\t have diffrent keys", :d4 ] ], x: [ [ "x:\t\t is empty. Expect: \"String\"", :x0 ], [ "x:\t\t is not Class \"String\"", :x1 ] ], y: [ [ "y:\t\t is empty. Expect: \"String\"", :y0 ], [ "y:\t\t is not Class \"String\"", :y1 ] ], options: [ [ "options:\t is empty. Expect: \"Hash\"", :p0 ], [ "options:\t is not Class \"Hash\"", :p1 ], [ "options:\t key: \"<--key-->\" is not a valid parameter, did you mean: \"<--similar-->\"? For more Information visit: https://docs.statosio.com/options/<--similar-->", :p2 ] ], silent: [ [ "silent:\t is not Class \"Hash\"", :s0 ] ] } errors = [] errors = check_dataset( dataset, , errors ) errors = check_y( y, , errors ) errors = check_x( x, , errors ) errors = ( , , allow_list, errors ) errors = check_silent( silent, , errors ) if silent == false if errors.length != 0 puts errors.length.to_s + ' Errors found: ' errors.each { | error | puts( ' - ' + error[ 0 ] ) } end end return errors.length == 0 end |