Class: Sunflower::List

Inherits:
Array
  • Object
show all
Defined in:
lib/sunflower/list.rb

Overview

Class representing a list of articles. Inherits from Array.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sunflower, type, key, opts = {}) ⇒ List

Create a new article list and fill it with items.

Sunflower may be nil; this will, however, make most methods unavailable.

This is in fact a wrapper for various list generator methods, each private, named with the format of “list_<type>”, which accept the key and opts arguments and return arrays. You can use this behavior to create your own ones.

You should probably use Sunflower#make_list instead of calling this directly.



15
16
17
18
19
20
21
22
23
24
# File 'lib/sunflower/list.rb', line 15

def initialize sunflower, type, key, opts={}
	@sunflower = sunflower
	
	meth = :"list_#{type}"
	if self.respond_to? meth, true
		super(self.send meth, key, opts)
	else
		raise Sunflower::Error, "no such list type available: #{type}"
	end
end

Class Method Details

.from_ary(ary, sunflower = nil) ⇒ Object

Construct new list from an array.



27
28
29
# File 'lib/sunflower/list.rb', line 27

def self.from_ary ary, sunflower=nil
	Sunflower::List.new sunflower, 'pages', ary
end

Instance Method Details

#pagesObject

Converts self to an array of Sunflower::Page objects.

Use #pages_preloaded to preload the text of all pages at once, instead of via separate requests.



35
36
37
# File 'lib/sunflower/list.rb', line 35

def pages
	Array.new self.map{|t| Sunflower::Page.new t, @sunflower }
end

#pages_preloadedObject

Converts self to an array of Sunflower::Page objects, then preloads the text in all of them using as little requests as possible. (API limit is at most 500 pages/req for bots, 50 for other users.)

If any title is invalid, Sunflower::Error will be raised.

If any title is uncanonicalizable by Sunflower#cleanup_title, it will not blow up or return incorrect results; however, text of some other pages may be missing (it will be lazy-loaded when requested, as usual).



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
# File 'lib/sunflower/list.rb', line 48

def pages_preloaded
	pgs = self.pages.sort_by{|a| a.title}
	at_once = @sunflower.is_bot? ? 500 : 50
	
	# this is different from self; page titles are guaranteed to be canonicalized
	titles = pgs.map{|a| a.title }
	
	titles.each_slice(at_once).with_index do |slice, slice_no|
		res = @sunflower.API('action=query&prop=revisions&rvprop=content&titles='+CGI.escape(slice.join '|'))
		res['query']['pages'].values.sort_by{|h| h['title'] || '' }.each_with_index do |h, i|
			page = pgs[slice_no*at_once + i]
			
			if h['title'] and h['title'] == page.title
				page.preloaded_text = true
				
				if h['missing']
					page.text = ''
				elsif h['invalid']
					raise Sunflower::Error, 'title invalid: '+page.title
				else
					page.text = h['revisions'][0]['*']
				end
			end
		end
	end
	
	return pgs
end