Module: Rdio
- Extended by:
- GLI::App
- Defined in:
- lib/rdio.rb,
lib/rdio/version.rb,
lib/rdio/desktop_bridge.rb
Defined Under Namespace
Classes: DesktopBridge
Constant Summary collapse
- VERSION =
'1.2.1'
Class Method Summary collapse
- .add_to_collection(tracks) ⇒ Object
- .api ⇒ Object
- .authorize_api ⇒ Object
- .bridge ⇒ Object
- .current_album_track_keys ⇒ Object
- .current_track_key ⇒ Object
- .current_user ⇒ Object
- .lyrics_for(artist, title) ⇒ Object
- .rdio_config ⇒ Object
- .shows_for(artist, count) ⇒ Object
- .write_config ⇒ Object
Class Method Details
.add_to_collection(tracks) ⇒ Object
123 124 125 126 127 |
# File 'lib/rdio.rb', line 123 def self.add_to_collection(tracks) tracks = Array(tracks) api.call 'addToCollection', { :keys => tracks.join(',') } end |
.api ⇒ Object
22 23 24 25 26 27 |
# File 'lib/rdio.rb', line 22 def self.api token = @access_token ? [@access_token, @access_secret] : nil @api = Api.new \ [@consumer_key, @consumer_secret], token end |
.authorize_api ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rdio.rb', line 29 def self. url = api.begin_authentication('oob') ask "Copy the four digit code from your browser. [ENTER] to continue. " Launchy.open url code = ask 'Code: ' @access_token, @access_secret = @api.complete_authentication(code) write_config say "You're all set. see `rdio help` for usage" end |
.bridge ⇒ Object
18 19 20 |
# File 'lib/rdio.rb', line 18 def self.bridge @bridge ||= Rdio::DesktopBridge.new end |
.current_album_track_keys ⇒ Object
116 117 118 119 120 121 |
# File 'lib/rdio.rb', line 116 def self.current_album_track_keys current_album_url = current_user['lastSongPlayed']['albumUrl'] data = api.call 'getObjectFromUrl', { :url => current_album_url } data['result']['trackKeys'] end |
.current_track_key ⇒ Object
110 111 112 113 114 |
# File 'lib/rdio.rb', line 110 def self.current_track_key data = api.call 'getObjectFromUrl', { :url => bridge.current_url } data['result']['key'] end |
.current_user ⇒ Object
106 107 108 |
# File 'lib/rdio.rb', line 106 def self.current_user @current_user ||= api.call('currentUser', :extras => 'lastSongPlayed')['result'] end |
.lyrics_for(artist, title) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/rdio.rb', line 41 def self.lyrics_for(artist, title) uri = URI('http://makeitpersonal.co/lyrics') params = { :artist => artist, :title => title } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) return res.body end |
.rdio_config ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/rdio.rb', line 90 def self.rdio_config { :consumer_key => @consumer_key, :consumer_secret => @consumer_secret, :access_token => @access_token, :access_secret => @access_secret } end |
.shows_for(artist, count) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rdio.rb', line 50 def self.shows_for(artist, count) uri = URI('http://ws.audioscrobbler.com/2.0') params = { :method => 'artist.getEvents', :artist => artist, :limit => count, :format => 'json', :autocorrect => 1, :api_key => '3c3e4b39c2aedcac5d745c70a898ee76' } uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) json = JSON.parse(res.body) return "Sorry, I’ve never heard of #{artist}" if json['error'] == 6 events = json['events']['event'] events = [events] if events.is_a?(Hash) return "No upcoming events for #{artist}" if !events corrected_artist_name = json['events']['@attr']['artist'] cities = [] countries = [] events.each do |event| cities << event['venue']['location']['city'] countries << event['venue']['location']['country'] end longest_city = cities.inject { |a, b| a.length > b.length ? a : b } longest_country = countries.inject { |a, b| a.length > b.length ? a : b } events.map! do |event| location = event['venue']['location'] city = location['city'] city_spaces = (0..longest_city.length - city.length).map{' '}.join('') country = location['country'] country_spaces = (0..longest_country.length - country.length).map{' '}.join('') "#{city}#{city_spaces} #{country}#{country_spaces} #{event['startDate']} #{event['startTime']}" end "Here are #{count} upcoming events for #{corrected_artist_name}\n#{events.join("\n")}\n" end |
.write_config ⇒ Object
99 100 101 102 103 104 |
# File 'lib/rdio.rb', line 99 def self.write_config p = File.join(File.(ENV['HOME']), '.rdio') File.open(p, 'w' ) do |out| YAML.dump rdio_config, out end end |