wschema 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0571bbc7b1887087bf9f9cb841a6b1f55fe594e5fbbc448b2fe8c50e68b2abbc
4
+ data.tar.gz: c2a856017f8e405ba2d7897780732c64bc0885799d2705d1235669eccddd4eae
5
+ SHA512:
6
+ metadata.gz: 710c2a550de64977fa556cbdccfa08cf52d068eab194c7ad3c968fc185b9f7813db8e195732218e6ee8e723af2ba9d111d87beac4ccdf9e64474cff451e7e7bc
7
+ data.tar.gz: 6c03d94882777e2820b9a0f6d85f2a15abde0691327bc46bb9a34b334c5058e2d4cc49ab35c6114a2f09ca42711c713733384d60b76773f3f4aa4839106c5a9e
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc 'Run tests'
8
+ task default: :test
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require '././lib/wschema'
4
+ puts Wschema.format_schema(ARGV[0])
@@ -0,0 +1,110 @@
1
+ require 'json'
2
+ # class Wschema
3
+ class Wschema
4
+ def self.labelize(options)
5
+ options
6
+ .tr('_'.freeze, ' '.freeze)
7
+ .strip
8
+ .gsub(/([a-z\d]*)/i) { |s| s.downcase }
9
+ .gsub(/\A\w/) { |s| s.upcase }
10
+ .gsub(/ id\z/, ' ID'.freeze)
11
+ end
12
+
13
+ def self.run_format_schema(schema)
14
+ schema&.each_with_object([]) do |field, array|
15
+ field_name = field['name']
16
+ if (props = field['properties'])
17
+ field['properties'] = run_format_schema(props)
18
+ end
19
+
20
+ # removing type for type=string
21
+ if field['type'] == 'string' || field['type'] == :string
22
+ field.delete('type')
23
+ end
24
+
25
+ # removing label for field_name.labelize == field[:label]
26
+ if field['label'] &&
27
+ (field['label'] == 'id' || labelize(field_name) == field['label'])
28
+ field.delete('label')
29
+ end
30
+
31
+ case field_name
32
+ when 'os'
33
+ field['label'] = 'OS'
34
+ when 'guid'
35
+ field['label'] = 'GUID'
36
+ when 'uuid'
37
+ field['label'] = 'UUID'
38
+ when 'uri'
39
+ field['label'] = 'URI'
40
+ end
41
+
42
+ # removing control_type for control_type=text
43
+ if field['control_type'] == 'text' || field['control_type'] == :text
44
+ field.delete('control_type')
45
+ end
46
+
47
+ # changing control_type for type=boolean || integer
48
+ if field['type'] == 'boolean' || field['type'] == :boolean
49
+ field['control_type'] = 'checkbox'
50
+ elsif field['type'] == 'integer' || field['type'] == :integer
51
+ field['control_type'] = 'integer'
52
+ end
53
+
54
+ field_info = [field['name'].downcase,
55
+ field['label']&.downcase,
56
+ field['hint']&.downcase].compact
57
+
58
+ if field_info.include?('email')
59
+ field['control_type'] = 'email'
60
+ elsif field_info.include?('phone')
61
+ field['control_type'] = 'phone'
62
+ elsif field_info.include?('uri')
63
+ field['control_type'] = 'url'
64
+ elsif field_info.include?('url')
65
+ field['control_type'] = 'url'
66
+ end
67
+
68
+ array << {
69
+ name: field['name'],
70
+ label: field['label'],
71
+ hint: field['hint'],
72
+ default: field['default'],
73
+ sticky: field['sticky'],
74
+ optional: field['optional'],
75
+ extends_schema: field['extends_schema'],
76
+ schema_neutral: field['schema_neutral'],
77
+ add_field_label: field['add_field_label'],
78
+ sample_data_type: field['sample_data_type'],
79
+ custom_properties: field['custom_properties'],
80
+ control_type: field['control_type'],
81
+ empty_list_title: field['empty_list_title'],
82
+ empty_list_text: field['empty_list_text'],
83
+ item_label: field['item_label'],
84
+ pick_list: field['pick_list'],
85
+ toggle_hint: field['toggle_hint'],
86
+ # TODO: handle toggle_fields like properties
87
+ toggle_field: field['toggle_field'],
88
+ render_input: field['render_input'],
89
+ parse_output: field['parse_output'],
90
+ type: field['type'],
91
+ of: field['of'],
92
+ properties: field['properties']
93
+ }.compact
94
+ end
95
+ end
96
+
97
+ def self.format_schema(workato_schema)
98
+ {
99
+ formatted_schema: run_format_schema(JSON.parse(workato_schema))
100
+ .to_json
101
+ }
102
+ end
103
+ end
104
+
105
+ # p Wschema.format_schema('[ {
106
+ # "control_type": "text",
107
+ # "label": "Name",
108
+ # "type": "string",
109
+ # "name": "name"
110
+ # }]')
@@ -0,0 +1,13 @@
1
+ require 'test/unit'
2
+ require 'wschema'
3
+
4
+ class WschemaTest < Test::Unit::TestCase
5
+ def test_format_schema
6
+ assert_equal %q({:formatted_schema=>"[{\"name\":\"name\"}]"}).strip, Wschema.format_schema(%q([ {
7
+ "control_type": "text",
8
+ "label": "Name",
9
+ "type": "string",
10
+ "name": "name"
11
+ }])).to_s.strip
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wschema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sharat Hegde
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-26 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: 1.8.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Workato schema gem. Formats Workato schema to remove defaults and sanitize
42
+ schema
43
+ email: sharat.developer@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Rakefile
49
+ - bin/wschema
50
+ - lib/wschema.rb
51
+ - test/test_wschema.rb
52
+ homepage: http://rubygems.org/gems/wschema
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Wschema!
74
+ test_files:
75
+ - test/test_wschema.rb