Sha256: d6e587226b7cf4c026fec5b9eca94636e9fa973e516fe2ecba1be8935ff49335

Contents?: true

Size: 935 Bytes

Versions: 1

Compression:

Stored size: 935 Bytes

Contents

module Imuze
  class GetMusic < Struct.new(:token, :mp3_uri, :options)
    include Methadone::CLILogging
    include Methadone::SH
    require 'uri'
    require 'net/http'
    require 'json'

    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

    def command_download
      command = %Q(wget "http:#{mp3_uri}" -O #{mp3_id}.mp3 --header "Authorization: #{token}")
      sh command do |stdout, stderr|
        puts 'We hope you liked it !'
      end
    end

    def download?
      options.nil? ? false : options[:download]
    end

    def mp3_id
      mp3_uri.split('/')[4]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imuzer-0.0.4 lib/imuze/get_music.rb