Class: ChimpsFBAnalyzer::FBAnalyzer
- Inherits:
-
Object
- Object
- ChimpsFBAnalyzer::FBAnalyzer
- Defined in:
- lib/chimps_fb_analyzer.rb
Constant Summary collapse
- FB_STATS =
[ "date", "shared", "updated status", "likes a page", "is now friends", "changed profile pic", "creates event", "went to a place", "going to a place", "adds a new photo", "was tagged in a friend's status", "was tagged in a photo", "friend writes on timeline", "joined a group", "using Facebook in another language" ]
- ARRAY_LENGTH =
FB_STATS.count
Instance Attribute Summary collapse
-
#wall_path ⇒ Object
readonly
Returns the value of attribute wall_path.
Instance Method Summary collapse
- #analyze_wall ⇒ Object
-
#initialize(directory_path, options = {}) ⇒ FBAnalyzer
constructor
A new instance of FBAnalyzer.
- #scan_html_node(html_node, current_data, name) ⇒ Object
- #valid_date?(date) ⇒ Boolean
- #write_data_to_csv(data_array, output_path = nil) ⇒ Object
Constructor Details
#initialize(directory_path, options = {}) ⇒ FBAnalyzer
Returns a new instance of FBAnalyzer.
32 33 34 35 36 37 38 |
# File 'lib/chimps_fb_analyzer.rb', line 32 def initialize(directory_path, ={}) @directory_pathname = Pathname.new(directory_path) @output_file = [:output_path] @wall_path = @directory_pathname.join("html", "wall.htm") @data = [] @get_status_update = false end |
Instance Attribute Details
#wall_path ⇒ Object (readonly)
Returns the value of attribute wall_path.
10 11 12 |
# File 'lib/chimps_fb_analyzer.rb', line 10 def wall_path @wall_path end |
Instance Method Details
#analyze_wall ⇒ Object
40 41 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 |
# File 'lib/chimps_fb_analyzer.rb', line 40 def analyze_wall f = @wall_path.open html = Nokogiri::HTML(f) f.close @name = html.css('html body div.contents h1').first.text at_date = Date.new html_data = html.css('html body div.contents').children @data = [FB_STATS] current_data = Array.new(ARRAY_LENGTH, 0) while !html_data.empty? html_node = html_data.shift if valid_date?(html_node.text) new_date = Date.parse(html_node.text) if at_date != new_date if at_date.to_s == "-4712-01-01" at_date = new_date else @data.push(current_data) at_date = new_date current_data = Array.new(ARRAY_LENGTH, 0) index = FB_STATS.index('date') current_data[index] = at_date.to_s end end elsif @get_status_update @get_status_update = false index = FB_STATS.index("updated status") if current_data[index] == 0 current_data[index] = html_node.text else current_data[index] = ("|" + html_node.text) end else current_data = scan_html_node(html_node, current_data, @name) end end @data end |
#scan_html_node(html_node, current_data, name) ⇒ 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 126 127 128 129 130 131 |
# File 'lib/chimps_fb_analyzer.rb', line 89 def scan_html_node(html_node, current_data, name) case html_node.text when /#{name} updated (his|her) status/ @get_status_update = true when /#{name} shared a/ index = FB_STATS.index("shared") current_data[index] += 1 when /#{name} likes/ index = FB_STATS.index("likes a page") current_data[index] += 1 when /#{name} is going to/ index = FB_STATS.index("going to a place") current_data[index] += 1 when /#{name} went to/ index = FB_STATS.index("went to a place") current_data[index] += 1 when /#{name} joined/ index = FB_STATS.index("joined a group") current_data[index] += 1 when /#{name} was tagged in .+ photo/ # TODO Finish index = FB_STATS.index("was tagged in a photo") current_data[index] += 1 when /#{name} and .+ are now friends/ index = FB_STATS.index("is now friends") current_data[index] += 1 when /#{name} changed (his|her) profile picture/ index = FB_STATS.index("changed profile pic") current_data[index] += 1 when /#{name} created/ index = FB_STATS.index("creates event") current_data[index] += 1 when /#{name} added a new photo to the album/ index = FB_STATS.index("adds a new photo") current_data[index] += 1 when /wrote on your timeline/ index = FB_STATS.index("friend writes on timeline") current_data[index] += 1 when /#{name} is now using Facebook in/ index = FB_STATS.index("using Facebook in another language") current_data[index] += 1 end current_data end |
#valid_date?(date) ⇒ Boolean
150 151 152 |
# File 'lib/chimps_fb_analyzer.rb', line 150 def valid_date?(date) Date.parse(date) rescue false end |
#write_data_to_csv(data_array, output_path = nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/chimps_fb_analyzer.rb', line 133 def write_data_to_csv(data_array, output_path=nil) filepath = "" if output_path filepath = Pathname.new(output_path) else filepath = File.join(Dir.pwd, "#{@name.split.join("_")}_stats.csv") end puts "Writing to #{filepath}" CSV.open(filepath, 'w') do |csv| data_array.each do |set| csv << set end end end |