Class: Muzang::Plugins::RubyGems

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ RubyGems

Returns a new instance of RubyGems.



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

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.



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

def last_gem
  @last_gem
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#call(connection, message) ⇒ Object



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 20

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

  match(message, /^!unwatch (.*?)$/) do |match|
    @store.transaction do
      if @store[:gems].delete(match[1])
        connection.msg(message.channel, "I removed gem #{match[1]} from watchlist")
      end
    end
  end

  match(message, /^!cojapacze$/) do
    @store.transaction do
      connection.msg(message.channel, "#{@store[:gems].keys.join(', ')}")
    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



89
90
91
# File 'lib/muzang-plugins/muzang-rubygems.rb', line 89

def period
  30
end