anyplayer 1.1.2 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,7 @@
1
- # The Selector is the tool that will find you the currently running player on your platform
1
+ require "rbconfig"
2
+
3
+ # The Selector is the tool that will find you the currently running player
4
+ # on your platform.
2
5
  #
3
6
  # Example:
4
7
  #
@@ -7,62 +10,90 @@
7
10
  # player = selector.player
8
11
  #
9
12
  # Needs the PLAYERS constant to contain a list of players.
13
+ module Anyplayer
14
+ class Selector
15
+ attr_accessor :verbose
16
+ attr_reader :errors
10
17
 
11
- class Anyplayer::Selector
12
- attr_accessor :verbose
13
- attr_reader :errors
18
+ def initialize
19
+ @verbose = false
20
+ @errors = []
21
+ end
14
22
 
15
- def initialize
16
- @verbose = false
17
- @errors = []
18
- end
23
+ # Returns an instance of the first music player that's launched
24
+ def player
25
+ PLAYERS.each do |player|
26
+ player_load(player) || next
19
27
 
20
- # Returns an instance of the first music player that's launched
21
- def player
22
- players_for_this_platform.each { |player|
23
- player_load(player) or next
24
- instance = player_class(player).new
25
- return instance if player_launched(instance)
26
- }
27
- nil
28
- end
28
+ instance = player_class(player).new
29
+ player_on_platform?(instance) || next
29
30
 
31
+ return instance if player_launched?(instance)
32
+ end
33
+ nil
34
+ end
30
35
 
31
- private
36
+ private
32
37
 
33
- def players_for_this_platform
34
- players = Anyplayer::PLAYERS
35
- players.reject! { |player| player =~ /_mac$/ } if RUBY_PLATFORM !~ /darwin/
36
- players.reject! { |player| player =~ /_windows$/ } if RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
37
- players
38
+ def platform
39
+ @platform ||= begin
40
+ host_os = RbConfig::CONFIG["host_os"]
41
+ case host_os
42
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
43
+ :windows
44
+ when /darwin|mac os/
45
+ :mac
46
+ when /linux/
47
+ :linux
48
+ when /solaris|bsd/
49
+ :unix
50
+ else
51
+ @errors << "Unknown operating system #{host_os.inspect}"
52
+ nil
53
+ end
54
+ end
38
55
  end
39
56
 
40
57
  def player_load(player)
58
+ log "#{player}:"
41
59
  begin
42
60
  require "anyplayer/players/#{player}"
43
- $stderr.puts "Loaded #{player}" if verbose
61
+ log " loaded"
44
62
  true
45
63
  rescue LoadError => e
46
- $stderr.puts "Could not load #{player}" if verbose
64
+ log " #{e.message}"
47
65
  @errors << "Error loading #{player}: #{e.message}"
48
66
  false
49
67
  end
50
68
  end
51
69
 
52
- def player_class(player)
53
- camelized = player.to_s.split(/_/).map{ |word| word.capitalize }.join
54
- Anyplayer::const_get(camelized)
70
+ def player_on_platform?(player)
71
+ result = player.platforms.include?(platform)
72
+ log " not on platform" unless result
73
+ result
55
74
  end
56
75
 
57
- def player_launched(player)
58
- $stderr.puts "#{player.name} launched?" if verbose
76
+ def player_class(player)
77
+ camelized = player.to_s.split(/_/).map(&:capitalize).join
78
+ Anyplayer.const_get(camelized)
79
+ end
59
80
 
81
+ def player_launched?(player)
60
82
  seconds = 5
61
83
  begin
62
- Timeout::timeout(seconds) { player.launched? }
84
+ Timeout.timeout(seconds) do
85
+ launched = player.launched?
86
+ log launched ? " launched" : " not launched"
87
+ launched
88
+ end
63
89
  rescue Timeout::Error
64
- $stderr.puts "Timed out after #{seconds} seconds" if verbose
90
+ log " timed out after #{seconds} seconds"
65
91
  false
66
92
  end
67
93
  end
94
+
95
+ def log(text)
96
+ $stderr.puts text if verbose
97
+ end
98
+ end
68
99
  end
@@ -1,3 +1,3 @@
1
1
  module Anyplayer
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -1,9 +1,15 @@
1
1
  require "test_helper"
2
2
 
3
- class AnyplayerTest < MiniTest::Unit::TestCase
4
- # def test_loading_all_players_does_not_explode
5
- # PLAYERS.each do |player|
6
- # require "anyplayer/players/#{player}"
7
- # end
8
- # end
3
+ class AnyplayerTest < Minitest::Test
4
+ def test_loading_all_players_does_not_explode
5
+ PLAYERS.each do |player|
6
+ require "anyplayer/players/#{player}"
7
+ end
8
+ rescue LoadError
9
+ # Allow LoadErrors that depend on Windows
10
+ end
11
+
12
+ def test_launched
13
+ assert_kind_of Anyplayer::Player, Anyplayer.launched
14
+ end
9
15
  end
