twilio_recordings 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/twilio_recordings.rb +48 -13
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8033bae6057d03b97e9b48a9c1ef77c5a1f6405c
         | 
| 4 | 
            +
              data.tar.gz: cdad125449613cf7daa2d6f17841dbea0e1d3a13
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: b2d7c6c5a3563262504ca0959bf697c5a6b3c7d084556670a727307849a5e4c0c24485ff025ea5350ef8e06f7b2ffbbb9424084432a8c4a106154567c977985f
         | 
| 7 | 
            +
              data.tar.gz: 999c7b8fbf38e64e599a66721349ad4728727a4ab39dbe4eb248bbad427f8949281879bd70b9bf631a3f9148e0d25a25ee660b873500dcc008c8d1bbb655df00
         | 
    
        data/lib/twilio_recordings.rb
    CHANGED
    
    | @@ -19,7 +19,8 @@ class TwilioRecordings | |
| 19 19 | 
             
                @account_sid = account_sid
         | 
| 20 20 | 
             
                @recording_sids = recording_sids.map{ |sid| self.class.sanitize(sid) }
         | 
| 21 21 | 
             
                @tmp_dir = options[:tmp_dir] || File.join('','tmp')
         | 
| 22 | 
            -
             | 
| 22 | 
            +
             | 
| 23 | 
            +
                @tmp_files = {}
         | 
| 23 24 | 
             
              end
         | 
| 24 25 |  | 
| 25 26 | 
             
              ##
         | 
| @@ -39,7 +40,8 @@ class TwilioRecordings | |
| 39 40 | 
             
              #   >> t.twilio_urls
         | 
| 40 41 | 
             
              #   => ["./my_tmp_dir/RE12345678901234567890123456789012.mp3"]
         | 
| 41 42 | 
             
              def filenames
         | 
| 42 | 
            -
                 | 
| 43 | 
            +
                init_tmp_files
         | 
| 44 | 
            +
                @recording_sids.map { |sid| @tmp_files[sid].path }
         | 
| 43 45 | 
             
              end
         | 
| 44 46 |  | 
| 45 47 | 
             
              ##
         | 
| @@ -56,11 +58,6 @@ class TwilioRecordings | |
| 56 58 | 
             
                end
         | 
| 57 59 | 
             
              end
         | 
| 58 60 |  | 
| 59 | 
            -
              def download_and_join
         | 
| 60 | 
            -
                download
         | 
| 61 | 
            -
                join
         | 
| 62 | 
            -
              end
         | 
| 63 | 
            -
             | 
| 64 61 | 
             
              ##
         | 
| 65 62 | 
             
              # Download the recordings to @tmp_dir
         | 
| 66 63 | 
             
              #
         | 
| @@ -70,16 +67,20 @@ class TwilioRecordings | |
| 70 67 | 
             
              #   >> t.download
         | 
| 71 68 | 
             
              #   => #<TwilioRecordings:0x00... >
         | 
| 72 69 | 
             
              def download
         | 
| 70 | 
            +
                recording_downloads = {}
         | 
| 73 71 | 
             
                # download the recordings
         | 
| 74 72 | 
             
                connection.in_parallel do
         | 
| 75 | 
            -
                  twilio_urls.zip( | 
| 76 | 
            -
                     | 
| 73 | 
            +
                  twilio_urls.zip(@recording_sids).each do |url, sid|
         | 
| 74 | 
            +
                    recording_downloads[sid] = connection.get(url)
         | 
| 77 75 | 
             
                  end
         | 
| 78 76 | 
             
                end
         | 
| 79 77 |  | 
| 78 | 
            +
                init_tmp_files
         | 
| 79 | 
            +
             | 
| 80 80 | 
             
                # write the recordings to file
         | 
| 81 | 
            -
                 | 
| 82 | 
            -
                   | 
| 81 | 
            +
                recording_downloads.each do |sid, response|
         | 
| 82 | 
            +
                  @tmp_files[sid].write(response.body)
         | 
| 83 | 
            +
                  @tmp_files[sid].close
         | 
| 83 84 | 
             
                end
         | 
| 84 85 |  | 
| 85 86 | 
             
                self
         | 
| @@ -95,11 +96,27 @@ class TwilioRecordings | |
| 95 96 | 
             
              # Arguments:
         | 
| 96 97 | 
             
              #   output: A string to the filename that the output should be written to. (optional, default is '/tmp/joined_{recording_ids}')
         | 
| 97 98 | 
             
              def join(output=nil)
         | 
| 98 | 
            -
                output | 
| 99 | 
            +
                unless output
         | 
| 100 | 
            +
                  output_file = Tempfile.new(['joined_recording','.mp3'], @tmp_dir)
         | 
| 101 | 
            +
                  output_file.binmode
         | 
| 102 | 
            +
                  output_file.close
         | 
| 103 | 
            +
                  output = output_file.path
         | 
| 104 | 
            +
                end
         | 
| 99 105 | 
             
                `cat #{filenames.join(" ")} > #{output}`
         | 
| 106 | 
            +
                cleanup
         | 
| 100 107 | 
             
                output
         | 
| 101 108 | 
             
              end
         | 
| 102 109 |  | 
| 110 | 
            +
              ##
         | 
| 111 | 
            +
              # Clean up the temporarily downloaded recordings.
         | 
| 112 | 
            +
              #
         | 
| 113 | 
            +
              # Example:
         | 
| 114 | 
            +
              #   >> t.cleanup
         | 
| 115 | 
            +
              #   => true
         | 
| 116 | 
            +
              def cleanup
         | 
| 117 | 
            +
                remove_tmp_files
         | 
| 118 | 
            +
              end
         | 
| 119 | 
            +
             | 
| 103 120 | 
             
              ##
         | 
| 104 121 | 
             
              # Sanitize the filename.
         | 
| 105 122 | 
             
              #
         | 
| @@ -110,4 +127,22 @@ class TwilioRecordings | |
| 110 127 | 
             
              def self.sanitize(filename)
         | 
| 111 128 | 
             
                filename.gsub(/[^a-zA-Z0-9]/, '')
         | 
| 112 129 | 
             
              end
         | 
| 113 | 
            -
             | 
| 130 | 
            +
             | 
| 131 | 
            +
              private
         | 
| 132 | 
            +
             | 
| 133 | 
            +
              def init_tmp_files
         | 
| 134 | 
            +
                return unless @tmp_files == {}
         | 
| 135 | 
            +
                @recording_sids.each do |sid|
         | 
| 136 | 
            +
                  @tmp_files[sid] = Tempfile.new([sid,'.mp3'], @tmp_dir)
         | 
| 137 | 
            +
                  @tmp_files[sid].binmode
         | 
| 138 | 
            +
                end
         | 
| 139 | 
            +
              end
         | 
| 140 | 
            +
             | 
| 141 | 
            +
              def remove_tmp_files
         | 
| 142 | 
            +
                return if @tmp_files == {}
         | 
| 143 | 
            +
                @tmp_files.values.each do |file|
         | 
| 144 | 
            +
                  file.unlink
         | 
| 145 | 
            +
                end
         | 
| 146 | 
            +
                @tmp_files = {}
         | 
| 147 | 
            +
              end
         | 
| 148 | 
            +
            end
         |