Class: RubyGems

Inherits:
Object
  • Object
show all
Includes:
Muzang::Plugins::Helpers
Defined in:
lib/muzang-plugins/muzang-rubygems.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Muzang::Plugins::Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ RubyGems

Returns a new instance of RubyGems.



10
11
12
13
14
15
16
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 10

def initialize(bot)
  @bot = bot
  @store = PStore.new("#{ENV["HOME"]}/.muzang/muzang.rubygems")
  @store.transaction do
    @store[:gems] ||= {}
  end
end

Instance Attribute Details

#last_gemObject

Returns the value of attribute last_gem.



8
9
10
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 8

def last_gem
  @last_gem
end

#storeObject

Returns the value of attribute store.



8
9
10
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 8

def store
  @store
end

Instance Method Details

#call(connection, message) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 18

def call(connection, message)
  match(message, /^watch! (.*?)$/) do |match|
    @current_gem = match[1]

    @store.transaction do
      unless @store[:gems][@current_gem]
        @store[:gems][match[1]] = {:name => @current_gem}
        @new_gem = true
      end
    end

    if @new_gem
      http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{@current_gem}.json").get
      http.callback {
        begin
          gem = JSON.parse(http.response)
          @store.transaction do
            @store[:gems][@current_gem][:version] = gem["version"]
          end
          connection.msg(message.channel, "I added gem #{@current_gem} to watchlist")
          connection.msg(message.channel, "Current version: #{@current_gem} (#{gem["version"]})")
          @new_gem = false
        rescue Exception
          connection.msg(message.channel, "Gem name is incorrect")
          @store.transaction{@store[:gems].delete(@current_gem)}
        end
      }
    else
      connection.msg(message.channel, "Gem #{@current_gem} is already observed")
    end
  end

  on_join(connection, message) do
    EventMachine.add_periodic_timer(period) do
      gems = @store.transaction{@store[:gems].values}
      EM::Iterator.new(gems, 1).each do |gem, iter|
        http = EventMachine::HttpRequest.new("http://rubygems.org/api/v1/gems/#{gem[:name]}.json").get
        http.callback {
          iter.next
          begin
            current_gem = JSON.parse(http.response)
            if Gem::Version.new(gem[:version]) < Gem::Version.new(current_gem["version"])
              @store.transaction do
                @store[:gems][gem[:name]][:version] = current_gem["version"]
              end
              connection.msg(message.channel, "New version #{gem[:name]} (#{current_gem["version"]})")
            end
          rescue
          end
        }
      end
    end
  end
end

#periodObject



73
74
75
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 73

def period
  30
end