@@ -0,0 +1,150 @@
1
+ require "test_helper"
2
+ require "anyplayer/command_line"
3
+
4
+ class CommandLineTest < Minitest::Test
5
+ USAGE = "Usage: anyplayer [-v] [command]
6
+
7
+ Where command is one of: playpause, play, pause, next, prev, voldown, volup, volume, track, artist, album, vote, name, launched, paused, playing.
8
+
9
+ Player connected: Fake Player.
10
+ "
11
+
12
+ def setup
13
+ @player = flexmock(:player)
14
+ @selector = flexmock(:selector)
15
+ @selector.should_receive(:player).and_return(@player)
16
+ flexmock(Anyplayer::Selector).should_receive(:new).and_return(@selector)
17
+ end
18
+
19
+ def test_no_command
20
+ flexmock(@player).should_receive(:name).once.and_return("Fake Player")
21
+ flexmock($stderr).should_receive(:puts).once.with(USAGE)
22
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once.with(1)
23
+ Anyplayer::CommandLine.parse(%w())
24
+ end
25
+
26
+ def test_wrong_command
27
+ flexmock(@player).should_receive(:name).once.and_return("Fake Player")
28
+ flexmock($stderr).should_receive(:puts).once.with(USAGE)
29
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once.with(1)
30
+ Anyplayer::CommandLine.parse(%w(err))
31
+ end
32
+
33
+ def test_verbose
34
+ # flexmock(@player).should_receive(:name).once.and_return("Foo")
35
+ flexmock(@player).should_receive(:play).once
36
+ flexmock(@selector).should_receive(:verbose=).once.with(true)
37
+ flexmock($stderr).should_receive(:puts).once.with(/^anyplayer v[0-9.]+$/)
38
+ Anyplayer::CommandLine.parse(%w(-v play))
39
+ end
40
+
41
+ # Actions
42
+
43
+ def test_playpause
44
+ flexmock(@player).should_receive(:playpause).once
45
+ Anyplayer::CommandLine.parse(%w(playpause))
46
+ end
47
+
48
+ def test_play
49
+ flexmock(@player).should_receive(:play).once
50
+ Anyplayer::CommandLine.parse(%w(play))
51
+ end
52
+
53
+ def test_pause
54
+ flexmock(@player).should_receive(:pause).once
55
+ Anyplayer::CommandLine.parse(%w(pause))
56
+ end
57
+
58
+ def test_next
59
+ flexmock(@player).should_receive(:next).once
60
+ Anyplayer::CommandLine.parse(%w(next))
61
+ end
62
+
63
+ def test_prev
64
+ flexmock(@player).should_receive(:prev).once
65
+ Anyplayer::CommandLine.parse(%w(prev))
66
+ end
67
+
68
+ def test_voldown
69
+ flexmock(@player).should_receive(:voldown).once
70
+ Anyplayer::CommandLine.parse(%w(voldown))
71
+ end
72
+
73
+ def test_volup
74
+ flexmock(@player).should_receive(:volup).once
75
+ Anyplayer::CommandLine.parse(%w(volup))
76
+ end
77
+
78
+ def test_vote
79
+ flexmock(@player).should_receive(:vote).once
80
+ Anyplayer::CommandLine.parse(%w(vote))
81
+ end
82
+
83
+ def test_volume
84
+ flexmock(@player).should_receive(:volume).once.and_return(42)
85
+ flexmock($stdout).should_receive(:puts).once.with(42)
86
+ Anyplayer::CommandLine.parse(%w(volume))
87
+ end
88
+
89
+ def test_track
90
+ flexmock(@player).should_receive(:track).once.and_return("Foo")
91
+ flexmock($stdout).should_receive(:puts).once.with("Foo")
92
+ Anyplayer::CommandLine.parse(%w(track))
93
+ end
94
+
95
+ def test_artist
96
+ flexmock(@player).should_receive(:artist).once.and_return("Foo")
97
+ flexmock($stdout).should_receive(:puts).once.with("Foo")
98
+ Anyplayer::CommandLine.parse(%w(artist))
99
+ end
100
+
101
+ def test_album
102
+ flexmock(@player).should_receive(:album).once.and_return("Foo")
103
+ flexmock($stdout).should_receive(:puts).once.with("Foo")
104
+ Anyplayer::CommandLine.parse(%w(album))
105
+ end
106
+
107
+ def test_name
108
+ flexmock(@player).should_receive(:name).once.and_return("Foo")
109
+ flexmock($stdout).should_receive(:puts).once.with("Foo")
110
+ Anyplayer::CommandLine.parse(%w(name))
111
+ end
112
+
113
+ # Booleans
114
+
115
+ def test_launched_true
116
+ flexmock(Anyplayer::CommandLine).should_receive(:exit) # .with(1)
117
+ flexmock(@player).should_receive(:launched?).once.and_return(true)
118
+ Anyplayer::CommandLine.parse(%w(launched))
119
+ end
120
+
121
+ def test_launched_false
122
+ flexmock(Anyplayer::CommandLine).should_receive(:exit) # .with(0)
123
+ flexmock(@player).should_receive(:launched?).once.and_return(false)
124
+ Anyplayer::CommandLine.parse(%w(launched))
125
+ end
126
+
127
+ def test_playing_true
128
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once # .with(1)
129
+ flexmock(@player).should_receive(:playing?).once.and_return(true)
130
+ Anyplayer::CommandLine.parse(%w(playing))
131
+ end
132
+
133
+ def test_playing_false
134
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once # .with(0)
135
+ flexmock(@player).should_receive(:playing?).once.and_return(false)
136
+ Anyplayer::CommandLine.parse(%w(playing))
137
+ end
138
+
139
+ def test_paused_true
140
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once # .with(1)
141
+ flexmock(@player).should_receive(:paused?).once.and_return(true)
142
+ Anyplayer::CommandLine.parse(%w(paused))
143
+ end
144
+
145
+ def test_paused_false
146
+ flexmock(Anyplayer::CommandLine).should_receive(:exit).once # .with(0)
147
+ flexmock(@player).should_receive(:paused?).once.and_return(false)
148
+ Anyplayer::CommandLine.parse(%w(paused))
149
+ end
150
+ end
@@ -1,12 +1,51 @@
1
1
  require "test_helper"
