Module: Repository

Defined in:
lib/common.rb

Constant Summary collapse

SUPPORTED_MIME_TYPES =
{
    "application/pdf": {name: "PDF", expression: /\.pdf$/i},
    "text/html": {name: "HTML", expression: /\.html?$/i},
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
{name: "Word", expression: /\.docx?$/i},
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
{name: "Excel", expression: /\.xlsx?$/i},
    "application/hl7-cds-knowledge-artifact-1.3+xml": {name: 'HL7 KNART v1.3', expression: /KRprt(?!_CRCK).*\.xml$/i},
    "application/docbook": {name: 'DocBook', expression: /(HIMKWP|KVRpt|CSD).*\.xml$/i},
    'application/hl7-cds-knowledge-artifact-composite+xml': {name: 'Composite Artifact', expression: /CRCK_.*\.xml$/i},
    'image/svg+xml': {name: "SVG Image", expression: /\.svg$/i}
}
KNART_MIME_TYPES =
[
    "application/hl7-cds-knowledge-artifact-1.3+xml"
]
COMPOSITE_MIME_TYPES =
[
    'application/hl7-cds-knowledge-artifact-composite+xml'
]
DIGRAPH_TEMPLATE =
'image/svg+xml': {name: "SVG Image", expression: /\.svg$/i}
    }
  
  KNART_MIME_TYPES = [
    "application/hl7-cds-knowledge-artifact-1.3+xml"
    ]

  COMPOSITE_MIME_TYPES = [
    'application/hl7-cds-knowledge-artifact-composite+xml'
    ]


DIGRAPH_TEMPLATE = "digraph {\n  <% artifacts.each do |a| %>\n  artifact_<%= a[:hash] %>[label=\"<%= a[:name] %>\\n<%= a[:file] %>\", shape=rounded, style=filled, fillcolor=lightblue]<% end %>\n\n  <% events.each do |k, v| %>\n  event_<%= v %>[label=\"<%= k %>\", style=filled, fillcolor=yellow]<% end %>\n  \n  <% artifacts.each do |a| %><% a[:emits].each do |e| %>\n  artifact_<%= a[:hash] %> -> event_<%= events[e[:name]] %>[fontsize=8, label=\"emits\\n<%= e[:conditions] %>\"]<% end %><% end %>\n  <% artifacts.each do |a| %><% a[:triggers].each do |t| %>\n  event_<%= events[t] %> -> artifact_<%= a[:hash] %>[label=\"triggers\"]<% end %><% end %>\n}\n"

Instance Method Summary collapse

Instance Method Details

#audit_content_directory(root, manifest) ⇒ Object



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
196
# File 'lib/common.rb', line 166

def audit_content_directory(root, manifest)
  good = []
  bad = []
  manifest['groups'].each do |group|
    group['items'].each do |item|
      if item['path']
        path = "#{root}/#{item['path']}"
        if File.exist? path
          good << item
        else
          bad << item
        end
      end
      begin
        if item['url']
          response = HTTParty.head(item['url'])
          if(response.code >= 400)
            bad << item
          else
            good << item
          end
        end
      rescue SocketError => e
        # Probably a bad DNS or protocol name.
        bad << item
      end
    end
  end
  return [good, bad]

end

#content_directory_to_item_tree(root) ⇒ Object



117
118
119
120
121
122
123
124
125
126
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
# File 'lib/common.rb', line 117

def content_directory_to_item_tree(root)
  content = []
  list = Dir["#{root}/**/**"]
  list.each do |n|
    # puts n
    if(File.file?(n))
      # ext = File.extname(n).downcase[1..-1]
      mimeType = mimeTypeForFile(n)
      name = n.split('/')[1] #.split['.'][0]
      name = name.split('_').collect(&:capitalize).join(' ')
      tags = n.split('/').select{|d| d.length <= 4}
      # Add subdirectory names to the tag list
      subs = Dir.glob(File.dirname(n) + '/*').select{ |t|
        puts t
        # puts          File.directory?(t)
        # puts  File.dirname(n) != t
        # byebug
        File.directory?(t) && n != t
      }.collect{ |t| File.basename(t)}
      puts "#{n} SUBS: #{subs}"
      tags |= subs
      if mimeType # it's a supported piece of content
        item = {
          'name': name,
          'path': n,
          'mimeType': mimeType,
          'tags': tags
        }
        update_security(item)
        item = rekey(item)
        # Any directory name of 4 characters or less will be treated as a tag automatically
        content << item
      end
    end
  end
  content
end

#generate_dot(doc) ⇒ Object



42
43
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
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
# File 'lib/common.rb', line 42

def generate_dot(doc)
  artifacts = []
  events = {}
  doc.xpath('//xmlns:containedArtifacts/xmlns:artifact').each do |a|
    name = a.xpath('./xmlns:name/@value').to_s
    file = a.xpath('./xmlns:reference/xmlns:url/@address').to_s
    file = '(embedded)' if file.empty?
    triggers = []
    emits = []
    tmp = {
      name: name,
      hash: Digest::SHA1.hexdigest(name),
      file: file,
      triggers: triggers,
      emits: emits
    }
    artifacts << tmp

    # Search for triggers
    # puts a
    a.xpath('.//xmlns:triggers/xmlns:trigger/@onEventName').each do |t|
      value = t.to_s
      triggers << value
      events[value] = Digest::SHA1.hexdigest(value)
    end

    # Emmitted events within embedded KNARTs.
    a.xpath('./xmlns:knowledgeDocument//xmlns:simpleAction[@xsi:type="FireEventAction"]').each do |action|
      conditions = action.xpath('./xmlns:conditions').to_s.gsub('"', '\"')
      # puts conditions
      action.xpath('.//elm:element[@name="EventName"]/elm:value/@value').each do |n|
        value = n.to_s
        emits << {
          name: value,
          conditions: conditions
        }
        events[value] = Digest::SHA1.hexdigest(value)
      end
    end

    # Emmitted events for referencesd KNARTs.
    a.xpath('./xmlns:onCompletion//xmlns:eventName/@name').each do |n|
      value = n.to_s
      emits << {
        name: value,
        conditions: '(always)'
      }
      events[value] = Digest::SHA1.hexdigest(value)
    end
    

  end
  # puts artifacts
  # puts events
  renderer = ERB.new(DIGRAPH_TEMPLATE)
  renderer.result(binding)
end

#mimeTypeForFile(path) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/common.rb', line 105

def mimeTypeForFile(path)
  mimeType = nil
  SUPPORTED_MIME_TYPES.each do |k,v|
    if v[:expression].match?(path)
      mimeType = k
      break
    end
  end
  # puts mimeType || path
  mimeType
end

#rekey(hash) ⇒ Object

Forces keys to strings.



101
102
103
# File 'lib/common.rb', line 101

def rekey(hash)
  hash.collect{|k,v| [k.to_s, v]}.to_h
end

#render_partial(name, stuff) ⇒ Object



198
199
200
201
# File 'lib/common.rb', line 198

def render_partial(name, stuff)
  template = File.join(File.dirname(File.expand_path(__FILE__)), name)
  Slim::Template.new(template).render(self, stuff)
end

#update_security(item) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/common.rb', line 155

def update_security(item)
  # byebug
  content = File.read(item[:path])
  item['security'] = rekey({
    sha1: Digest::SHA1.hexdigest(content),
    sha256: Digest::SHA256.hexdigest(content),
    md5: Digest::MD5.hexdigest(content)
  })
  puts item
end