class Tubeclip::Parser::ActivityParser

Returns an array of the user's activity

Public Instance Methods

parse_content(content) click to toggle source
# File lib/tubeclip/parser.rb, line 126
def parse_content(content)
  doc = Nokogiri::XML(content.body)
  feed = doc.at("feed")

  activities = []
  feed.css("entry").each do |entry|
    if parsed_activity = parse_activity(entry)
      activities << parsed_activity
    end
  end

  return activities
end

Protected Instance Methods

parse_activity(entry) click to toggle source

Parses the user's activity feed.

# File lib/tubeclip/parser.rb, line 143
def parse_activity(entry)
  # Figure out what kind of activity we have
  video_type = nil
  parsed_activity = nil
  entry.css("category").each do |category_tag|
    if category_tag["scheme"] == "http://gdata.youtube.com/schemas/2007/userevents.cat"
      video_type = category_tag["term"]
    end
  end

  if video_type
    case video_type
    when "video_rated"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "video_rated",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :videos => parse_activity_videos(entry),
        :video_id => entry.at_xpath("yt:videoid") ? entry.at_xpath("yt:videoid").text : nil
      )
    when "video_shared"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "video_shared",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :videos => parse_activity_videos(entry),
        :video_id => entry.at_xpath("yt:videoid") ? entry.at_xpath("yt:videoid").text : nil
      )
    when "video_favorited"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "video_favorited",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :videos => parse_activity_videos(entry),
        :video_id => entry.at_xpath("yt:videoid") ? entry.at_xpath("yt:videoid").text : nil
      )
    when "video_commented"
      # Load the comment and video URL
      comment_thread_url = nil
      video_url = nil
      entry.css("link").each do |link_tag|
        case link_tag["rel"]
        when "http://gdata.youtube.com/schemas/2007#comments"
          comment_thread_url = link_tag["href"]
        when "http://gdata.youtube.com/schemas/2007#video"
          video_url = link_tag["href"]
        else
          # Invalid rel type, do nothing
        end
      end

      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "video_commented",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :videos => parse_activity_videos(entry),
        :video_id => entry.at_xpath("yt:videoid") ? entry.at_xpath("yt:videoid").text : nil,
        :comment_thread_url => comment_thread_url,
        :video_url => video_url
      )
    when "video_uploaded"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "video_uploaded",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :videos => parse_activity_videos(entry),
        :video_id => entry.at_xpath("yt:videoid") ? entry.at_xpath("yt:videoid").text : nil
      )
    when "friend_added"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "friend_added",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :username => entry.at_xpath("yt:username") ? entry.at_xpath("yt:username").text : nil
      )
    when "user_subscription_added"
      parsed_activity = Tubeclip::Model::Activity.new(
        :type => "user_subscription_added",
        :time => entry.at("updated") ? entry.at("updated").text : nil,
        :author => entry.at("author/name") ? entry.at("author/name").text : nil,
        :username => entry.at_xpath("yt:username") ? entry.at_xpath("yt:username").text : nil
      )
    else
      # Invalid activity type, just let it return nil
    end
  end

  return parsed_activity
end
parse_activity_videos(entry) click to toggle source

If a user enabled inline attribute videos may be included in results.

# File lib/tubeclip/parser.rb, line 234
def parse_activity_videos(entry)
  videos = []

  entry.css("link").each do |link_tag|
    videos << Tubeclip::Parser::VideoFeedParser.new(link_tag).parse if link_tag.at("entry")
  end

  if videos.size <= 0
    videos = nil
  end

  return videos
end