Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/parser.rb
Instance Method Summary collapse
- #attribute_type(attribute) ⇒ Object
- #create_file(filename, content) ⇒ Object
- #fetch! ⇒ Object
- #generate_attributes_literals(json) ⇒ Object
- #get_array_attribute_literal(type) ⇒ Object
- #get_array_mapping_literal(type) ⇒ Object
- #get_attribute_literal_prefix ⇒ Object
-
#initialize(path, realm = false) ⇒ Parser
constructor
A new instance of Parser.
- #load_from_config(path) ⇒ Object
- #parse! ⇒ Object
-
#parseForMoya! ⇒ Object
parser for moya model mapper.
Constructor Details
#initialize(path, realm = false) ⇒ Parser
Returns a new instance of Parser.
25 26 27 28 29 30 |
# File 'lib/parser.rb', line 25 def initialize path, realm=false json = File.read(path) @json = JSON.parse(json) @realm = realm @parsed = {} end |
Instance Method Details
#attribute_type(attribute) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/parser.rb', line 53 def attribute_type attribute result = "" if attribute.is_a? String result = "String" elsif attribute.is_a? Integer result = "Int" elsif attribute.is_a? Float result = "Double" elsif !!attribute == attribute result = "Bool" end result end |
#create_file(filename, content) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/parser.rb', line 67 def create_file filename, content _filename = filename + ".swift" File.open(_filename, "w") do |file| file.write content puts "created file #{_filename}" # puts content end # end end |
#fetch! ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/parser.rb', line 44 def fetch! puts 'fetching' json = RestClient.get(@url, headers = @headers) puts 'fetched' @json = JSON.parse(json) self.parse! end |
#generate_attributes_literals(json) ⇒ Object
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 |
# File 'lib/parser.rb', line 89 def generate_attributes_literals json swiftClassAttributes = [] swiftClass = "" # take all hash if json.is_a? Hash # loop on each key value pair json.each do |key, value| # if value is not array or hash it is a attribute if !(value.is_a? Array) && !(value.is_a? Hash) attribute = Attribute.new(key, "#{attribute_type value}") swiftClassAttributes.push(attribute) elsif value.is_a? Hash newSwiftClass = generate_attributes_literals value @parsed.store(key.capitalize.camelize, newSwiftClass) attribute = Attribute.new(key, "#{key}") swiftClassAttributes.push(attribute) elsif value.is_a? Array if value.first.is_a? Hash newSwiftClass = generate_attributes_literals value.first @parsed.store(key.capitalize.camelize, newSwiftClass) attribute = Attribute.new(key, "#{key}", true) swiftClassAttributes.push(attribute) else attribute = Attribute.new(key, "#{attribute_type value.first}", true) swiftClassAttributes.push(attribute) end end end elsif json.is_a? Array if json.first.is_a? Hash newSwiftClass = generate_attributes_literals json.first @parsed.store("TOPLEVELCONTAINER", newSwiftClass) end end # swiftClass swiftClassAttributes end |
#get_array_attribute_literal(type) ⇒ Object
81 82 83 |
# File 'lib/parser.rb', line 81 def get_array_attribute_literal type @realm ? " = List<#{type.capitalize.camelize}>()" : ": [#{type.capitalize}] = []" end |
#get_array_mapping_literal(type) ⇒ Object
85 86 87 |
# File 'lib/parser.rb', line 85 def get_array_mapping_literal type @realm ? "(map[\"#{type}\"], ListTransform<#{type.capitalize.camelize}>())" : "map[\"#{type}\"]" end |
#get_attribute_literal_prefix ⇒ Object
77 78 79 |
# File 'lib/parser.rb', line 77 def get_attribute_literal_prefix @realm ? "@objc dynamic " : "" end |
#load_from_config(path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/parser.rb', line 32 def load_from_config path config = File.read(path) config_json = JSON.parse(config) @url = config_json["url"] @headers = config_json["headers"] if @url =~ URI::regexp self.fetch! else puts "ERROR: URL in config.json is not valid. please add valid url." end end |
#parse! ⇒ Object
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 |
# File 'lib/parser.rb', line 127 def parse! swiftClass = generate_attributes_literals @json if @json.is_a? Hash @parsed.store("Container", swiftClass) end @parsed.each do |class_name, attributes| realm_funcs = <<-REALMCLASS \t\trequired convenience init?(map: Map) { \t\t\t\tself.init() \t\t} \t\toverride class func primaryKey() -> String? { \t\t\t\t// change according to your requirement \t\t\t\treturn "id" \t\t} REALMCLASS non_realm_funcs = <<-DEFAULT \t\trequired init?(map: Map) { \t\t} DEFAULT # (map["friends"], ListTransform<User>()) attribute_literals = "" mapping_literals = "" attributes.each do |attribute| attribute_literal = "" mapping_literal = "" if attribute.is_array attribute_literal = "\t\tvar #{attribute.name.camelize}#{get_array_attribute_literal attribute.type}\n" mapping_literal = "\t\t\t\t#{attribute.name.camelize} <- #{get_array_mapping_literal attribute.type}\n" else default_value = attribute.default_value default_value = default_value.nil? ? "?" : " = #{default_value}" attribute_literal = "\t\t#{get_attribute_literal_prefix}var #{attribute.name.camelize}: #{attribute.type.capitalize}#{default_value}\n" mapping_literal = "\t\t\t\t#{attribute.name.camelize} <- map[\"#{attribute.name}\"]\n" end attribute_literals += attribute_literal mapping_literals += mapping_literal end class_model = <<-CLASS // // Created with veda-apps. // https://rubygems.org/gems/veda-apps // import Foundation import ObjectMapper #{@realm? "import RealmSwift\nimport ObjectMapper_Realm" : ""} class #{class_name}:#{@realm? " Object," : ""} Mappable { #{attribute_literals} #{@realm? realm_funcs : non_realm_funcs} \t\tfunc mapping(map: Map) { #{mapping_literals} \t\t} }\n CLASS create_file class_name, class_model end end |
#parseForMoya! ⇒ Object
parser for moya model mapper
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/parser.rb', line 191 def parseForMoya! swiftClass = generate_attributes_literals @json if @json.is_a? Hash @parsed.store("Container", swiftClass) end all_content = "" @parsed.each do |class_name, attributes| attribute_literals = "" mapping_literals = "" key_literals = "" attributes.each do |attribute| attribute_literal = "" mapping_literal = "" key_literal = "" if attribute.is_array attribute_literal = "\tlet #{attribute.name.camelize}: [#{attribute.type.capitalize.camelize}]\n" else attribute_literal = "\tlet #{attribute.name.camelize}: #{attribute.type.capitalize.camelize}\n" end mapping_literal = "\t\t#{attribute.name.camelize} = try map.from(Key.#{attribute.name.camelize})\n" key_literal = "\t\tstatic let #{attribute.name.camelize} = \"#{attribute.name}\"\n" attribute_literals += attribute_literal mapping_literals += mapping_literal key_literals += key_literal end class_model = <<-CLASS // // Created with veda-apps. // https://rubygems.org/gems/veda-apps // import Foundation import Moya import Mapper struct #{class_name} { #{attribute_literals} } extension #{class_name}: Mappable { \tinit(map: Mapper) throws { #{mapping_literals} \t} \tstruct Key { #{key_literals} \t} }\n CLASS all_content += ("\n\n" + class_model) create_file class_name, class_model end create_file "ALLCONTENT", all_content end |