class ChimpsFBAnalyzer::FBAnalyzer

Constants

ARRAY_LENGTH
FB_STATS

Attributes

wall_path[R]

Public Class Methods

new(directory_path, options={}) click to toggle source
# File lib/chimps_fb_analyzer.rb, line 32
def initialize(directory_path, options={})
  @directory_pathname = Pathname.new(directory_path)
  @output_file = options[:output_path]
  @wall_path = @directory_pathname.join("html", "wall.htm")
  @data = []
  @get_status_update = false
end

Public Instance Methods

analyze_wall() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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