Class: Vanity::Source::RubyGems

Inherits:
Object
  • Object
show all
Includes:
Vanity::Source
Defined in:
lib/vanity/sources/ruby_gems.rb

Overview

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

Instance Method Summary collapse

Methods included from Vanity::Source

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

Instance Method Details

#meta(context) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/vanity/sources/ruby_gems.rb', line 46

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

#setup(context, params) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/vanity/sources/ruby_gems.rb', line 7

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

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

Yields:

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


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/vanity/sources/ruby_gems.rb', line 19

def update(context, webhook)
  gem_name = context["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 context["version"]
    current, latest = Gem::Version.new(context["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>."
      yield :activity=>{ :uid=>"#{gem_name}-#{latest}", :url=>"http://rubygems.org/gems/#{gem_name}",
                         :html=>html, :tags=>%w{release} }
    end
  end
  context["version"] = json["version"]
  context.update json.slice(*%w{homepage_uri project_uri info authors info})
  yield :set=>{ :downloads=>json["downloads"] }
end

#validate(context) ⇒ Object



15
16
17
# File 'lib/vanity/sources/ruby_gems.rb', line 15

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