Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Move Rdio deskop methods to DesktopBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Dec 30, 2012
1 parent c55457d commit fcdebdd
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 54 deletions.
45 changes: 6 additions & 39 deletions lib/rdio.rb
Expand Up @@ -4,6 +4,7 @@

require 'api'
require 'rdio/version'
require 'rdio/desktop_bridge'
require 'highline/import'

module Rdio
Expand All @@ -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 \
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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

Expand Down
43 changes: 43 additions & 0 deletions 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
58 changes: 58 additions & 0 deletions 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
33 changes: 18 additions & 15 deletions spec/rdio_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit fcdebdd

Please sign in to comment.