Sha256: 985845d854d6e1cdd762a1ce234ce08afccd351750e08c8bf79cc8631e555cdb

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

require 'open-uri'
require 'time'
require 'rexml/document'

class Technorati
  include REXML

  def choose(num)
    return cosmos unless cosmos.size > num
    bag = []
    set = cosmos.dup
    num.times {|x| bag << set.delete_at(rand(set.size))}
    bag
  end

  attr_accessor :url, :link, :title, :cosmos

  # This object holds given information of a picture
  class Inbound < Struct.new(:link, :title)
    def to_s; title end
  end

  def initialize(url, refresh = true)
    self.cosmos  = []
    self.url    = url
    self.refresh if refresh
  end

  # This method lets you refresh the tasks int the tasks array
  # useful if you keep the object cached in memory and
  def refresh
    open(@url) do |http|
      parse(http.read)
    end
  end

private

  def parse(body)
    xml = Document.new(body)
    self.cosmos        = []
    XPath.each(xml, "//item/") do |elem|
      inbound = Inbound.new
      inbound.title       = XPath.match(elem, "title/text()").to_s
      inbound.link        = XPath.match(elem, "link/text()").to_s
      cosmos << inbound
    end
  end
end


Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
typo-3.99.0 app/models/aggregations/technorati.rb
typo-3.99.3 app/models/aggregations/technorati.rb
typo-3.99.1 app/models/aggregations/technorati.rb
typo-3.99.2 app/models/aggregations/technorati.rb
typo-4.0.1 app/models/aggregations/technorati.rb
typo-4.0.0 app/models/aggregations/technorati.rb
typo-4.0.2 app/models/aggregations/technorati.rb
typo-3.99.4 app/models/aggregations/technorati.rb
typo-4.0.3 app/models/aggregations/technorati.rb
typo-4.1.1 vendor/plugins/technorati_sidebar/lib/technorati.rb
typo-4.1 vendor/plugins/technorati_sidebar/lib/technorati.rb