Class: FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/PhariDocGen/FileHandler.rb

Instance Method Summary collapse

Instance Method Details

#dataValue(argument) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'lib/PhariDocGen/FileHandler.rb', line 312

def dataValue(argument)
    if argument.include? "\s"
        name = argument.slice(0, argument.index("\s"))
        type = argument.slice(argument.index("\s") + 1, argument.size - 1)
        data = MethodParam.new(name, type)
        # data.printSelf
        return data
    end
    'nil'
end

#decodeArgument(line) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/PhariDocGen/FileHandler.rb', line 300

def decodeArgument(line)
    args = []
    initialIndex = line.index('@') + 1
    finalIndex = line.index("\s", line.index('@'))
    lenght = finalIndex - initialIndex
    keyword = line.slice(initialIndex, lenght)
    argument = line.slice(finalIndex + 1, line.size - finalIndex)
    args << keyword
    args << argument
    args
end

#decodeDescription(line) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/PhariDocGen/FileHandler.rb', line 323

def decodeDescription(line)
    initialIndex = line.index('*') + 3
    lenght = line.size - initialIndex
    description = line.slice(initialIndex, lenght)
    # puts "#{description}"
    description
end

#hasBetween?(line, stringA, stringB) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
# File 'lib/PhariDocGen/FileHandler.rb', line 102

def hasBetween?(line, stringA, stringB)
    if line.include?(stringA)
        initial = line.index(stringA)
        len = line.length - initial
        lineSlice = line.slice(initial, len)
        return true if lineSlice.include?(stringB)
    end
    false
end

#hasDescription?(line) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
291
292
293
294
295
296
297
298
# File 'lib/PhariDocGen/FileHandler.rb', line 288

def hasDescription?(line)
    if (line.include? '#') && (line.include? '**')
        if line.index('#') < line.index('**')
            true
        else
            false
        end
    else
        false
    end
end

#hasKeyword?(line) ⇒ Boolean

Métodos de decodificação da leitura

Returns:

  • (Boolean)


276
277
278
279
280
281
282
283
284
285
286
# File 'lib/PhariDocGen/FileHandler.rb', line 276

def hasKeyword?(line)
    if (line.include? '#') && (line.include? '@')
        if line.index('#') < line.index('@')
            true
        else
            false
        end
    else
        false
    end
end

#higlightText(line) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/PhariDocGen/FileHandler.rb', line 112

def higlightText(line)
    initialIndex = line.index('**') + 2
    finalIndex = line.index('**', initialIndex)
    initialString = line.slice(0, initialIndex - 2)
    higlightedString = line.slice(initialIndex, finalIndex - initialIndex)
    finalString = line.slice(finalIndex + 2, line.length - finalIndex + 2)
    line = initialString + '<b>' + higlightedString + '</b>' + finalString
    # puts line
    line
end

#italicText(line) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/PhariDocGen/FileHandler.rb', line 123

def italicText(line)
    initialIndex = line.index('*') + 1
    finalIndex = line.index('*', initialIndex)
    initialString = line.slice(0, initialIndex - 1)
    higlightedString = line.slice(initialIndex, finalIndex - initialIndex)
    finalString = line.slice(finalIndex + 1, line.length - finalIndex + 1)
    line = initialString + '<i>' + higlightedString + '</i>' + finalString
    # puts line
    line
end

#linkText(line) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/PhariDocGen/FileHandler.rb', line 134

def linkText(line)
    if line.include?(' [') && line.include?('](')
        initialContentIndex = line.index('[') + 1
        finalContentIndex = line.index(']', initialContentIndex)
        initialLinkIndex = line.index('](') + 2
        finalLinkIndex = line.index(')', initialLinkIndex)
        initialString = line.slice(0, initialContentIndex - 1)
        contentString = line.slice(initialContentIndex, finalContentIndex - initialContentIndex)
        linkString = line.slice(initialLinkIndex, finalLinkIndex - initialLinkIndex)
        finalString = line.slice(finalLinkIndex + 1, line.length - finalLinkIndex + 1)
        line = initialString + '<a href="' + linkString + '">' + contentString + '</a>' + finalString
    end
    line
