linkingpaths (owner)

Revisions

gist: 13586 Download_button fork
public
Public Clone URL: git://gist.github.com/13586.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'rexml/document'
 
module Faker
  class Avatar
 
    @@avatars = []
    class << self
 
      def avatar
        Faker::Avatar.get_random_avatar
      end
 
      def avatar_file
        uri = URI.parse(Faker::Avatar.avatar)
        tmp_file = uri.path[/.*\/(.*)\z/,1]
        open(tmp_file, "wb") { |file|
            file.write(Net::HTTP.get_response(uri).read_body)
        }
        File.new(tmp_file)
      end
 
      def available_avatars
        @@avatars.size > 0
      end
      def cache_some_avatars
        url = URI.parse("http://twitter.com/statuses/public_timeline.xml")
        html=Net::HTTP.get_response(url).read_body
        doc = REXML::Document.new(html)
        doc.elements.each("//status/user/profile_image_url") { |avatar| @@avatars << avatar.text }
        return available_avatars
      end
      def get_random_avatar
        if available_avatars
          @@avatars.pop
        else
          raise RuntimeError.new("Unable to get avatars from the web.") unless cache_some_avatars
          get_random_avatar
        end
      end
    end
 
  end
 
end