Class: JSONSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/json-serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, linkauthor) ⇒ JSONSerializer

Returns a new instance of JSONSerializer.



7
8
9
10
# File 'lib/json-serializer.rb', line 7

def initialize(json, linkauthor)
  @json = json
  @linkauthor = linkauthor
end

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



4
5
6
# File 'lib/json-serializer.rb', line 4

def json
  @json
end

#linkauthorObject

Returns the value of attribute linkauthor.



5
6
7
# File 'lib/json-serializer.rb', line 5

def linkauthor
  @linkauthor
end

Instance Method Details

#compileObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/json-serializer.rb', line 12

def compile
  structured_gists = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }

  entries = JSON.parse(json)
  entries.each do |arr|
    title = arr["Title"] != nil ? arr["Title"].strip() : nil
    authors = arr["Authors"] != nil ? arr["Authors"].strip() : nil
    description = arr["Description"] != nil ? arr["Description"].strip() : nil
    section = arr["Section"] != nil ? arr["Section"].strip() : nil
    subsection = arr["Subsection"] != nil ? arr["Subsection"].strip() : nil
    url = arr["URL"] != nil ? arr["URL"].strip() : nil
    url_user = arr["URL_USER"] != nil ? arr["URL_USER"].strip() : nil

    if title == nil
      puts "Error: A title must be included for #{url}"
      next
    end
    
    if section == nil || subsection == nil
      puts "Error: #{title} must have a section and subsection to be compiled!"
      next
    end

    structured_gists[section][subsection][title] = {"Authors"=>authors, "Description"=>description,
                                                    "URL"=>url, "URL_USER"=>url_user}
  end
  structured_gists = structured_gists.sort.to_h
  structured_gists.keys.each do |k|
    structured_gists[k] = structured_gists[k].sort.to_h
    structured_gists[k].keys.each do |l|
      structured_gists[k][l] = structured_gists[k][l].sort.to_h
    end
  end
  
  return structured_gists
end

#to_html(file) ⇒ Object



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
# File 'lib/json-serializer.rb', line 72

def to_html(file)
  structured_gists = compile()
  File.open(file,"w") do |f|
    f.puts("<!DOCTYPE HTML>")
    f.puts("<html>")
    f.puts("  <head>\n    <meta charset=\"UTF-8\">\n  </head>");
    f.puts("  <body>")
    f.puts("    Generated by <a href=\"https://github.com/claymcleod/gist-compile\">gist-compile</a> at "+Time.new.strftime("%Y-%m-%d %H:%M:%S"))
    structured_gists.each do |j, v|
      f.puts("    <div class=\"section\">\r")
      f.puts("    <a id=\"#{j}\"></a>")
      f.puts("    <h1>#{j}</h1>\r")
      f.puts("    </div>")
      structured_gists[j].each do |k, v|
        f.puts("      <h2>#{k}</h2>\r")
        f.puts("\r\n")
        structured_gists[j][k].each do |t, v|
          url= structured_gists[j][k][t]["URL"]
          if url == nil
            f.puts("        <h3>#{t}</h3>\r")
          else
            f.puts("        <h3><a href=\'#{url}\'>#{t}</a></h3>")
          end
          f.puts("          <ul>");
          structured_gists[j][k][t].each do |l, v|
            if l != "URL" && l != "URL_USER" && v != nil
              if linkauthor && l == "Authors"
                f.puts("          <li>#{l}: <a href=\"http://#{structured_gists[j][k][t]["URL_USER"]}\">#{v}</a></li>\r")
              else
                f.puts("          <li>#{l}: #{v}</li>\r")
              end
            end
          end
          f.puts("\r\n")
          f.puts("          </ul>\r")
        end
      end
    end
    f.puts("    </body>");
    f.puts("</html>");
    f.close()
  end
end

#to_md(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/json-serializer.rb', line 49

def to_md(file)
  structured_gists = compile()
  File.open(file,"w") do |f|
    structured_gists.each do |j, v|
      f.puts("# #{j}\r")
      structured_gists[j].each do |k, v|
        f.puts("### #{k}\r")
        f.puts("\r\n")
        structured_gists[j][k].each do |t, v|
          f.puts("##### #{t}\r")
          structured_gists[j][k][t].each do |l, v|
            if v != nil
              f.puts("* #{l}: #{v}\r")
            end
          end
          f.puts("\r\n")
        end
      end
    end
    f.close()
  end
end