end

#packageExistis?(packageName) ⇒ Boolean

Leitura dos arquivos

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/PhariDocGen/FileHandler.rb', line 9

def packageExistis?(packageName)
    found = false
    package = ''
    Dir.glob('/**/' + packageName + '/') do |folder|
        found = true
        package = folder
        # puts 'done'
    end
    packagePath = package.to_s
    # puts packagePath
    if found
        return packagePath
    else
        puts 'Aborting: package not found'
        exit
    end
end

#readFiles(project) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/PhariDocGen/FileHandler.rb', line 35

def readFiles(project)
    models = []
    path = packageExistis?(project)
    Dir.glob(path + 'backend/api/models/*.rb') do |file|
        filename = file.to_s
        inputFile = File.new(filename)
        modelname = File.basename(inputFile, '.rb')
        helpername = path + 'backend/api/app/helpers/' + modelname + '_helper.rb'
        controllername = path + 'backend/api/app/controllers/' + modelname + '.rb'
        if File.exist?(helpername) && File.exist?(controllername)
            modelHelper = File.new(helpername, 'r')
            modelController = File.new(controllername, 'r')
            methods = readMethods(modelHelper)
            routes = readRoutes(modelController)
            currentModel = Modelo.new(modelname, methods, routes)
            models << currentModel
        end
    end
    models
end

#readMethods(helper) ⇒ Object



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
# File 'lib/PhariDocGen/FileHandler.rb', line 149

def readMethods(helper)
    tipo = ''
    nome = ''
    descricao = ''
    inputs = []
    outputs = []
    methods = []
    toSent = true
    arr = IO.readlines(helper)
    arr.each do |line|
        if hasKeyword?(line)
            arg = decodeArgument(line)
            keyword = arg[0]
            argument = arg[1]
            case keyword
            when 'methods'
                if nome != ''
                    methods << Metodo.new(nome, tipo, inputs, outputs, descricao)
                    inputs = []
                    outputs = []
                    toSent = false
                end
                tipo = argument
            when 'name'
                if nome != '' && toSent
                    methods << Metodo.new(nome, tipo, inputs, outputs, descricao)
                    inputs = []
                    outputs = []
                else
                    toSent = true
                end
                nome = argument
            # methods.last.printName
            # puts "#{methods.size}"
            when 'param'
                data = dataValue(argument)
                inputs << data
            when 'return'
                data = dataValue(argument)
                outputs << data
            end
        end
        descricao = decodeDescription(line) if hasDescription?(line)
    end
    methods << Metodo.new(nome, tipo, inputs, outputs, descricao)
    methods
end

#readProject(project) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/PhariDocGen/FileHandler.rb', line 27

def readProject(project)
    path = packageExistis?(project)
    projectDescription = ''
    readme = File.new(path + 'README.md')
    projectDescription = readProjectDescription(readme)
    projectDescription
end

#readProjectDescription(readme) ⇒ Object

Métodos auxiliares a leitura



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
# File 'lib/PhariDocGen/FileHandler.rb', line 57

def readProjectDescription(readme)
    isCode = false
    @projectDescription = ''
    arr = IO.readlines(readme)
    arr.each do |line|
        next if line.start_with?('# ')
        if line.start_with?('## ')
            line = '<h3>' + line.slice(3, line.length - 3)
            line += '</h3>'
            # puts line
        end
        if line.start_with?('### ')
            line = '<h4>' + line.slice(4, line.length - 4) + '</h4>'
            # puts line
        end
        if line.start_with?('##### ')
            line = '<h5>' + line.slice(6, line.length - 6) + '</h5>'
            # puts line
        end
        if line.start_with?('* ')
            line = '<li>' + line.slice(2, line.length - 2) + '</li>'
            # puts line
        end
        while hasBetween?(line, '**', '**')
            line = higlightText(line)
            # puts line
        end
        line = italicText(line) while hasBetween?(line, '*', '*')
        if line.include?('```')
            if isCode
                line = '</code><br><br>'
                isCode = false
            else
                line = '<br><br><code>'
                isCode = true
            end
        end
        while line.include?(' [') && line.include?('](')
            line = linkText(line)
        end
        @projectDescription += line
    end
    @projectDescription
