Class: Rubygem

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Rubygem

Returns a new instance of Rubygem.



11
12
13
14
15
16
17
18
# File 'lib/rubygemstats.rb', line 11

def initialize(name)
	stats = Rubygem.scrape(Rubygem.uri(name))
	@name = name
	@version = stats[:version]
	@total_downloads = stats[:total_downloads]
	@for_this_version = stats[:for_this_version]
	@authors = stats[:authors]
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



9
10
11
# File 'lib/rubygemstats.rb', line 9

def authors
  @authors
end

#for_this_versionObject

Returns the value of attribute for_this_version.



8
9
10
# File 'lib/rubygemstats.rb', line 8

def for_this_version
  @for_this_version
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/rubygemstats.rb', line 5

def name
  @name
end

#total_downloadsObject

Returns the value of attribute total_downloads.



7
8
9
# File 'lib/rubygemstats.rb', line 7

def total_downloads
  @total_downloads
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/rubygemstats.rb', line 6

def version
  @version
end

Class Method Details

.authors(doc) ⇒ Object



50
51
52
# File 'lib/rubygemstats.rb', line 50

def self.authors(doc)
	doc.search("//div[@class='authors info-item']").search("//p").inner_html.split(", ")
end

.for_this_version(doc) ⇒ Object



46
47
48
# File 'lib/rubygemstats.rb', line 46

def self.for_this_version(doc)
	doc.search("//div[@class='downloads counter']").search("//strong")[1].inner_html.gsub(",","").to_i
end

.gem_name(uri) ⇒ Object



34
35
36
# File 'lib/rubygemstats.rb', line 34

def self.gem_name(uri)
	uri.scan(/\/gems\/\w+/).first.split("/")[-1]
end

.scrape(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rubygemstats.rb', line 24

def self.scrape(uri)
	doc = open(uri){|f|Hpricot(f)}
	g = self.gem_name(uri)
	v = self.version(doc)
	td = self.total_downloads(doc)
	ftv = self.for_this_version(doc)
	a = self.authors(doc)
	{:gem_name => g, :version => v, :total_downloads => td, :for_this_version => ftv, :authors => a}
end

.total_downloads(doc) ⇒ Object



42
43
44
# File 'lib/rubygemstats.rb', line 42

def self.total_downloads(doc)
	doc.search("//div[@class='downloads counter']").search("//strong").first.inner_html.gsub(",","").to_i
end

.uri(name) ⇒ Object



20
21
22
# File 'lib/rubygemstats.rb', line 20

def self.uri(name)
	"http://rubygems.org/gems/#{name}"
end

.version(doc) ⇒ Object



38
39
40
# File 'lib/rubygemstats.rb', line 38

def self.version(doc)
	doc.search("//div[@class='versions']").search("//a").first.attributes["href"].split("/")[-1]
end