Class: HotelPrice::Rakuten::RakutenAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/hotel_price/rakuten/rakuten_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ RakutenAPI

Returns a new instance of RakutenAPI.



3
4
5
6
7
8
# File 'lib/hotel_price/rakuten/rakuten_api.rb', line 3

def initialize params
  @config = {
    rakuten_hotel_id: params[:rakuten_hotel_id].to_s ||= 0,
    rakuten_api_key: params[:rakuten_api_key] ||= ENV["RT_API_KEY"]
  }
end

Instance Method Details

#get_price(rakuten_hotel_id, checkin_date, num_adults) ⇒ Object



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
# File 'lib/hotel_price/rakuten/rakuten_api.rb', line 10

def get_price(rakuten_hotel_id, checkin_date, num_adults)
  query_string = make_query_string(rakuten_hotel_id, checkin_date.to_s, num_adults)
  url = "https://app.rakuten.co.jp/services/api/Travel/VacantHotelSearch/20131024?#{query_string}"
  json = Net::HTTP.get(URI.parse(url))
  result = JSON.parse(json)
  if result["error"] == "not_found"
    {
      date: Time.now.strftime("%Y-%m-%d"),
      checkin_date: checkin_date,
      rakuten_hotel_id: @config[:rakuten_hotel_id],
      adult_num: num_adults,
      breakfast: "",
      plan_num: 0,
      min_price: 0
    }
  elsif result["error"] == "wrong_parameterd"
    "入力した値が正しくありません。"
  else
    {
      date: DateTime.now.strftime("%Y-%m-%d"),
      checkin_date: checkin_date,
      rakuten_hotel_id: rakuten_hotel_id,
      hotel_name: result["hotels"][0]["hotel"][0]["hotelBasicInfo"]["hotelName"],
      adult_num: num_adults,
      breakfast: "",
      plan_num: result["pagingInfo"]["recordCount"],
      room_name: result["hotels"][0]["hotel"][1]["roomInfo"][0]["roomBasicInfo"]["roomName"],
      plan_name: result["hotels"][0]["hotel"][1]["roomInfo"][0]["roomBasicInfo"]["planName"],
      min_price: result["hotels"][0]["hotel"][1]["roomInfo"][1]["dailyCharge"]["rakutenCharge"]
    }
  end
end

#make_query_string(rakuten_hotel_id, checkin_date, num_adults) ⇒ Object



43
44
45
46
# File 'lib/hotel_price/rakuten/rakuten_api.rb', line 43

def make_query_string(rakuten_hotel_id, checkin_date, num_adults)
  checkout_date = (Date.parse(checkin_date) + 1).strftime("%Y-%m-%d")
  "format=json&sort=%2BroomCharge&searchPattern=1&applicationId=#{@config[:rakuten_api_key]}&hotelNo=#{rakuten_hotel_id}&adultNum=#{num_adults}&checkinDate=#{checkin_date}&checkoutDate=#{checkout_date}&squeezeCondition="
end