Class: DashFu::Mario::RubyGems

Inherits:
Object
  • Object
show all
Includes:
DashFu::Mario
Defined in:
lib/dash-fu/marios/ruby_gems.rb

Overview

Track activity (downloads and releases) of a Ruby Gem from rubygems.org.

Instance Method Summary collapse

Methods included from DashFu::Mario

all, #description, #display, find, included, load_marios, #name, #register, #unregister

Instance Method Details

#meta(source) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/dash-fu/marios/ruby_gems.rb', line 46

def meta(source)
  [ { :title=>"Project", :text=>source["gem_name"], :url=>source["homepage_uri"] || source["project_uri"] },
    { :text=>source["info"] },
    { :title=>"Version", :text=>source["version"] },
    { :title=>"Authors", :text=>source["authors"] },
    { :title=>"Source", :text=>"RubyGems", :url=>source["project_uri"] } ]
end

#setup(source, params) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dash-fu/marios/ruby_gems.rb', line 6

def setup(source, params)
  gem_name = params["gem_name"].to_s.strip
  source["metric.name"] = "RubyGems: #{gem_name}"
  source["metric.columns"] = [{ :id=>"downloads", :label=>"Downloads" }]
  source["metric.totals"] = true
  source["gem_name"] = gem_name
end

#update(source, request) {|:set=>{ :downloads=>json["downloads"] }| ... } ⇒ Object

Yields:

  • (:set=>{ :downloads=>json["downloads"] })


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
# File 'lib/dash-fu/marios/ruby_gems.rb', line 18

def update(source, request)
  gem_name = source["gem_name"]
  uri = URI.parse("http://rubygems.org/api/v1/gems/#{Rack::Utils.escape gem_name}.json")
  case response = Net::HTTP.get_response(uri)
  when Net::HTTPOK
    json = JSON.parse(response.body) rescue nil
  when Net::HTTPNotFound
    raise "Could not find the Gem #{gem_name}"
  end
  unless json
    logger.error "RubyGems: #{response.code} #{response.message}"
    raise "Last request didn't go as expected, trying again later"
  end  

  if source["version"]
    current, latest = Gem::Version.new(source["version"]), Gem::Version.new(json["version"])
    if latest > current
      html = "released <a href=\"http://rubygems.org/gems/#{gem_name}/versions/#{latest}\">#{gem_name} version #{latest}</a>."
      person = { :fullname=>"RubyGems", :identities=>"url:http://rubygems.org/", :photo_url=>"http://dash-fu.com/images/sources/ruby.png" }
      yield :activity=>{ :uid=>"#{gem_name}-#{latest}", :url=>"http://rubygems.org/gems/#{gem_name}",
                         :html=>html, :tags=>%w{release}, :person=>person }
    end
  end
  source["version"] = json["version"]
  source.update json.slice(*%w{homepage_uri project_uri info authors info})
  yield :set=>{ :downloads=>json["downloads"] }
end

#validate(source) ⇒ Object



14
15
16
# File 'lib/dash-fu/marios/ruby_gems.rb', line 14

def validate(source)
  raise "Missing gem name" if source["gem_name"].blank?
end