forked from duffelhq/duffel-api-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_and_book.rb
More file actions
99 lines (78 loc) · 2.68 KB
/
search_and_book.rb
File metadata and controls
99 lines (78 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
require "duffel_api"
require "bigdecimal"
client = DuffelAPI::Client.new(
access_token: ENV.fetch("DUFFEL_ACCESS_TOKEN"),
)
# 365 days from now
departure_date = (Time.now + (60 * 60 * 24 * 365)).strftime("%Y-%m-%d")
birth_date = Date.parse("1993-04-01")
current_date = Date.today
age = current_date.year - birth_date.year
age += 1 if current_date.yday > birth_date.yday
offer_request = client.offer_requests.create(params: {
cabin_class: "economy",
passengers: [{
age: age,
}],
slices: [{
# We use a non-sensical route to make sure we get speedy, reliable Duffel Airways
# resullts.
origin: "LHR",
destination: "STN",
departure_date: departure_date,
}],
# This attribute is sent as a query parameter rather than in the body like the others.
# Worry not! The library handles this complexity for you.
return_offers: false,
})
puts "Created offer request: #{offer_request.id}"
offers = client.offers.all(params: { offer_request_id: offer_request.id })
puts "Got #{offers.count} offers"
selected_offer = offers.first
puts "Selected offer #{selected_offer.id} to book"
priced_offer = client.offers.get(selected_offer.id,
params: { return_available_services: true })
puts "The final price for offer #{priced_offer.id} is #{priced_offer.total_amount} " \
"#{priced_offer.total_currency}"
available_service = priced_offer.available_services.first
puts "Adding an extra bag with service #{available_service['id']}, " \
"costing #{available_service['total_amount']} #{available_service['total_currency']}"
total_amount = (
BigDecimal(priced_offer.total_amount) + BigDecimal(available_service["total_amount"])
).to_s("F")
order = client.orders.create(params: {
selected_offers: [priced_offer.id],
services: [{
id: available_service["id"],
quantity: 1,
}],
payments: [
{
type: "balance",
amount: total_amount,
currency: priced_offer.total_currency,
},
],
passengers: [
{
id: priced_offer.passengers.first["id"],
title: "mr",
gender: "m",
given_name: "Tim",
family_name: "Rogers",
born_on: birth_date.to_s,
phone_number: "+441290211999",
email: "tim@duffel.com",
},
],
})
puts "Created order #{order.id} with booking reference #{order.booking_reference}"
order_cancellation = client.order_cancellations.create(params: {
order_id: order.id,
})
puts "Requested refund quote for order #{order.id} - " \
"#{order_cancellation.refund_amount} #{order_cancellation.refund_currency} is " \
"available"
client.order_cancellations.confirm(order_cancellation.id)
puts "Confirmed refund quote for order #{order.id}"