twilio_recordings 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/twilio_recordings.rb +48 -13
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b82464745bd864b23b18200a752942e511554f8d
4
- data.tar.gz: 48db032d6c9ace15122eaca0217703c18da0ccd6
3
+ metadata.gz: 8033bae6057d03b97e9b48a9c1ef77c5a1f6405c
4
+ data.tar.gz: cdad125449613cf7daa2d6f17841dbea0e1d3a13
5
5
  SHA512:
6
- metadata.gz: a6ff3f5523953e6452b08ac4130fe0fbb982283252fa0689b75962f2fa4223f1f7cb251e80f75ea3d2ea4afa21ff3b3279319aa36e99b010773cdaa7cd80a5b5
7
- data.tar.gz: 9859f7880ecce0dedaf94d7a05a937780da9dbb436b42da393c8f054080c49729fe4d58d383debbe48becca24ba3fbe8894f5085c762f2d469d9618da5bb839b
6
+ metadata.gz: b2d7c6c5a3563262504ca0959bf697c5a6b3c7d084556670a727307849a5e4c0c24485ff025ea5350ef8e06f7b2ffbbb9424084432a8c4a106154567c977985f
7
+ data.tar.gz: 999c7b8fbf38e64e599a66721349ad4728727a4ab39dbe4eb248bbad427f8949281879bd70b9bf631a3f9148e0d25a25ee660b873500dcc008c8d1bbb655df00
@@ -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
- @recording_downloads = {}
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
- @recording_sids.map { |r| File.join(@tmp_dir, "#{r}.mp3") }
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(filenames).each do |url, filename|
76
- @recording_downloads[filename] = connection.get(url)
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
- @recording_downloads.each do |filename, response|
82
- File.open(filename, 'wb') { |f| f.write(response.body) }
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 ||= File.join(@tmp_dir, "joined_#{@recording_sids.join('_')}.mp3")
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
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twilio_recordings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Boxley