Skip to content

Commit

Permalink
Added spec for Document#get.
Browse files Browse the repository at this point in the history
  • Loading branch information
theabuitendyk committed Jul 26, 2016
1 parent a3e2164 commit e4bd6ee
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
10 changes: 4 additions & 6 deletions lib/dotloop/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ def all(profile_id:, loop_id:)
end
end

def get(profile_id:, loop_id:, document_id:, document_name:, output_path:)
def get(profile_id:, loop_id:, document_id:, document_name:)
document_name = CGI.escape(document_name)
output = StringIO.new(@client.raw("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}/document/#{document_id}/#{document_name}.pdf"))
def output.original_filename
[document_name, '.png'].join
end
output
StringIO.new(
@client.raw("/profile/#{profile_id.to_i}/loop/#{loop_id.to_i}/document/#{document_id}/#{document_name}.pdf")
)
end
end
end
21 changes: 6 additions & 15 deletions spec/dotloop/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,12 @@

describe '#get' do
it 'should get pdf data' do
# see webmock helper dotloop_pdf
document = subject.get(profile_id: 1_234, loop_id: 76_046, document_id:, document_name:, output_path:)
expect(documents).to_not be_empty
expect(documents).to all(be_a(Dotloop::Models::Document))
expect(documents.first).to have_attributes(
created_by: 2_462,
document_id: 561_622,
document_name: 'AgencyDisclosureStatementSeller',
folder_name: 'Folder',
loop_id: 274_231,
last_modified_date: DateTime.parse('2014-08-25T18:33:46-04:00'),
created_date: DateTime.parse('2014-08-25T23:29:31-04:00'),
shared_with: [405_246, 405_247, 405_260],
signature_verfication_link: 'DL5616224301S'
)
dotloop_pdf
document = subject.get(profile_id: 1_234,
loop_id: 76_046,
document_id: 561_622,
document_name: 'AgencyDisclosureStatementSeller')
expect(document.string).to eq(disclosure_file_data)
end
end
end
14 changes: 14 additions & 0 deletions spec/helpers/fixtures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Fixtures
def disclosure_file_data
File.read(
filename_to_path(
'profile/1234/loop/76046/document/561622/AgencyDisclosureStatementSeller.pdf'
)
)
end

def filename_to_path(filename)
dotloop_stub_path = ROOT.join('spec', 'stub_responses')
File.new(dotloop_stub_path.join(filename))
end
end
26 changes: 17 additions & 9 deletions spec/helpers/webmocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Helpers
def dotloop_mock(request)
WebMock.reset!
endpoint = standard_endpoints(request)
data_file = File.new(filename_to_path(endpoint))
data_file = File.new(filename_to_path_json(endpoint))
WebMock
.stub_request(:get, endpoint_to_url(endpoint))
.to_return(body: data_file, status: 200, headers: { 'Content-Type' => 'application/json' })
end

def dotloop_mock_batch(request)
endpoint = standard_endpoints(request)
batch1_file = File.new(filename_to_path([endpoint, '_page1']))
batch2_file = File.new(filename_to_path([endpoint, '_page2']))
batch1_file = File.new(filename_to_path_json([endpoint, '_page1']))
batch2_file = File.new(filename_to_path_json([endpoint, '_page2']))
url = endpoint_to_url(endpoint)
WebMock.reset!
WebMock
Expand All @@ -23,16 +23,24 @@ def dotloop_mock_batch(request)
end

def dotloop_pdf
WebMock
.stub_request(:get, endpoint_to_url('profile/1234/loop/76046/document/561622/'))
.to_return(body: data_file, status: 200, headers: { 'Content-Type' => 'application/json' })
WebMock.stub_request(
:get,
endpoint_to_url(
'profile/1234/loop/76046/document/561622/AgencyDisclosureStatementSeller.pdf'
)
)
.to_return(body: disclosure_file_data,
status: 200,
headers: {
'Content-Type' => 'application/pdf',
'content-disposition' => ['attachment; filename="AgencyDisclosureStatementSeller.pdf"']
})
end

private

def filename_to_path(filenames)
dotloop_stub_path = ROOT.join('spec', 'stub_responses')
File.new(dotloop_stub_path.join([filenames, '.json'].flatten.join))
def filename_to_path_json(filenames)
filename_to_path([filenames, '.json'].flatten.join)
end

def endpoint_to_url(endpoint)
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
require 'pry'
require 'webmock/rspec'
require_relative './helpers/webmocks'
require_relative './helpers/fixtures'

ROOT = Pathname.new(Gem::Specification.find_by_name('dotloop').gem_dir).freeze
require 'dotloop'

WebMock.disable_net_connect!(allow_localhost: true)

RSpec.configure do |conf|
conf.include Fixtures
conf.include Helpers
end
Binary file not shown.

0 comments on commit e4bd6ee

Please sign in to comment.