Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- lib/zentool/graph.rb
Overview
Graph class for pull_articles.rb
Class Method Summary collapse
- .article_link_map(articles, categories, sections, id_title_map, basic_auth) ⇒ Object
- .create_id_title_map(articles) ⇒ Object
- .extract_IDs(string) ⇒ Object
- .extract_links(string) ⇒ Object
- .validate_link(article, link, basic_auth) ⇒ Object
- .wrap(s, width = 15) ⇒ Object
Instance Method Summary collapse
- #generate ⇒ Object
- #graph_edges(id_title_map) ⇒ Object
- #graph_nodes(id_title_map) ⇒ Object
- #graph_settings ⇒ Object
-
#initialize(articles, sections, categories, basic_auth) ⇒ Graph
constructor
A new instance of Graph.
Constructor Details
#initialize(articles, sections, categories, basic_auth) ⇒ Graph
Returns a new instance of Graph.
4 5 6 |
# File 'lib/zentool/graph.rb', line 4 def initialize(articles, sections, categories, basic_auth) @articles, @sections, @categories, @basic_auth = articles, sections, categories, basic_auth end |
Class Method Details
.article_link_map(articles, categories, sections, id_title_map, basic_auth) ⇒ Object
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 |
# File 'lib/zentool/graph.rb', line 54 def self.article_link_map(articles, categories, sections, id_title_map, basic_auth) puts 'Constructing article link map...' article_link_map = {} articles.each do |article| unless (categories[sections[article['section_id']]['category_id']]['name'] == 'Announcements') || (article['body'].class != String) referenced_links = Graph.extract_links(article['body']) referenced_articles = [] unless referenced_links.empty? referenced_links.each do |link| if validate_link(article, link, basic_auth) id = Graph.extract_IDs(link) title = id_title_map[id] unless (id.class == NilClass) || (title.class == NilClass) || (id.to_s.size != 9) referenced_articles << Graph.wrap("#{title}\n#{id}") end else end end article_link_map[article['id']] = referenced_articles end end end article_link_map end |
.create_id_title_map(articles) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/zentool/graph.rb', line 21 def self.create_id_title_map(articles) x = {} articles.each do |article| x[article['id'].to_i] = article['title'] end x end |
.extract_IDs(string) ⇒ Object
33 34 35 |
# File 'lib/zentool/graph.rb', line 33 def self.extract_IDs(string) string.split(//).map { |x| x[/\d+/] }.compact.join('').to_i end |
.extract_links(string) ⇒ Object
29 30 31 |
# File 'lib/zentool/graph.rb', line 29 def self.extract_links(string) [URI.extract(string, /http(s)?/)].flatten end |
.validate_link(article, link, basic_auth) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zentool/graph.rb', line 37 def self.validate_link(article, link, basic_auth) response = HTTParty.get(link, basic_auth) unless response.code == 200 puts puts 'Broken Link Found' puts '-----------------' puts puts 'From page: ' + article['title'] puts 'At URL: ' + article['html_url'] puts puts "Message: Error #{response.code}: #{response.}" puts 'Broken link: ' + link return false end return true end |
.wrap(s, width = 15) ⇒ Object
17 18 19 |
# File 'lib/zentool/graph.rb', line 17 def self.wrap(s, width = 15) s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n") end |
Instance Method Details
#generate ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/zentool/graph.rb', line 8 def generate id_title_map = Graph.create_id_title_map(@articles) @article_link_map = Graph.article_link_map(@articles, @categories, @sections, id_title_map, @basic_auth) graph_settings graph_nodes(id_title_map) graph_edges(id_title_map) @g.output(png: 'article_relationships.png') end |
#graph_edges(id_title_map) ⇒ Object
111 112 113 114 115 |
# File 'lib/zentool/graph.rb', line 111 def graph_edges(id_title_map) @article_link_map.each do |id, referenced_articles| @g.add_edges(Graph.wrap("#{id_title_map[id]}\n#{id}"), referenced_articles.map(&:to_s)) end end |
#graph_nodes(id_title_map) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/zentool/graph.rb', line 103 def graph_nodes(id_title_map) nodes = [] @article_link_map.each do |id, _referenced_articles| nodes << Graph.wrap("#{id_title_map[id]}\n#{id}") end @g.add_nodes(nodes) end |
#graph_settings ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/zentool/graph.rb', line 80 def graph_settings $LOAD_PATH.unshift('../lib') @g = GraphViz.new('G') @g.node[:color] = '#222222' @g.node[:style] = 'filled' @g.node[:shape] = 'box' @g.node[:penwidth] = '1' @g.node[:fontname] = 'Helvetica' @g.node[:fillcolor] = '#eeeeee' @g.node[:fontcolor] = '#333333' @g.node[:margin] = '0.05' @g.node[:fontsize] = '12' @g.edge[:color] = '#666666' @g.edge[:weight] = '1' @g.edge[:fontsize] = '10' @g.edge[:fontcolor] = '#444444' @g.edge[:fontname] = 'Helvetica' @g.edge[:dir] = 'forward' @g.edge[:arrowsize] = '1' @g.edge[:arrowhead] = 'vee' end |