Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move Rdio deskop methods to DesktopBridge

  • Loading branch information...
commit fcdebdd038711b6b6ea2c72b7fb3b1aca9355c1a 1 parent c55457d
Wynn Netherland authored December 29, 2012
45  lib/rdio.rb
@@ -4,6 +4,7 @@
4 4
 
5 5
 require 'api'
6 6
 require 'rdio/version'
  7
+require 'rdio/desktop_bridge'
7 8
 require 'highline/import'
8 9
 
9 10
 module Rdio
@@ -13,6 +14,10 @@ module Rdio
13 14
 
14 15
   version Rdio::VERSION
15 16
 
  17
+  def self.bridge
  18
+    @bridge ||= Rdio::DesktopBridge.new
  19
+  end
  20
+
16 21
   def self.api
17 22
     token = @access_token ? [@access_token, @access_secret] : nil
18 23
     @api = Api.new \
@@ -48,44 +53,6 @@ def self.write_config
48 53
     end
49 54
   end
50 55
 
51  
-  def self.apple_script(cmd)
52  
-    `osascript -e '#{cmd}'`
53  
-  end
54  
-
55  
-  def self.tell_rdio(cmd)
56  
-    apple_script "tell app \"Rdio\" to #{cmd}"
57  
-  end
58  
-
59  
-  def self.current_track
60  
-    tell_rdio('name of the current track').gsub(/\n/, '')
61  
-  end
62  
-
63  
-  def self.current_artist
64  
-    tell_rdio('artist of the current track').gsub(/\n/, '')
65  
-  end
66  
-
67  
-  def self.current_album
68  
-    tell_rdio('album of the current track').gsub(/\n/, '')
69  
-  end
70  
-
71  
-  def self.display_track(text)
72  
-    text = "Now playing: %{track} / %{artist} / %{album}" if text.nil?
73  
-    say (text % {
74  
-      :artist => current_artist,
75  
-      :track  => current_track,
76  
-      :album  => current_album
77  
-    })
78  
-  end
79  
-
80  
-  def self.set_volume(pct = 30)
81  
-    tell_rdio "set the sound volume to #{pct}"
82  
-  end
83  
-
84  
-  def self.rdio_url(protocol = 'http')
85  
-    path = tell_rdio 'rdio url of the current track'
86  
-    "#{protocol}://www.rdio.com#{path}"
87  
-  end
88  
-
89 56
   def self.current_track_key
90 57
     data = api.call 'getObjectFromUrl', { :url => rdio_url }
91 58
 
@@ -138,7 +105,7 @@ def self.add_to_collection(tracks)
138 105
   skips_pre
139 106
   command :current do |c|
140 107
     c.action do |global_options,options,args|
141  
-      display_track args.first
  108
+      say bridge.now_playing(args.first)
142 109
     end
143 110
   end
144 111
 
43  lib/rdio/desktop_bridge.rb
... ...
@@ -0,0 +1,43 @@
  1
+module Rdio
  2
+  class DesktopBridge
  3
+
  4
+    def apple_script(cmd)
  5
+      `osascript -e '#{cmd}'`
  6
+    end
  7
+
  8
+    def tell_rdio(cmd)
  9
+      apple_script "tell app \"Rdio\" to #{cmd}"
  10
+    end
  11
+
  12
+    def current_track
  13
+      tell_rdio('name of the current track').gsub(/\n/, '')
  14
+    end
  15
+
  16
+    def current_artist
  17
+      tell_rdio('artist of the current track').gsub(/\n/, '')
  18
+    end
  19
+
  20
+    def current_album
  21
+      tell_rdio('album of the current track').gsub(/\n/, '')
  22
+    end
  23
+
  24
+    def now_playing(text=nil)
  25
+      text ||= "Now playing: %{track} / %{artist} / %{album}"
  26
+      text % {
  27
+        :artist => current_artist,
  28
+        :track  => current_track,
  29
+        :album  => current_album
  30
+      }
  31
+    end
  32
+
  33
+    def set_volume(pct = 30)
  34
+      tell_rdio "set the sound volume to #{pct}"
  35
+    end
  36
+
  37
+    def rdio_url(protocol = 'http')
  38
+      path = tell_rdio 'rdio url of the current track'
  39
+      "#{protocol}://www.rdio.com#{path}"
  40
+    end
  41
+
  42
+  end
  43
+end
58  spec/rdio/desktop_bridge_spec.rb
... ...
@@ -0,0 +1,58 @@
  1
+require 'spec_helper'
  2
+
  3
