Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/didww/resource/export.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
require 'forwardable'
require 'down/http'
require 'zlib'
require 'stringio'
require 'didww/callback/const'

module DIDWW
Expand Down Expand Up @@ -52,11 +54,23 @@ def initialize(params = {})
super params.reverse_merge(filters: {})
end

def csv
def download
return unless url.present?
Down::Http.new(headers: { 'Api-Key' => DIDWW::Client.api_key, 'X-DIDWW-API-Version' => DIDWW::Client.api_version }).open(url)
end

def csv
file = download
return unless file
gz = Zlib::GzipReader.new(file)
begin
StringIO.new(gz.read)
ensure
gz.close unless gz.closed?
file.close if file.respond_to?(:close) && !file.closed?
end
end

def complete?
status == STATUS_COMPLETED
end
Expand Down
68 changes: 54 additions & 14 deletions spec/didww/resources/export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,27 @@
end
end

describe '#csv' do
let (:csv_io) { export.csv }
describe '#download' do
let (:download_io) { export.download }

describe 'when file is ready' do
before do
stub_request(:get, export.url).to_return(
status: 200,
body: api_fixture('exports/id/get/csv_download/200', ext: :csv),
headers: { 'Content-Type' => 'text/csv' }
body: File.binread(File.expand_path('../../fixtures/exports/id/get/csv_download/200.csv.gz', __dir__)),
headers: { 'Content-Type' => 'application/octet-stream' }
)
end
it 'does not raise an error' do
expect { export.csv }.to_not raise_error
expect { export.download }.to_not raise_error
end
it 'returs a readable object' do
expect(csv_io).to be_kind_of(Down::ChunkedIO)
expect(csv_io.read).to be_kind_of(String)
it 'returns a readable object' do
expect(download_io).to be_kind_of(Down::ChunkedIO)
expect(download_io.read).to be_kind_of(String)
end
it 'has csv caption' do
expect(csv_io.read).to start_with('Date/Time (UTC),Source,DID,Destination')
it 'returns gzip data' do
magic = download_io.read(2)
expect(magic.bytes).to eq([0x1f, 0x8b])
end
end

Expand All @@ -61,11 +62,11 @@
end
it 'does not call the api' do
WebMock.reset!
expect { export.csv }.to_not raise_error
expect { export.download }.to_not raise_error
expect(a_request(:any, /.*/)).to_not have_been_made
end
it 'returs nil' do
expect(csv_io).to be_nil
it 'returns nil' do
expect(download_io).to be_nil
end
end

Expand All @@ -74,13 +75,52 @@
stub_request(:get, export.url).to_return(status: 404, body: '')
end
it 'raises a NotFound error' do
expect { export.csv }.to raise_error(Down::ClientError)
expect { export.download }.to raise_error(Down::ClientError)
end
end

end
end

describe '#csv' do
let (:export) do
stub_didww_request(:get, "/exports/#{id}").to_return(
status: 200,
body: api_fixture('exports/id/get/without_includes/200'),
headers: json_api_headers
)
client.exports.find(id).first
end

describe 'when file is ready' do
before do
stub_request(:get, export.url).to_return(
status: 200,
body: File.binread(File.expand_path('../../fixtures/exports/id/get/csv_download/200.csv.gz', __dir__)),
headers: { 'Content-Type' => 'application/octet-stream' }
)
end
it 'does not raise an error' do
expect { export.csv }.to_not raise_error
end
it 'returns a StringIO' do
expect(export.csv).to be_kind_of(StringIO)
end
it 'has csv caption' do
expect(export.csv.read).to start_with('Date/Time Start (UTC)')
end
end

describe 'when url is empty' do
before do
export.url = nil
end
it 'returns nil' do
expect(export.csv).to be_nil
end
end
end

context 'when Export does not exist' do
it 'raises a NotFound error' do
stub_didww_request(:get, "/exports/#{id}").to_return(
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/exports/get/without_includes/200.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"export_type": "cdr_in",
"status": "Completed",
"created_at": "2017-06-25T14:56:31.513Z",
"url": "https://sandbox-api.didww.com/v3/exports/a4f6b765-f20c-45c7-98d2-80c2f3a41517.csv"
"url": "https://sandbox-api.didww.com/v3/exports/a4f6b765-f20c-45c7-98d2-80c2f3a41517.csv.gz"
}
},
{
Expand Down
Binary file added spec/fixtures/exports/id/get/csv_download/200.csv.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/fixtures/exports/id/get/without_includes/200.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"export_type": "cdr_in",
"status": "Completed",
"created_at": "2017-06-25T14:56:31.513Z",
"url": "https://sandbox-api.didww.com/v3/exports/a4f6b765-f20c-45c7-98d2-80c2f3a41517.csv"
"url": "https://sandbox-api.didww.com/v3/exports/a4f6b765-f20c-45c7-98d2-80c2f3a41517.csv.gz"
}
}
}