Sha256: 5b4982bf2fff1a344d62b66db26562b28d019b8b786894058fb9bf375c6424ac

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

require 'net/http'
require 'rubygems'
require 'hpricot'

class AllAvatarsSite

  def self.instance
    @@instance ||= AllAvatarsSite.new
  end

  def initialize
    @unused_avatar_urls = []
    @used_avatar_urls = []
  end

  def load_urls
    while @unused_avatar_urls.empty?
      path = "/avatars/showgallery.php?si=&perpage=18&sort=6&cat=all&ppuser="
      response = Net::HTTP.get_response("www.allavatars.com", path).body
      doc = Hpricot(response)
      doc.search("img") do |img|
        source = img.attributes['src']
        if source =~ %r{http://www.allavatars.com/avatars/data/.*} && !@used_avatar_urls.member?(source)
          @unused_avatar_urls << source
        end
      end
    end
  end

  def random_url
    load_urls if @unused_avatar_urls.empty?
    url = @unused_avatar_urls.pop
    @used_avatar_urls << url
    url
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
randomizer-0.0.2 lib/randomizer/all_avatars_site.rb