Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to return API response or download labels on checkout #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 10 additions & 6 deletions lib/posthorn/office.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Office
attr_reader :user
attr_accessor :cart

DEFAULT_PAGE_FORMAT = 25 # Brother 62mm

def initialize
@client = SoapClient.new
@cart = []
Expand All @@ -27,12 +29,10 @@ def balance
cents
end

def checkout!(args = {})
page = 0

def checkout!(page_format: DEFAULT_PAGE_FORMAT, download_labels: true)
message = {
userToken: user.user_token,
pageFormatId: args[:page_format] || 25, # Brother 62mm
pageFormatId: page_format,
positions: @cart.map.with_index { |label, ix| label.to_h(page: ix) },
total: balance
}
Expand All @@ -43,12 +43,16 @@ def checkout!(args = {})

@cart.clear

return data unless download_labels

download_labels(data)
end

def download_labels(data)
request = HTTPI::Request.new
request.url = data[:link]
request.read_timeout = 240
HTTPI.get(request).body
end

end
end
end
69 changes: 45 additions & 24 deletions spec/office_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,54 @@
expect(office.user.wallet_balance).to be > 0
end

it 'should order national post stamps' do
office = Office.new
describe 'checkout!' do
let(:office) { Office.new }
let(:receiver) do
Address.new(
company: 'Hans Peter Wurst Inc.',
firstname: 'Hans Peter',
lastname: 'Wurst',
street: 'Schoolstraat',
no: '2',
city: 'Lemiers',
zip: '6295',
country: 'de'
)
end
let(:sender) do
Address.new(
company: 'AISLER B.V.',
street: 'Schoolstraat',
no: '2',
city: 'Lemiers',
zip: '6295AV',
country: 'nl'
)
end

receiver = Address.new(
company: 'Hans Peter Wurst Inc.',
firstname: 'Hans Peter',
lastname: 'Wurst',
street: 'Schoolstraat',
no: '2',
city: 'Lemiers',
zip: '6295',
country: 'de')
before do
office.cart << Label.new(sender, receiver, product_id: 21, price: 145)
end

sender = Address.new(
company: 'AISLER B.V.',
street: 'Schoolstraat',
no: '2',
city: 'Lemiers',
zip: '6295AV',
country: 'nl')
context 'when downloading labels' do
let(:response) { office.checkout!(page_format: 25) }

office.cart << Label.new(sender, receiver, product_id: 21, price: 145)
expect(office.cart.length).to eq(1)
it 'has one item in the cart' do
expect(office.cart.length).to eq(1)
end

it 'should order national post stamps' do
expect(response.length).to be >= 100
end
end

labels = office.checkout!(page_format: 25)
expect(labels.length).to be >= 100
context 'when not downloading labels' do
let(:response) { office.checkout!(page_format: 25, download_labels: false) }

it 'returns the response of the API' do
expect(response[:link]).to be_a(String)
end
end
end

it 'should return cart\' balance' do
Expand Down Expand Up @@ -72,5 +94,4 @@

expect(office.balance).to eq(3 * 145)
end

end
end