cinch-lastfm 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,18 @@
1
1
  require 'cinch'
2
2
  require 'nokogiri'
3
3
  require 'open-uri'
4
+ require 'redis'
5
+ require 'redis-namespace'
4
6
 
5
7
  module Cinch
6
8
  module Plugins
7
9
  class Lastfm
8
10
  include Cinch::Plugin
9
- #attr_accessor :backend
11
+ attr_accessor :backend, :redis
10
12
  # Redis backend
11
13
 
12
14
  class << self
13
- attr_accessor :lfmapi #:host, :port # Redis backend scrub
15
+ attr_accessor :lfmapi, :host, :port # Redis backend scrub
14
16
 
15
17
  def configure(&block)
16
18
  yield self
@@ -19,11 +21,15 @@ module Cinch
19
21
 
20
22
  def initialize(*args)
21
23
  super
22
- # @backend = Redis.new(self.class.host, self.class.port)
23
- # Will feature a redis backend
24
+ @redis = Redis.new(:host => self.class.host, :port => self.class.port, :thread_safe => true)
25
+ @backend = Redis::Namespace.new("cinch-lastfm", :redis => redis)
26
+ # Redis backend
24
27
  end
25
28
 
26
29
  match %r{np ([a-zA-Z0-9]+)}, :method => :now_playing
30
+ match %r{link ([a-zA-Z0-9]+)}, :method => :link_account
31
+ match %r{np}, :method => :now_playing_user
32
+
27
33
 
28
34
  # Gets Now Playing for an user
29
35
  def now_playing(m, nick)
@@ -31,6 +37,21 @@ module Cinch
31
37
  m.reply("#{nick} has recently played: #{artist} - #{track}")
32
38
  end
33
39
 
40
+ # Gets Now Playing for a linked account
41
+ def now_playing_user(m)
42
+ nick = @backend.get(m.user)
43
+ if nick
44
+ artist, track = get_data(nick)
45
+ m.reply("#{nick} has recently played: #{artist} - #{track}")
46
+ end
47
+ end
48
+
49
+ # Links a designated Last Fm account
50
+ def link_account(m, nick)
51
+ @backend.set(m.user, nick)
52
+ m.reply("#{m.user} is now linked with account #{nick}!")
53
+ end
54
+
34
55
  def get_data(nick)
35
56
  lfmXML = Nokogiri::XML(open("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=#{nick}&api_key=#{self.class.lfmapi}"))
36
57
  artist = lfmXML.xpath('//artist').first.content
data/spec/lastfm_spec.rb CHANGED
@@ -4,6 +4,10 @@ describe Cinch::Plugins::Lastfm do
4
4
 
5
5
  before :each do
6
6
  Cinch::Plugins::Lastfm.any_instance.stub(:__register).and_return(true)
7
+ Cinch::Plugins::Lastfm.configure do |c|
8
+ c.host = "localhost"
9
+ c.port = "6379"
10
+ end
7
11
  @bot = double()
8
12
  end
9
13
 
@@ -12,11 +16,28 @@ describe Cinch::Plugins::Lastfm do
12
16
  @lfmbot = Cinch::Plugins::Lastfm.new(@bot)
13
17
  @message = double()
14
18
  @lfmbot.stub(:get_data).and_return(["Artist","Track"])
15
- @message.should_receive(:reply).with("czujechlodno has recently played: Artist - Track")
19
+ @message.should_receive(:reply).with("user has recently played: Artist - Track")
20
+ end
21
+
22
+ it "should get now_playing" do
23
+ @lfmbot.now_playing(@message, "user")
24
+ end
25
+
26
+ end
27
+
28
+ context "#now_playing_user" do
29
+ before :each do
30
+ @lfmbot = Cinch::Plugins::Lastfm.new(@bot)
31
+ @message = double()
32
+ @message.stub(:user).and_return("testuser")
33
+ @lfmbot.stub(:get_data).and_return(["Artist","Track"])
34
+ @message.should_receive(:reply).with("testuser is now linked with account user!")
35
+ @message.should_receive(:reply).with("user has recently played: Artist - Track")
16
36
  end
17
37
 
18
38
  it "should get now_playing" do
19
- @lfmbot.now_playing(@message, "czujechlodno")
39
+ @lfmbot.link_account(@message, "user")
40
+ @lfmbot.now_playing_user(@message)
20
41
  end
21
42
 
22
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-lastfm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-29 00:00:00.000000000 Z
12
+ date: 2013-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 1.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 1.5.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: cinch
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -43,6 +43,38 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 2.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: redis
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: redis-namespace
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.0
46
78
  - !ruby/object:Gem::Dependency
47
79
  name: rspec
48
80
  requirement: !ruby/object:Gem::Requirement