Permalink
Browse files

Move Rdio deskop methods to DesktopBridge

  • Loading branch information...
1 parent c55457d commit fcdebdd038711b6b6ea2c72b7fb3b1aca9355c1a @pengwynn committed Dec 30, 2012
Showing with 125 additions and 54 deletions.
  1. +6 −39 lib/rdio.rb
  2. +43 −0 lib/rdio/desktop_bridge.rb
  3. +58 −0 spec/rdio/desktop_bridge_spec.rb
  4. +18 −15 spec/rdio_spec.rb
View
45 lib/rdio.rb
@@ -4,6 +4,7 @@
require 'api'
require 'rdio/version'
+require 'rdio/desktop_bridge'
require 'highline/import'
module Rdio
@@ -13,6 +14,10 @@ module Rdio
version Rdio::VERSION
+ def self.bridge
+ @bridge ||= Rdio::DesktopBridge.new
+ end
+
def self.api
token = @access_token ? [@access_token, @access_secret] : nil
@api = Api.new \
@@ -48,44 +53,6 @@ def self.write_config
end
end
- def self.apple_script(cmd)
- `osascript -e '#{cmd}'`
- end
-
- def self.tell_rdio(cmd)
- apple_script "tell app \"Rdio\" to #{cmd}"
- end
-
- def self.current_track
- tell_rdio('name of the current track').gsub(/\n/, '')
- end
-
- def self.current_artist
- tell_rdio('artist of the current track').gsub(/\n/, '')
- end
-
- def self.current_album
- tell_rdio('album of the current track').gsub(/\n/, '')
- end
-
- def self.display_track(text)
- text = "Now playing: %{track} / %{artist} / %{album}" if text.nil?
- say (text % {
- :artist => current_artist,
- :track => current_track,
- :album => current_album
- })
- end
-
- def self.set_volume(pct = 30)
- tell_rdio "set the sound volume to #{pct}"
- end
-
- def self.rdio_url(protocol = 'http')
- path = tell_rdio 'rdio url of the current track'
- "#{protocol}://www.rdio.com#{path}"
- end
-
def self.current_track_key
data = api.call 'getObjectFromUrl', { :url => rdio_url }
@@ -138,7 +105,7 @@ def self.add_to_collection(tracks)
skips_pre
command :current do |c|
c.action do |global_options,options,args|
- display_track args.first
+ say bridge.now_playing(args.first)
end
end
View
43 lib/rdio/desktop_bridge.rb
@@ -0,0 +1,43 @@
+module Rdio
+ class DesktopBridge
+
+ def apple_script(cmd)
+ `osascript -e '#{cmd}'`
+ end
+
+ def tell_rdio(cmd)
+ apple_script "tell app \"Rdio\" to #{cmd}"
+ end
+
+ def current_track
+ tell_rdio('name of the current track').gsub(/\n/, '')
+ end
+
+ def current_artist
+ tell_rdio('artist of the current track').gsub(/\n/, '')
+ end
+
+ def current_album
+ tell_rdio('album of the current track').gsub(/\n/, '')
+ end
+
+ def now_playing(text=nil)
+ text ||= "Now playing: %{track} / %{artist} / %{album}"
+ text % {
+ :artist => current_artist,
+ :track => current_track,
+ :album => current_album
+ }
+ end
+
+ def set_volume(pct = 30)
+ tell_rdio "set the sound volume to #{pct}"
+ end
+
+ def rdio_url(protocol = 'http')
+ path = tell_rdio 'rdio url of the current track'
+ "#{protocol}://www.rdio.com#{path}"
+ end
+
+ end
+end
View
58 spec/rdio/desktop_bridge_spec.rb
@@ -0,0 +1,58 @@
+require 'spec_helper'
+
+describe 'Rdio::DesktopBridge' do
+
+ before do
+ @bridge = Rdio::DesktopBridge.new
+ end
+
+ it "sends AppleScript to Rdio.app" do
+ @bridge.should_receive(:apple_script).
+ with("tell app \"Rdio\" to name of the current track")
+
+ @bridge.tell_rdio("name of the current track")
+ end
+
+ it "gets the name of the current track" do
+ @bridge.should_receive(:apple_script).
+ with("tell app \"Rdio\" to name of the current track").
+ and_return("Hurt\n")
+
+ track = @bridge.current_track
+ expect(track).to eq('Hurt')
+ end
+
+ it "gets the name of the current artist" do
+ @bridge.should_receive(:apple_script).
+ with("tell app \"Rdio\" to artist of the current track").
+ and_return("Johnny Cash\n")
+
+ artist = @bridge.current_artist
+ expect(artist).to eq('Johnny Cash')
+ end
+
+ it "gets the name of the current album" do
+ @bridge.should_receive(:apple_script).
+ with("tell app \"Rdio\" to album of the current track").
+ and_return("The Man Comes Around\n")
+
+ album = @bridge.current_album
+ expect(album).to eq('The Man Comes Around')
+ end
+
+ context "displaying now playing info" do
+
+ before do
+ @bridge.stub(:current_track).and_return("Hurt")
+ @bridge.stub(:current_artist).and_return("Johnny Cash")
+ @bridge.stub(:current_album).and_return("The Man Comes Around")
+ end
+
+ it "displays track, artist, and album by default" do
+ expect(@bridge.now_playing).to \
+ eq("Now playing: Hurt / Johnny Cash / The Man Comes Around")
+ end
+
+ end
+
+end
View
33 spec/rdio_spec.rb
@@ -43,26 +43,29 @@
end
end
- it "returns now playing info" do
- Rdio.stub(:current_artist).and_return 'Johnny Cash'
- Rdio.stub(:current_track).and_return 'Hurt'
- Rdio.stub(:current_album).and_return 'The Man Comes Around'
+ context "now playing" do
- HighLine.any_instance.should_receive(:say).
- with("Now playing: Hurt / Johnny Cash / The Man Comes Around")
+ before do
+ Rdio::DesktopBridge.any_instance.stub(:current_artist).and_return 'Johnny Cash'
+ Rdio::DesktopBridge.any_instance.stub(:current_track).and_return 'Hurt'
+ Rdio::DesktopBridge.any_instance.stub(:current_album).and_return 'The Man Comes Around'
+ end
- Rdio.run %w(current)
- end
- it "returns custom formatted now playing info" do
- Rdio.stub(:current_artist).and_return 'Johnny Cash'
- Rdio.stub(:current_track).and_return 'Hurt'
- Rdio.stub(:current_album).and_return 'The Man Comes Around'
+ it "returns now playing info" do
+ HighLine.any_instance.should_receive(:say).
+ with("Now playing: Hurt / Johnny Cash / The Man Comes Around")
- HighLine.any_instance.should_receive(:say).
- with("Now rocking Hurt by Johnny Cash from the album The Man Comes Around")
+ Rdio.run %w(current)
+ end
+
+ it "returns custom formatted now playing info" do
+ HighLine.any_instance.should_receive(:say).
+ with("Now rocking Hurt by Johnny Cash from the album The Man Comes Around")
+
+ Rdio.run ["current", %Q(Now rocking %{track} by %{artist} from the album %{album})]
+ end
- Rdio.run ["current", %Q(Now rocking %{track} by %{artist} from the album %{album})]
end
context "when authenticated" do

0 comments on commit fcdebdd

Please sign in to comment.