2
2
 
3
- class PlayerTest < MiniTest::Unit::TestCase
4
- include FlexMock::TestCase
5
-
3
+ class PlayerTest < Minitest::Test
6
4
  def setup
7
5
  @player = Noplayer.new
8
6
  end
9
7
 
8
+ # Default name
9
+
10
+ def test_default_name
11
+ assert_equal "Noplayer", @player.name
12
+ end
13
+
14
+ def test_default_name_should_remove_namespace
15
+ flexmock(@player).should_receive(:class).and_return("Foo::Bar")
16
+ assert_equal "Bar", @player.name
17
+ end
18
+
19
+ # Default paused?
20
+
21
+ def test_paused_is_false_if_not_playing
22
+ flexmock(@player).should_receive(:playing?).once.and_return(false)
23
+ assert @player.paused?
24
+ end
25
+
26
+ def test_paused_is_true_if_not_paused
27
+ flexmock(@player).should_receive(:playing?).once.and_return(true)
28
+ assert !@player.paused?
29
+ end
30
+
31
+ # Default playpause
32
+
33
+ def test_playpause_toggles_play
34
+ flexmock(@player).should_receive(:paused?).once.and_return(true)
35
+ flexmock(@player).should_receive(:play).once
36
+
37
+ @player.playpause
38
+ end
39
+
40
+ def test_playpause_toggles_pause
41
+ flexmock(@player).should_receive(:paused?).once.and_return(false)
42
+ flexmock(@player).should_receive(:pause).once
43
+
44
+ @player.playpause
45
+ end
46
+
47
+ # Votes
48
+
10
49
  def test_votes
11
50
  Player::DEFAULT_VOTES_TO_SKIP.times do |i|
12
51
  @player.vote
@@ -16,7 +55,7 @@ class PlayerTest < MiniTest::Unit::TestCase
16
55
  assert_equal @player.votes, 0
17
56
  else
18
57
  assert_equal @player.votes, i + 1
19
- end
58
+ end
20
59
  end
21
60
  end
22
61
 
@@ -45,9 +84,7 @@ class PlayerTest < MiniTest::Unit::TestCase
45
84
  assert_equal @player.votes, 0
46
85
  else
47
86
  assert_equal @player.votes, i + 1
48
- end
87
+ end
49
88
  end
50
89
  end
51
-
52
-
53
90
  end
@@ -2,12 +2,10 @@ require "test_helper"
2
2
  require "ruby-mpd"
3
3
  require "anyplayer/players/mpd"
4
4
 
5
- class MpdTest < MiniTest::Unit::TestCase
6
- include FlexMock::TestCase
7
-
5
+ class MpdTest < Minitest::Test
8
6
  def setup
9
7
  @mpd_instance_mock = flexmock("mpd")
10
- flexmock(MPD, "MPD", :new => @mpd_instance_mock)
8
+ flexmock(MPD, "MPD", new: @mpd_instance_mock)
11
9
  end
12
10
 
13
11
  def test_uses_ruby_mpd_gem