end

#readRoutes(controller) ⇒ Object



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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/PhariDocGen/FileHandler.rb', line 197

def readRoutes(controller)
    tipo = ''
    requisicao = ''
    dataInput = ''
    nome = ''
    inputs = []
    outputs = []
    routes = []
    toSent = true
    arr = IO.readlines(controller)
    arr.each do |line|
        next unless hasKeyword?(line)
        arg = decodeArgument(line)
        keyword = arg[0]
        argument = arg[1]
        case keyword
        when 'route'
            if nome != ''
                routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
                inputs = []
                outputs = []
                toSent = false
            end
            tipo = argument
        when 'POST'
            if nome != '' && toSent
                routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
                inputs = []
                outputs = []
            else
                toSent = true
            end
            requisicao = keyword
            dataInput = argument
        when 'GET'
            if nome != '' && toSent
                routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
                inputs = []
                outputs = []
            else
                toSent = true
            end
            requisicao = keyword
            dataInput = argument
        when 'PUT'
            if nome != '' && toSent
                routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
                inputs = []
                outputs = []
            else
                toSent = true
            end
            requisicao = keyword
            dataInput = argument
        when 'DELETE'
            if nome != '' && toSent
                routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
                inputs = []
                outputs = []
            else
                toSent = true
            end
            requisicao = keyword
            dataInput = argument
        when 'name'
            nome = argument
        when 'param'
            data = dataValue(argument)
            inputs << data
        when 'return'
            data = dataValue(argument)
            outputs << data
        end
    end
    routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
    routes
end

#writeCSS(file) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/PhariDocGen/FileHandler.rb', line 350

def writeCSS(file)
    file.write("body{
background-color: #702794;
  }
  a{
color: #333;
  }
  li{
padding-left: 3%;
  }
  .logo{
margin: 5%;
  }
  .left{
float: left;
  }
  .right{
float: right;
  }
  .list-indexes{
font-size: 24px;
  }")
end

#writeFiles(models, project, projectDescription, path) ⇒ Object

Escrita dos arquivos



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/PhariDocGen/FileHandler.rb', line 332

def writeFiles(models, project, projectDescription, path)
    FileUtils.mkdir_p(path + 'css')
    css = File.open(path + 'css/master.css', 'w')
    writeCSS(css)
    css.close
    projectHTML = File.open(path + 'project.html', 'w')
    writeHeader(projectHTML)
    writeProjectPage(project, projectHTML, models, projectDescription)
    projectHTML.close
    FileUtils.mkdir_p(path + 'models')
    models.each do |model|
        name = model.name.downcase
        modelHTML = File.open(path + "models/#{name}.html", 'w')
        writeHeaderInDir(modelHTML)
        writeModelPage(modelHTML, model)
    end
end

#writeHeader(file) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/PhariDocGen/FileHandler.rb', line 374

