Module: Bskyrb::LexiconParser
- Defined in:
- lib/bskyrb/codegen.rb
Constant Summary collapse
- @@active_namespace =
""
Class Method Summary collapse
- .basic_class_definitions(classes_hash) ⇒ Object
- .basic_template ⇒ Object
- .build_class_hash_from_schema(json_schema, output = {}) ⇒ Object
- .build_classes_hash_from_lexicon_defs(lexicon_file) ⇒ Object
- .build_queries_and_procedures_from_lexicon(lexicon_file) ⇒ Object
- .def_is_procedure(defn) ⇒ Object
- .def_is_query(defn) ⇒ Object
- .def_is_subscription(defn) ⇒ Object
- .input_output_class_definitions(classes_hash) ⇒ Object
- .input_output_template ⇒ Object
- .ref_to_class_str(ref, ns = @@active_namespace) ⇒ Object
Class Method Details
.basic_class_definitions(classes_hash) ⇒ Object
124 125 126 127 128 |
# File 'lib/bskyrb/codegen.rb', line 124 def self.basic_class_definitions(classes_hash) classes_hash.map do |klass_str, properties| ERB.new(basic_template).result_with_hash({klass_str: klass_str, properties: properties}) end end |
.basic_template ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bskyrb/codegen.rb', line 103 def self.basic_template %( module <%= klass_str.split("::").first %> class <%= klass_str.split("::").last %> <% properties.each do |key, value| %> attr_accessor :<%= key %> <% end %> def self.from_hash(hash) # httparty-returned string-keyed hash instance = new <% properties.each do |key, value| %> instance.send("<%= key %>=", hash["<%= key %>"]) <% end %> instance end end end ) end |
.build_class_hash_from_schema(json_schema, output = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bskyrb/codegen.rb', line 74 def self.build_class_hash_from_schema(json_schema, output = {}) return if json_schema.nil? return if json_schema.has_key? "main" to_process = json_schema["properties"] || json_schema to_process.each_pair do |key, value| if key == "type" || !value.is_a?(Hash) # || value["type"] == "query" || key == "main" next end output[key] = if value["type"] == "object" build_class_hash_from_schema(value, {}) elsif value["type"] == "array" [build_class_hash_from_schema(value, {}).values.first] elsif value["type"] == "ref" ref_to_class_str(value["ref"]) else -1 end end output end |
.build_classes_hash_from_lexicon_defs(lexicon_file) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bskyrb/codegen.rb', line 27 def self.build_classes_hash_from_lexicon_defs(lexicon_file) parsed = File.open(lexicon_file) { |f| JSON.parse(f.read) } output = {} # return if def_is_query(parsed) || def_is_procedure(parsed) || def_is_subscription(parsed) parsed["defs"].each do |k, v| # map {|v| build_class_hash_from_schema(v)} next if v["type"] == "main" # skip queries, procedures, etc. @@active_namespace = parsed["id"].split(".").map(&:capitalize).join("") next if k == "main" output[ref_to_class_str(k)] = if v["type"] == "record" build_class_hash_from_schema(v["record"]["properties"]) else build_class_hash_from_schema(v) end end output end |
.build_queries_and_procedures_from_lexicon(lexicon_file) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bskyrb/codegen.rb', line 44 def self.build_queries_and_procedures_from_lexicon(lexicon_file) parsed = File.open(lexicon_file) { |f| JSON.parse(f.read) } output = {} return unless def_is_query(parsed) || def_is_procedure(parsed) parsed["defs"].each do |k, v| # map {|v| build_class_hash_from_schema(v)} next if k != "main" || v["type"] == "record" # skip basic defs next if v["type"] == "subscription" # some day @@active_namespace = parsed["id"].split(".").map(&:capitalize).join("") class_name = ref_to_class_str(parsed["id"].split(".").last) built_def = {} output[class_name] = {} if v["type"] == "query" built_def["input"] = build_class_hash_from_schema(v["parameters"]) if v["output"] && v["output"]["schema"] built_def["output"] = build_class_hash_from_schema(v["output"]["schema"]) end end if v["type"] == "procedure" if v["input"] built_def["input"] = build_class_hash_from_schema(v["input"]["schema"]) end if v["output"] && v["output"]["schema"] built_def["output"] = build_class_hash_from_schema(v["output"]["schema"]) end end output[class_name] = built_def unless built_def.empty? end output end |
.def_is_procedure(defn) ⇒ Object
21 22 23 24 25 |
# File 'lib/bskyrb/codegen.rb', line 21 def self.def_is_procedure(defn) defn["defs"].has_key?("main") && defn["defs"]["main"].has_key?("type") && defn["defs"]["main"]["type"] == "procedure" end |
.def_is_query(defn) ⇒ Object
9 10 11 12 13 |
# File 'lib/bskyrb/codegen.rb', line 9 def self.def_is_query(defn) defn["defs"].has_key?("main") && defn["defs"]["main"].has_key?("type") && defn["defs"]["main"]["type"] == "query" end |
.def_is_subscription(defn) ⇒ Object
15 16 17 18 19 |
# File 'lib/bskyrb/codegen.rb', line 15 def self.def_is_subscription(defn) defn["defs"].has_key?("main") && defn["defs"]["main"].has_key?("type") && defn["defs"]["main"]["type"] == "subscription" end |
.input_output_class_definitions(classes_hash) ⇒ Object
164 165 166 167 168 |
# File 'lib/bskyrb/codegen.rb', line 164 def self.input_output_class_definitions(classes_hash) classes_hash.map do |klass_str, properties| ERB.new(input_output_template).result_with_hash({klass_str: klass_str, properties: properties}) end end |
.input_output_template ⇒ Object
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 |
# File 'lib/bskyrb/codegen.rb', line 130 def self.input_output_template %( module <%= klass_str.split("::").first %> module <%= klass_str.split("::").last %> <% ["input", "output"].each do |top_level_key| %> <% next unless properties.has_key?(top_level_key) && !properties[top_level_key].nil? %> class <%= top_level_key.capitalize %> <% properties[top_level_key].each do |key, value| %> attr_accessor :<%= key %> <% end %> def self.from_hash(hash) # httparty-returned string-keyed hash instance = new <% properties[top_level_key].each do |key, value| %> instance.send("<%= key %>=", hash["<%= key %>"]) <% end %> instance end def to_h { <% properties[top_level_key].each do |key, value| %> "<%= key %>" => <%= key %>, <% end %> } end end <% end %> end end ) end |
.ref_to_class_str(ref, ns = @@active_namespace) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/bskyrb/codegen.rb', line 96 def self.ref_to_class_str(ref, ns = @@active_namespace) klass_str_lower = ref.split("#")[-1] klass_str = klass_str_lower[0].capitalize + klass_str_lower.slice(1..) ns += "::" if ns "#{ns}#{klass_str}" end |