hotels_pro 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +91 -0
  8. data/Rakefile +2 -0
  9. data/hotels_pro.gemspec +24 -0
  10. data/lib/hotels_pro.rb +69 -0
  11. data/lib/hotels_pro/api/api_method.rb +29 -0
  12. data/lib/hotels_pro/api/api_params.rb +29 -0
  13. data/lib/hotels_pro/api/elements/book_info.rb +27 -0
  14. data/lib/hotels_pro/api/elements/daily_rate.rb +12 -0
  15. data/lib/hotels_pro/api/elements/filter.rb +13 -0
  16. data/lib/hotels_pro/api/elements/hotel.rb +22 -0
  17. data/lib/hotels_pro/api/elements/lead_traveller.rb +15 -0
  18. data/lib/hotels_pro/api/elements/pax.rb +16 -0
  19. data/lib/hotels_pro/api/elements/pax_array.rb +17 -0
  20. data/lib/hotels_pro/api/elements/policy.rb +15 -0
  21. data/lib/hotels_pro/api/elements/room_response.rb +16 -0
  22. data/lib/hotels_pro/api/methods/allocate_hotel_code.rb +21 -0
  23. data/lib/hotels_pro/api/methods/amend_hotel_booking.rb +27 -0
  24. data/lib/hotels_pro/api/methods/cancel_hotel_booking.rb +20 -0
  25. data/lib/hotels_pro/api/methods/get_available_hotel.rb +28 -0
  26. data/lib/hotels_pro/api/methods/get_balance.rb +18 -0
  27. data/lib/hotels_pro/api/methods/get_hotel_booking_status.rb +21 -0
  28. data/lib/hotels_pro/api/methods/get_hotel_cancellation_policy.rb +19 -0
  29. data/lib/hotels_pro/api/methods/make_hotel_booking.rb +24 -0
  30. data/lib/hotels_pro/configuration.rb +14 -0
  31. data/lib/hotels_pro/query.rb +34 -0
  32. data/lib/hotels_pro/request.rb +43 -0
  33. data/lib/hotels_pro/response.rb +33 -0
  34. data/lib/hotels_pro/stubs.rb +66 -0
  35. data/lib/hotels_pro/stubs/stub.rb +23 -0
  36. data/lib/hotels_pro/underscorer.rb +21 -0
  37. data/lib/hotels_pro/version.rb +3 -0
  38. data/spec/api_params_helper.rb +8 -0
  39. data/spec/fixtures/allocate_hotel_code.json +1 -0
  40. data/spec/fixtures/amend_hotel_booking.json +1 -0
  41. data/spec/fixtures/cancel_hotel_booking.json +1 -0
  42. data/spec/fixtures/get_available_hotel.json +1 -0
  43. data/spec/fixtures/get_balance.json +1 -0
  44. data/spec/fixtures/get_hotel_booking_status.json +1 -0
  45. data/spec/fixtures/get_hotel_cancellation_policy.json +1 -0
  46. data/spec/fixtures/make_hotel_booking.json +1 -0
  47. data/spec/hotels_pro/api/.gitkeep +0 -0
  48. data/spec/hotels_pro/api/api_method_spec.rb +14 -0
  49. data/spec/hotels_pro/api/api_params_spec.rb +36 -0
  50. data/spec/hotels_pro/api/elements/pax_array_spec.rb +14 -0
  51. data/spec/hotels_pro/api/methods/allocate_hotel_code_spec.rb +34 -0
  52. data/spec/hotels_pro/api/methods/amend_hotel_booking_spec.rb +79 -0
  53. data/spec/hotels_pro/api/methods/cancel_hotel_booking_spec.rb +32 -0
  54. data/spec/hotels_pro/api/methods/get_available_hotel_spec.rb +83 -0
  55. data/spec/hotels_pro/api/methods/get_balance_spec.rb +26 -0
  56. data/spec/hotels_pro/api/methods/get_hotel_booking_status_spec.rb +31 -0
  57. data/spec/hotels_pro/api/methods/get_hotel_cancellation_policy_spec.rb +31 -0
  58. data/spec/hotels_pro/api/methods/make_hotel_booking_spec.rb +66 -0
  59. data/spec/hotels_pro/query_spec.rb +52 -0
  60. data/spec/hotels_pro/request_spec.rb +73 -0
  61. data/spec/hotels_pro/response_spec.rb +24 -0
  62. data/spec/hotels_pro/stubs/stub_spec.rb +41 -0
  63. data/spec/hotels_pro/stubs_spec.rb +37 -0
  64. data/spec/hotels_pro/underscorer_spec.rb +33 -0
  65. data/spec/request_helper.rb +6 -0
  66. data/spec/spec_helper.rb +15 -0
  67. metadata +222 -0
