lib/imuze/get_music.rb in imuzer-0.0.2 vs lib/imuze/get_music.rb in imuzer-0.0.3
- old
+ new
@@ -1,7 +1,7 @@
module Imuze
- class GetMusic < Struct.new(:token, :mp3_uri)
+ class GetMusic < Struct.new(:token, :mp3_uri, :options)
include Methadone::CLILogging
include Methadone::SH
require 'uri'
require 'net/http'
require 'json'
@@ -9,17 +9,34 @@
def self.call(*args)
new(*args).call
end
def call
+ download? ? command_download : command_play
+ end
+
+ private
+
+ def command_play
+ command = %Q(curl "http:#{mp3_uri}" -s -H "Content-Type: application/json" -H "Authorization: #{token}" | mpg123 - 2> /dev/null)
sh command do |stdout, stderr|
puts 'We hope you liked it !'
end
end
- private
+ def command_download
+ command = %Q(wget "http:#{mp3_uri}" -O #{mp3_id}.mp3)
+ sh command do |stdout, stderr|
+ puts 'We hope you liked it !'
+ end
+ end
- def command
- %Q(curl "http:#{mp3_uri}" -s -H "Content-Type: application/json" -H "Authorization: #{token}" | mpg123 - 2> /dev/null)
+ def download?
+ options.nil? ? false : options[:download]
+ end
+
+ def mp3_id
+ puts mp3_uri.split('/')
+ mp3_uri.split('/')[4]
end
end
end