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
|
# File 'lib/hotel_price/agoda/agoda_api.rb', line 5
def self.get_price(hotel_id, checkin_date, num_adults)
@api_key = HotelPrice.configuration ? HotelPrice.configuration.agoda_api_key : nil
if @api_key.nil?
puts "Must specify agoda_api_key in configuration to use the Agoda API"
return -1
end
endpoint_url = "http://affiliateapi7643.agoda.com/affiliateservice/lt_v1"
checkin_date = checkin_date.to_s
Date.parse checkin_date rescue return ""
t = Date.parse(checkin_date)
checkin_arg = t.strftime("%Y-%m-%d")
checkout_arg = (t + 1).strftime("%Y-%m-%d")
params = {
"criteria": {
"additional": {
"currency": "JPY",
"discountOnly": false,
"language": "ja-jp",
"occupancy": {
"numberOfAdult": num_adults,
"numberOfChildren": 0
}
},
"checkInDate": checkin_arg,
"checkOutDate": checkout_arg,
"hotelId": [hotel_id]
}
}
url = URI.parse(endpoint_url)
req = Net::HTTP::Post.new(url.path)
req["Authorization"] = @api_key
req["Content-Type"] = "application/json"
req.body = params.to_json
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
parsed_body = JSON.parse(res.body)
if parsed_body["results"].nil? || parsed_body["results"].empty?
return "No rooms for search criteria - please confirm hotel ID"
end
parsed_body["results"][0]["dailyRate"]
end
|