@@ -0,0 +1,15 @@
1
+ module HotelsPro
2
+ module Api
3
+ module Elements
4
+ class Policy
5
+ include Virtus
6
+
7
+ attribute :cancellation_day, Integer
8
+ attribute :fee_type, String
9
+ attribute :fee_amount, Float
10
+ attribute :currency, String
11
+ attribute :remarks, String
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'hotels_pro/api/elements/pax'
2
+
3
+ module HotelsPro
4
+ module Api
5
+ module Elements
6
+ class RoomResponse
7
+ include Virtus
8
+
9
+ attribute :room_category, String
10
+ attribute :paxes, Array[Pax]
11
+ attribute :total_room_rate, Float
12
+ attribute :rates_per_night, Array[DailyRate]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'hotels_pro/api/elements/hotel'
2
+
3
+ module HotelsPro
4
+ module Api
5
+ module Methods
6
+ class AllocateHotelCode < ApiMethod
7
+
8
+ attribute :search_id, String
9
+ attribute :hotel_code, String
10
+
11
+ class Result < ApiMethod::Result
12
+ attribute :response_id, Integer
13
+ attribute :search_id, String
14
+ attribute :hotel_code, String
15
+ attribute :available_hotels, Array[Api::Elements::Hotel]
16
+ attribute :total_found, Integer
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'hotels_pro/api/elements/book_info'
2
+ require 'hotels_pro/api/elements/lead_traveller'
3
+ require 'hotels_pro/api/elements/pax_array'
4
+
5
+ module HotelsPro
6
+ module Api
7
+ module Methods
8
+ class AmendHotelBooking < ApiMethod
9
+ attribute :tracking_id, String
10
+ attribute :check_in, Date
11
+ attribute :check_out, Date
12
+ attribute :lead_traveller_info, Api::Elements::LeadTraveller
13
+ attribute :rooms, Array[Api::Elements::PaxArray], :default => []
14
+ attribute :preferences, String
15
+ attribute :note, String
16
+
17
+ class Result < ApiMethod::Result
18
+ attribute :response_id, Integer
19
+ attribute :tracking_id, Integer
20
+ attribute :agency_reference_number, String
21
+ attribute :amend_status, String
22
+ attribute :note, String
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ module HotelsPro
2
+ module Api
3
+ module Methods
4
+ class CancelHotelBooking < ApiMethod
5
+ include ApiParams
6
+ include Virtus
7
+
8
+ attribute :tracking_id, String
9
+
10
+ class Result < ApiMethod::Result
11
+ attribute :response_id, Integer
12
+ attribute :tracking_id, String
13
+ attribute :agency_reference_number, String
14
+ attribute :booking_status, String
15
+ attribute :note, String
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ require 'hotels_pro/api/elements/filter'
2
+ require 'hotels_pro/api/elements/hotel'
3
+ require 'hotels_pro/api/elements/pax_array'
4
+
5
+ module HotelsPro
6
+ module Api
7
+ module Methods
8
+ class GetAvailableHotel < ApiMethod
9
+
10
+ attribute :destination_id, String
11
+ attribute :check_in, Date
12
+ attribute :check_out, Date
13
+ attribute :currency, String, :default => "EUR"
14
+ attribute :client_nationality, String
15
+ attribute :on_request, Boolean, :default => true
16
+ attribute :rooms, Array[Api::Elements::PaxArray], :default => []
17
+ attribute :filters, Array[Api::Elements::Filter], :default => []
18
+
19
+ class Result < ApiMethod::Result
20
+ attribute :response_id, Integer
21
+ attribute :search_id, String
22
+ attribute :total_found, Integer
23
+ attribute :available_hotels, Array[Api::Elements::Hotel]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ module HotelsPro
2
+ module Api
3
+ module Methods
4
+ class GetBalance < ApiMethod
5
+ include ApiParams
6
+ include Virtus
7
+
8
+ class Result < ApiMethod::Result
9
+ attribute :response_id, Integer
10
+ attribute :currency, String
11
+ attribute :total_deposit, Float
12
+ attribute :total_booking_amount, Float
13
+ attribute :current_balance, Float
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'hotels_pro/api/elements/book_info'
2
+
3
+ module HotelsPro
4
+ module Api
5
+ module Methods
6
+ class GetHotelBookingStatus < ApiMethod
7
+ include ApiParams
8
+ include Virtus
9
+
10
+ attribute :tracking_id, String
11
+
12
+ class Result < ApiMethod::Result
13
+ attribute :response_id, Integer
14
+ attribute :tracking_id, String
15
+ attribute :agency_reference_number, String
16
+ attribute :hotel_booking_info, HotelsPro::Api::Elements::BookInfo
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module HotelsPro
2
+ module Api
3
+ module Methods
4
+ class GetHotelCancellationPolicy < ApiMethod
5
+ include ApiParams
6
+ include Virtus
7
+
8
+ attribute :tracking_id, String
9
+
10
+ class Result < ApiMethod::Result
11
+ attribute :response_id, Integer
12
+ attribute :tracking_id, String
13
+ attribute :agency_reference_number, String
14
+ attribute :cancellation_policy, Array[HotelsPro::Api::Elements::Policy], :default => []
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'hotels_pro/api/elements/book_info'
2
+ require 'hotels_pro/api/elements/lead_traveller'
3
+ require 'hotels_pro/api/elements/pax_array'
4
+
5
+ module HotelsPro
6
+ module Api
7
+ module Methods
8
+ class MakeHotelBooking < ApiMethod
9
+ attribute :process_id, String
10
+ attribute :agency_reference_number, String
11
+ attribute :lead_traveller_info, HotelsPro::Api::Elements::LeadTraveller
12
+ attribute :other_traveller_info, Array[Api::Elements::Pax], :default => []
13
+ attribute :preferences, String
14
+ attribute :note, String
15
+
16
+ class Result < ApiMethod::Result
17
+ attribute :response_id, Integer
18
+ attribute :tracking_id, Integer
19
+ attribute :hotel_booking_info, HotelsPro::Api::Elements::BookInfo
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module HotelsPro
2
+ class Configuration
3
+ attr_accessor :environment, :api_key, :logger, :only_stubs
4
+
5
+ def initialize
6
+ @environment = "test"
7
+ end
8
+
9
+ def api_url
10
+ env = ("" if environment == "live") || "_test"
11
+ "http://api.hotelspro.com/4.1#{env}/hotel/b2bHotelJSON.php"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ require 'open-uri'
2
+
3
+ module HotelsPro
4
+ class Query
5
+ def initialize(api_params)
6
+ chunks = api_params.inject([]) do |arr, (k, v)|
7
+ arr << encode(k, v)
8
+ arr
9
+ end
10
+ @query = "?" << chunks.join("&")
11
+ end
12
+
13
+ def to_s
14
+ @query
15
+ end
16
+
17
+ def encode(key, value)
18
+ case value
19
+ when Hash
20
+ chunks = value.map do |k, v|
21
+ encode("#{key}[#{k}]", v)
22
+ end
23
+ chunks.join("&")
24
+ when Array
25
+ chunks = value.each_with_index.map do |v, i|
26
+ encode("#{key}[#{i}]", v)
27
+ end
28
+ chunks.join("&")
29
+ else
30
+ "#{key}=#{URI::encode(value)}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ module HotelsPro
2
+ class Request
3
+ attr_accessor :api_method, :params
4
+
5
+ def initialize(api_method, params={})
6
+ @api_method = api_method.camelize(:lower)
7
+ @params = params
8
+ end
9
+
10
+ def query
11
+ params = @params.dup
12
+ params["method"] = api_method
13
+ params["apiKey"] = HotelsPro.configuration.api_key
14
+
15
+ Query.new(params).to_s
16
+ end
17
+
18
+ def uri
19
+ "#{HotelsPro.configuration.api_url}#{query}"
20
+ end
21
+
22
+ def perform
23
+ HotelsPro.log("Request URL: #{uri}")
24
+
25
+ raw_response = stubbed_response || real_response
26
+ HotelsPro.log("Response: #{raw_response}")
27
+
28
+ Response.new(raw_response)
29
+ end
30
+
31
+ def stubbed_response
32
+ response = HotelsPro::Stubs.match(self)
33
+ if !response && HotelsPro.configuration.only_stubs
34
+ raise HotelsPro::UnstubbedRequest.new("Unstubbed request to URL: #{uri}")
35
+ end
36
+ response
37
+ end
38
+
39
+ def real_response
40
+ Typhoeus::Request.get(uri).body
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ require "hotels_pro/underscorer"
2
+
3
+ module HotelsPro
4
+ class Response
5
+ include Underscorer
6
+
7
+ attr_reader :error_message, :data, :error
8
+
9
+ def initialize(json)
10
+ @data = underscore(body(json))
11
+ detect_error(data)
12
+ end
13
+
14
+ # HotelsPro API returns status 200 OK for errors and includes error message in body as:
15
+ # [0, "error message"]
16
+ def detect_error(data)
17
+ if data.is_a?(Array) && data.size == 2 && data[0] == 0
18
+ @error = ErrorResponse.new(data[1])
19
+ end
20
+ end
21
+
22
+ def error?
23
+ !error.nil?
24
+ end
25
+
26
+ def body(json)
27
+ JSON.parse(json)
28
+ rescue JSON::ParserError
29
+ @error = RemoteError.new("Remote API down.")
30
+ nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,66 @@
1
+ module HotelsPro
2
+ module Stubs
3
+ class << self
4
+ def stubs
5
+ @stubs ||= []
6
+ end
7
+
8
+ def clear
9
+ @stubs = []
10
+ end
11
+
12
+ def get_available_hotel(matcher=nil)
13
+ stub = Stub.new('getAvailableHotel', matcher)
14
+ stubs << stub
15
+ stub
16
+ end
17
+
18
+ def allocate_hotel_code(matcher=nil)
19
+ stub = Stub.new('allocateHotelCode', matcher)
20
+ stubs << stub
21
+ stub
22
+ end
23
+
24
+ def make_hotel_booking(matcher=nil)
25
+ stub = Stub.new('makeHotelBooking', matcher)
26
+ stubs << stub
27
+ stub
28
+ end
29
+
30
+ def get_hotel_booking_status(matcher=nil)
31
+ stub = Stub.new('getHotelBookingStatus', matcher)
32
+ stubs << stub
33
+ stub
34
+ end
35
+
36
+ def cancel_hotel_booking(matcher=nil)
37
+ stub = Stub.new('cancelHotelBooking', matcher)
38
+ stubs << stub
39
+ stub
40
+ end
41
+
42
+ def get_hotel_cancellation_policy(matcher=nil)
43
+ stub = Stub.new('getHotelCancellationPolicy', matcher)
44
+ stubs << stub
45
+ stub
46
+ end
47
+
48
+ def amend_hotel_booking(matcher=nil)
49
+ stub = Stub.new('amendHotelBooking', matcher)
50
+ stubs << stub
51
+ stub
52
+ end
53
+
54
+ def get_balance(matcher=nil)
55
+ stub = Stub.new('getBalance', matcher)
56
+ stubs << stub
57
+ stub
58
+ end
59
+
60
+ def match(request)
61
+ stub = stubs.find { |s| s.matches?(request) }
62
+ stub.response if stub
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ module HotelsPro
2
+ module Stubs
3
+ class Stub
4
+ def initialize(api_method, matcher=nil)
5
+ @api_method = api_method
6
+ @matcher = matcher
7
+ end
8
+
9
+ def response(resp=nil)
10
+ if resp
11
+ @response = resp
12
+ self
13
+ else
14
+ @response
15
+ end
16
+ end
17
+
18
+ def matches?(request)
19
+ request.api_method == @api_method && (@matcher.nil? || @matcher.call(request))
20
+ end
21
+ end
22
+ end
23
+ end