@@ -1,6 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
- class AnyplayerTest < MiniTest::Unit::TestCase
3
+ class AnyplayerTest < Minitest::Test
4
4
  def test_player
5
5
  selector = Selector.new
6
6
  player = selector.player
@@ -19,7 +19,7 @@ class AnyplayerTest < MiniTest::Unit::TestCase
19
19
  selector = Selector.new
20
20
  selector.verbose = true
21
21
 
22
- assert_output nil, "Loaded noplayer\nNoplayer launched?\n" do
22
+ assert_output nil, "noplayer:\n loaded\n launched\n" do
23
23
  selector.player
24
24
  end
25
25
  end
@@ -1,6 +1,5 @@
1
- require "minitest/unit"
2
1
  require "minitest/autorun"
3
- require "flexmock/test_unit"
2
+ require "flexmock/minitest"
4
3
  require "anyplayer"
5
4
 
6
5
  require "anyplayer/players/noplayer"
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyplayer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
5
- prerelease:
4
+ version: 1.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sunny Ripert
@@ -10,54 +9,62 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-12-18 00:00:00.000000000 Z
12
+ date: 2020-12-19 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: ruby-mpd
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ~>
18
+ - - "~>"
21
19
  - !ruby/object:Gem::Version
22
20
  version: 0.3.0
23
21
  type: :runtime
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
- - - ~>
25
+ - - "~>"
29
26
  - !ruby/object:Gem::Version
30
27
  version: 0.3.0
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: minitest
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
- - - ! '>='
32
+ - - ">="
37
33
  - !ruby/object:Gem::Version
38
34
  version: '0'
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
- - - ! '>='
39
+ - - ">="
45
40
  - !ruby/object:Gem::Version
46
41
  version: '0'
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: flexmock
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
- - - ! '>='
46
+ - - ">="
53
47
  - !ruby/object:Gem::Version
54
48
  version: '0'
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
- - - ! '>='
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
61
68
  - !ruby/object:Gem::Version
62
69
  version: '0'
63
70
  description: Play/pause/skip songs in iTunes Mac, iTunes Windows, Spotify Mac, Rdio
@@ -70,16 +77,18 @@ executables:
70
77
  extensions: []
71
78
  extra_rdoc_files: []
72
79
  files:
73
- - .envrc
74
- - .gitignore
75
- - .travis.yml
80
+ - ".gitignore"
81
+ - ".ruby-version"
82
+ - ".travis.yml"
83
+ - CHANGELOG.md
84
+ - CONTRIBUTING.md
76
85
  - Gemfile
77
- - Gemfile.lock
78
86
  - MIT-LICENSE
79
87
  - README.md
80
88
  - Rakefile
81
89
  - anyplayer.gemspec
82
90
  - bin/anyplayer
91
+ - bin/rake
83
92
  - lib/anyplayer.rb
84
93
  - lib/anyplayer/command_line.rb
85
94
  - lib/anyplayer/player.rb
@@ -95,6 +104,7 @@ files:
95
104
  - lib/anyplayer/selector.rb
96
105
  - lib/anyplayer/version.rb
97
106
  - test/anyplayer_test.rb
107
+ - test/command_line_test.rb
98
108
  - test/player_test.rb
99
109
  - test/players/mpd_test.rb
100
110
  - test/selector_test.rb
@@ -102,36 +112,29 @@ files:
102
112
  homepage: http://github.com/sunny/anyplayer
103
113
  licenses:
104
114
  - MIT
115
+ metadata: {}
105
116
  post_install_message:
106
117
  rdoc_options: []
107
118
  require_paths:
108
119
  - lib
109
120
  required_ruby_version: !ruby/object:Gem::Requirement
110
- none: false
111
121
  requirements:
112
- - - ! '>='
122
+ - - ">="
113
123
  - !ruby/object:Gem::Version
114
124
  version: '0'
115
- segments:
116
- - 0
117
- hash: -4308262987889258544
118
125
  required_rubygems_version: !ruby/object:Gem::Requirement
119
- none: false
120
126
  requirements:
121
- - - ! '>='
127
+ - - ">="
122
128
  - !ruby/object:Gem::Version
123
129
  version: '0'
124
- segments:
125
- - 0
126
- hash: -4308262987889258544
127
130
  requirements: []
128
- rubyforge_project:
129
- rubygems_version: 1.8.23
131
+ rubygems_version: 3.1.2
130
132
  signing_key:
131
- specification_version: 3
133
+ specification_version: 4
132
134
  summary: Interact with the running music player
133
135
  test_files:
134
136
  - test/anyplayer_test.rb
137
+ - test/command_line_test.rb
135
138
  - test/player_test.rb
136
139
  - test/players/mpd_test.rb
137
140
  - test/selector_test.rb