def writeHeader(file)
    file.write("<!DOCTYPE html>

    <head lang='en'>
        <meta charset='UTF-8'>
        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>
        <link rel='stylesheet' href='css/master.css'>
    </head>

    <body>
        <nav class='navbar navbar-default navbar-fixed-top'>
            <div class='container-fluid'>
                <div class='navbar-header'>
                  <a href='project.html'><img class='logo' src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' height='50px' alt='Logo'></a>
                </div>
                <ul class='nav navbar-nav'>
                </ul>
            </div>
        </nav>
        <br><br><br>")
end

#writeHeaderInDir(file) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/PhariDocGen/FileHandler.rb', line 397

def writeHeaderInDir(file)
    file.write("<!DOCTYPE html>

    <head lang='en'>
        <meta charset='UTF-8'>
        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>
        <link rel='stylesheet' href='../css/master.css'>
    </head>

    <body>
        <nav class='navbar navbar-default navbar-fixed-top'>
            <div class='container-fluid'>
                <div class='navbar-header'>
                  <a href='../project.html'><img class='logo' src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' height='50px' alt='Logo'></a>
                </div>
                <ul class='nav navbar-nav'>
                </ul>
            </div>
        </nav>
        <br><br><br>")
end

#writeModelPage(file, model) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/PhariDocGen/FileHandler.rb', line 452

def writeModelPage(file, model)
    name = model.name.capitalize
    file.write("<div class='container panel panel-default'>
      <div class='row'>
        <div class='panel panel-default'>
          <div class='panel-heading'>
            <h1>PhariSchool</h1>
          </div>
          <div class='panel-body'>
            <h2>#{name} Model</h2>
          </div>
        </div>
      </div>
      <div class='row'>
        <div class='col-md-6'>
          <div class='panel panel-default'>
            <div class='panel-heading'>
              <h2>Methods</h2>
            </div>
          </div>
          <ul class='list-group'>")
    methods = model.methods
    methods.each do |method|
        type = method.type
        name = method.name
        inputs = method.inputs
        outputs = method.outputs
        description = method.description
        file.write("            <div class='list-group-item'>
                      <h4 class='list-group-item-heading'>#{type} - #{name}</h4>
                      <p><br>Input:<br>
                      <ul>")
        inputs.each do |input|
            type = input.type
            name = input.name
            file.write("<li>#{type}: #{name}</li>")
        end
        file.write("</ul><br>Output:<br>
                          <ul>")
        outputs.each do |output|
            if output != 'nil'
                type = output.type
                name = output.name
                file.write("<li>#{type}: #{name}</li>")
            else
                file.write('<li>Null</li>')
            end
        end
        file.write("</ul><br>Description: #{description}</p>
                    </div>")
    end
    file.write("</ul>
  </div>
  <div class='col-md-6'>
    <div class='panel panel-default'>
      <div class='panel-heading'>
          <h2>Routes</h2>
      </div>
    </div>
    <ul class='list-group'>")
    routes = model.routes
    routes.each do |route|
        type = route.type
        name = route.name
        request = route.request
        dataInput = route.dataInput
        inputs = route.inputs
        outputs = route.outputs
        file.write("            <div class='list-group-item'>
                      <h4 class='list-group-item-heading'>Route type #{type} - #{name}</h4>
                      <p><br>Request: #{request}<br>
                      <br>Notation type: #{dataInput}<br>
                      <br>Input:<br>
                      <ul>")
        inputs.each do |input|
            type = input.type
            name = input.name
            file.write("<li>#{type}: #{name}</li>")
        end
        file.write("</ul><br>Output:<br>
                          <ul>")
        outputs.each do |output|
            if output != 'nil'
                type = output.type
                name = output.name
                file.write("<li>#{type}: #{name}</li>")
            else
                file.write('<li>Null</li>')
            end
        end
        file.write("</ul></p>
                    </div>")
    end
    file.write("</ul>
  </div>
</div>
  </div>
  </body>")
end

#writeProjectPage(project, file, models, description) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/PhariDocGen/FileHandler.rb', line 420

def writeProjectPage(project, file, models, description)
    file.write("<div class='container panel panel-default'>
      <div class='row'>
        <div class='panel panel-default'>
          <div class='panel-heading'>
            <h1>#{project}</h1>
          </div>
          <div class='panel-body'>
            <h2>Project Description</h2>
            #{description}
          </div>
        </div>
      </div>
      <div class='row'>
        <div class='col-md-4'>
          <ul class='list-group'>")
    models.each do |model|
        link = model.name.downcase
        name = model.name.capitalize
        file.write("\t\t<a href='models/#{link}.html' class='list-indexes list-group-item'>#{name}</a>
        ")
    end
    file.write("\t</ul>
      </div>
      <div class='col-md-4 col-md-offset-2'>
        <img src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' alt='Phari logo' width='100%'>
      </div>
    </div>
  </div>
</body>")
end