Class: Goatless::FriendFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/goatless/friend_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(friend_url, username, password) ⇒ FriendFetcher

Returns a new instance of FriendFetcher.



6
7
8
9
10
11
12
13
# File 'lib/goatless/friend_fetcher.rb', line 6

def initialize(friend_url, username, password)
  if Goatless.options[:verbose]
    puts "Processing feed: #{friend_url}"
  end
  @friend_url = friend_url
  @username = username
  @password = password
end

Instance Attribute Details

#friend_urlObject

Returns the value of attribute friend_url.



4
5
6
# File 'lib/goatless/friend_fetcher.rb', line 4

def friend_url
  @friend_url
end

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'lib/goatless/friend_fetcher.rb', line 4

def items
  @items
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/goatless/friend_fetcher.rb', line 4

def password
  @password
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/goatless/friend_fetcher.rb', line 4

def username
  @username
end

Instance Method Details

#fetchObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/goatless/friend_fetcher.rb', line 15

def fetch
  @patron_session = Patron::Session.new
  @patron_session.username = @username
  @patron_session.password = @password
  @patron_session.timeout  = 10
  @patron_session.connect_timeout = 30000
  @patron_session.auth_type = :digest
  @items = []
  xml = @patron_session.get("#{@friend_url}?auth=digest").body
  parse(xml)
end

#parse(xml) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/goatless/friend_fetcher.rb', line 27

def parse(xml)
  xml_doc = Nokogiri::XML::Reader(xml)
  @storage = PStore.new('goatless.pstore') # Hope this can decrease memory usage

  max_items = 50 # Move this to the CLI flag or config
  current_item = nil
  current_author = nil

  xml_doc.each do |node|
    next if max_items == 0
    case node.name
    when 'lj:journal'
      current_author = node.inner_xml if current_author.nil?
    when 'item'
      if current_item.nil?
        current_item = RssItem.new
      else
        current_item.author = current_author
        if current_item.pub_date > (DateTime.now - 7) # do not add items older then week
          @storage.transaction do
            @storage[:items] ||= Array.new
            unless @storage[:items].include? current_item
              @storage[:items] << current_item
              @storage.commit
            end
          end
        end
        current_item = nil
        max_items -= 1
      end
    when 'link'
      unless current_item.nil?
        current_item.permalink = node.inner_xml if current_item.permalink.nil?
      end
    when 'pubDate'
      current_item.pub_date = DateTime.parse(node.inner_xml) if current_item.pub_date.nil?
    when 'description'
      unless current_item.nil?
        if current_item.body.nil?
          current_item.body = node.inner_xml
        end
      end
    when 'lj:security'
      current_item.security_type = node.inner_xml if current_item.security_type.nil?
    when 'title'
      unless current_item.nil?
        current_item.title = node.inner_xml if current_item.title.nil?
      end
    end
  end
end