+describe 'Rdio::DesktopBridge' do
  4
+
  5
+  before do
  6
+    @bridge = Rdio::DesktopBridge.new
  7
+  end
  8
+
  9
+  it "sends AppleScript to Rdio.app" do
  10
+    @bridge.should_receive(:apple_script).
  11
+      with("tell app \"Rdio\" to name of the current track")
  12
+
  13
+    @bridge.tell_rdio("name of the current track")
  14
+  end
  15
+
  16
+  it "gets the name of the current track" do
  17
+    @bridge.should_receive(:apple_script).
  18
+      with("tell app \"Rdio\" to name of the current track").
  19
+      and_return("Hurt\n")
  20
+
  21
+    track = @bridge.current_track
  22
+    expect(track).to eq('Hurt')
  23
+  end
  24
+
  25
+  it "gets the name of the current artist" do
  26
+    @bridge.should_receive(:apple_script).
  27
+      with("tell app \"Rdio\" to artist of the current track").
  28
+      and_return("Johnny Cash\n")
  29
+
  30
+    artist = @bridge.current_artist
  31
+    expect(artist).to eq('Johnny Cash')
  32
+  end
  33
+
  34
+  it "gets the name of the current album" do
  35
+    @bridge.should_receive(:apple_script).
  36
+      with("tell app \"Rdio\" to album of the current track").
  37
+      and_return("The Man Comes Around\n")
  38
+
  39
+    album = @bridge.current_album
  40
+    expect(album).to eq('The Man Comes Around')
  41
+  end
  42
+
  43
+  context "displaying now playing info" do
  44
+
  45
+    before do
  46
+      @bridge.stub(:current_track).and_return("Hurt")
  47
+      @bridge.stub(:current_artist).and_return("Johnny Cash")
  48
+      @bridge.stub(:current_album).and_return("The Man Comes Around")
  49
+    end
  50
+
  51
+    it "displays track, artist, and album by default" do
  52
+      expect(@bridge.now_playing).to \
  53
+        eq("Now playing: Hurt / Johnny Cash / The Man Comes Around")
  54
+    end
  55
+
  56
+  end
  57
+
  58
+end
33  spec/rdio_spec.rb
@@ -43,26 +43,29 @@
43 43
     end
44 44
   end
45 45
 
46  
-  it "returns now playing info" do
47  
-    Rdio.stub(:current_artist).and_return 'Johnny Cash'
48  
-    Rdio.stub(:current_track).and_return 'Hurt'
49  
-    Rdio.stub(:current_album).and_return 'The Man Comes Around'
  46
+  context "now playing" do
50 47
 
51  
-    HighLine.any_instance.should_receive(:say).
52  
-      with("Now playing: Hurt / Johnny Cash / The Man Comes Around")
  48
+    before do
  49
+      Rdio::DesktopBridge.any_instance.stub(:current_artist).and_return 'Johnny Cash'
  50
+      Rdio::DesktopBridge.any_instance.stub(:current_track).and_return 'Hurt'
  51
+      Rdio::DesktopBridge.any_instance.stub(:current_album).and_return 'The Man Comes Around'
  52
+    end
53 53
 
54  
-    Rdio.run %w(current)
55  
-  end
56 54
 
57  
-  it "returns custom formatted now playing info" do
58  
-    Rdio.stub(:current_artist).and_return 'Johnny Cash'
59  
-    Rdio.stub(:current_track).and_return 'Hurt'
60  
-    Rdio.stub(:current_album).and_return 'The Man Comes Around'
  55
+    it "returns now playing info" do
  56
+      HighLine.any_instance.should_receive(:say).
  57
+        with("Now playing: Hurt / Johnny Cash / The Man Comes Around")
61 58
 
62  
-    HighLine.any_instance.should_receive(:say).
63  
-      with("Now rocking Hurt by Johnny Cash from the album The Man Comes Around")
  59
+      Rdio.run %w(current)
  60
+    end
  61
+
  62
+    it "returns custom formatted now playing info" do
  63
+      HighLine.any_instance.should_receive(:say).
  64
+        with("Now rocking Hurt by Johnny Cash from the album The Man Comes Around")
  65
+
  66
+      Rdio.run ["current", %Q(Now rocking %{track} by %{artist} from the album %{album})]
  67
+    end
64 68
 
65  
-    Rdio.run ["current", %Q(Now rocking %{track} by %{artist} from the album %{album})]
66 69
   end
67 70
 
68 71
   context "when authenticated" do

0 notes on commit fcdebdd

Please sign in to comment.
Something went wrong with that request. Please try again.