centaman 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ require 'timeout'
2
+
3
+ module Centaman
4
+ #:nodoc:
5
+ class Service < Wrapper
6
+ DEFAULT_TIMEOUT_TIME = 15
7
+
8
+ def after_init(args)
9
+ # overwritten by subclasses
10
+ end
11
+
12
+ def wrap_request_in_case_of_timeout(proc, options = {})
13
+ time = options.fetch(:timeout_time, DEFAULT_TIMEOUT_TIME)
14
+ resp = nil
15
+ begin
16
+ resp = Timeout.timeout(time) do
17
+ proc.call
18
+ end
19
+ rescue Timeout::Error
20
+ raise Exceptions::CentamanTimeout
21
+ end
22
+ resp
23
+ end
24
+
25
+ def fetch_all
26
+ @get_request ||= begin
27
+ req = Proc.new do
28
+ self.class.get(endpoint, payload(:get))
29
+ end
30
+ resp = wrap_request_in_case_of_timeout(req, timeout_time: 20)
31
+
32
+ raise resp['Message'] if resp.is_a?(Hash)
33
+ resp
34
+ end
35
+ end
36
+
37
+ def post
38
+ @post_request ||= begin
39
+ req = Proc.new do
40
+ self.class.post(endpoint, payload(:post))
41
+ end
42
+ resp = wrap_request_in_case_of_timeout(req)
43
+ self.respond_to?(:build_object) ? after_post(resp) : resp
44
+ end
45
+ end
46
+
47
+ def after_post(response)
48
+ build_object(response)
49
+ end
50
+
51
+ def payload(request_type = :get)
52
+ hash = { payload_key(request_type) => options_hash }
53
+ hash.merge(headers: headers)
54
+ end
55
+
56
+ def payload_key(request_type)
57
+ request_type == :get ? :query : :body
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,50 @@
1
+ module Centaman
2
+ class Service::BookingTime < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ attr_reader :booking_type_id, :start_date, :end_date, :timed_ticket_type_id
5
+
6
+ def after_init(args)
7
+ @booking_type_id = args[:booking_type_id]
8
+ @start_date = args[:start_date]
9
+ @end_date = args[:end_date]
10
+ parse_dates
11
+ @timed_ticket_type_id = args[:id] # when finding a particular time
12
+ end
13
+
14
+ def object_class
15
+ Centaman::Object::BookingTime
16
+ end
17
+
18
+ def self.find(booking_type_id, booking_time_id, date)
19
+ obj = new(
20
+ booking_type_id: booking_type_id,
21
+ booking_time_id: booking_time_id,
22
+ start_date: date,
23
+ end_date: date
24
+ )
25
+ obj.objects.detect {|obj| obj.id == booking_time_id }
26
+ end
27
+
28
+ def endpoint
29
+ '/ticket_services/TimedTicketType'
30
+ end
31
+
32
+ def options
33
+ super + [
34
+ { key: 'BookingTypeId', value: booking_type_id },
35
+ { key: 'StartDate', value: start_date },
36
+ { key: 'EndDate', value: end_date },
37
+ { key: 'id', value: timed_ticket_type_id },
38
+ ]
39
+ end
40
+
41
+ def parse_dates
42
+ @start_date = start_date.present? && start_date.is_a?(Date) ? format_date_to_string(start_date) : start_date
43
+ @end_date = end_date.present? && end_date.is_a?(Date) ? format_date_to_string(end_date) : end_date
44
+ end
45
+
46
+ def format_date_to_string(date)
47
+ date.strftime('%Y-%m-%d')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ module Centaman
2
+ class Service::BookingType < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ def endpoint
5
+ '/ticket_services/TimedTicket'
6
+ end
7
+
8
+ def object_class
9
+ Centaman::Object::BookingType
10
+ end
11
+
12
+ def all_booking_type
13
+ object_class.new({
14
+ 'BookingTypeId' => 0,
15
+ 'BookingDescription' => 'All Booking Types'
16
+ })
17
+ end
18
+
19
+ def self.find(booking_type_id, date)
20
+ obj = new(
21
+ start_date: date,
22
+ end_date: date
23
+ )
24
+ obj.objects.detect {|obj| obj.booking_type_id == booking_type_id }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module Centaman
2
+ class Service::Capacity < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ attr_reader :booking_time_id, :start_date
5
+
6
+ def after_init(args)
7
+ @booking_time_id = args[:booking_time_id]
8
+ @start_date = args[:start_date]
9
+ require_args
10
+ end
11
+
12
+ def endpoint
13
+ '/ticket_services/TimedTicketType'
14
+ end
15
+
16
+ def object_class
17
+ Centaman::Object::Capacity
18
+ end
19
+
20
+ def objects
21
+ capacities = super
22
+ capacities.each do |capacity|
23
+ capacity.sold_out = capacity.vacancy <= 0
24
+ end
25
+ capacities
26
+ end
27
+
28
+ def options
29
+ super + [
30
+ { key: 'TimedTicketTypeId', value: booking_time_id },
31
+ { key: 'StartDate', value: start_date }
32
+ ]
33
+ end
34
+
35
+ def require_args
36
+ raise "booking_time_id is required for #{self.class.name}" if booking_time_id.nil?
37
+ raise "start_date is required for #{self.class.name}" if start_date.nil?
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module Centaman
2
+ class Service::CouponCheck < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ attr_reader :coupon_code
5
+
6
+ def after_init(args)
7
+ @coupon_code = args[:coupon_code]
8
+ require_args
9
+ end
10
+
11
+ def endpoint
12
+ '/coupon_services/check'
13
+ end
14
+
15
+ def object_class
16
+ Centaman::Object::CouponCheck
17
+ end
18
+
19
+ def options_hash
20
+ {
21
+ 'CouponCode' => coupon_code,
22
+ 'ProductArea' => 'TimedTickets'
23
+ }.to_json
24
+ end
25
+
26
+ def require_args
27
+ raise "coupon_code is required for #{self.class.name}" if coupon_code.nil?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ module Centaman
2
+ #:nodoc:
3
+ class Service::CreateCustomer < Centaman::Service
4
+ include Centaman::JsonWrapper
5
+ attr_reader :first_name, :last_name, :email, :phone
6
+
7
+ def after_init(args)
8
+ @first_name = args[:first_name]
9
+ @last_name = args[:last_name]
10
+ @email = args[:email]
11
+ @phone = args[:phone]
12
+ end
13
+
14
+ def endpoint
15
+ '/ticket_services/TimedTicket'
16
+ end
17
+
18
+ def object_class
19
+ Centaman::Object::Customer
20
+ end
21
+
22
+ # rubocop:disable Metrics/MethodLength
23
+ def address
24
+ {
25
+ 'Street1' => '',
26
+ 'Street2' => '',
27
+ 'City' => '',
28
+ 'State' => '',
29
+ 'Postalcode' => '',
30
+ 'Country' => '',
31
+ 'HomePhone' => phone,
32
+ 'WorkPhone' => '',
33
+ 'MobilePhone' => ''
34
+ }
35
+ end
36
+ # rubocop:enable Metrics/MethodLength
37
+
38
+ # rubocop:disable Metrics/MethodLength
39
+ def options_hash
40
+ {
41
+ 'FirstName' => first_name,
42
+ 'LastName' => last_name,
43
+ 'Email' => email,
44
+ 'Address' => address
45
+ }.to_json
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ module Centaman
2
+ class Service::Extra < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ attr_reader :booking_time_id
5
+
6
+ def after_init(args)
7
+ @booking_time_id = args[:booking_time_id]
8
+ require_args
9
+ end
10
+
11
+ def endpoint
12
+ '/ticket_services/TimedTicketExtra'
13
+ end
14
+
15
+ def object_class
16
+ Centaman::Object::Extra
17
+ end
18
+
19
+ def options
20
+ super + [
21
+ { key: 'TimedTicketTypeId', value: booking_time_id }
22
+ ]
23
+ end
24
+
25
+ def require_args
26
+ raise "booking_time_id is required for #{self.class.name}" if booking_time_id.nil?
27
+ end
28
+
29
+ def sample_response
30
+ [{
31
+ 'ExtraId' => 581,
32
+ 'ExtraDescription' => 'Bar Package Cocktail Cruises',
33
+ 'ExtraPrice' => 22.05,
34
+ 'DepositPercentage' => 100.0,
35
+ 'IsTaxInclusive' => true, 'TaxPercentage' => 10.25
36
+ }]
37
+ end
38
+
39
+ def objects
40
+ Rails.env.test? ? build_objects(sample_response) : super
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ module Centaman
2
+ class Service::GiftTicket < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ attr_reader :department_id
5
+
6
+ def after_init(args)
7
+ @department_id = args[:department_id] || 100067
8
+ end
9
+
10
+ def endpoint
11
+ '/ticket_services/Ticket'
12
+ end
13
+
14
+ def object_class
15
+ Centaman::Object::GiftTicket
16
+ end
17
+
18
+ def options
19
+ super + [
20
+ { key: 'DepartmentID', value: department_id }
21
+ ]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,88 @@
1
+ module Centaman
2
+ class Service::PurchaseTicket < Centaman::Service
3
+ attr_reader :booking_type, :booking_time, :tickets, :payment_reference,
4
+ :contact, :order_info, :checkout_service
5
+
6
+ def after_init(args)
7
+ @booking_type = args[:booking_type]
8
+ @booking_time = args[:booking_time]
9
+ @tickets = args[:tickets]
10
+ @payment_reference = args[:payment_reference]
11
+ @contact = args[:contact]
12
+ @order_info = args[:order_info]
13
+ @checkout_service = args[:checkout_service]
14
+ end
15
+
16
+ def endpoint
17
+ '/ticket_services/TimedTicketTransaction'
18
+ end
19
+
20
+ def tickets_payload
21
+ calculators.map do |calculator|
22
+ ticket_hash = {
23
+ 'ItemDescription' => calculator.description,
24
+ 'ItemCode' => calculator.id,
25
+ 'Quantity' => calculator.quantity,
26
+ 'ItemCost' => calculator.price_per_after_online_discount,
27
+ 'TotalPaid' => calculator.total_per,
28
+ 'TaxPaid' => calculator.taxes_per,
29
+ 'AttendeeName' => '',
30
+ # 'Barcode' => nil,
31
+ 'IsExtraItem' => calculator.is_extra?,
32
+ 'CouponCode' => order_info.coupon_service.centaman_coupon_code || ''
33
+ }
34
+ ticket_hash
35
+ end
36
+ end
37
+
38
+ def options_hash_no_json
39
+ [
40
+ {
41
+ "Item" => tickets_payload,
42
+ "TimedTicketTypeId" => booking_time.id,
43
+ "TimedTicketTypeDescription" => "#{contact.last_name}, #{contact.first_name}",
44
+ "BookingTypeId" => booking_type.booking_type_id,
45
+ "StartDate" => booking_time.start_date.strftime('%Y-%m-%d'),
46
+ # "StartTime" => booking_time.start_time,
47
+ "EndTime" => booking_time.end_time,
48
+ "PaymentReference" => payment_reference,
49
+ "BookingCost" => total_after_discounts.round(2),
50
+ "TotalPaid" => total_paid.round(2),
51
+ "TaxPaid" => total_taxes.round(2),
52
+ "TransactionDate" => Date.today.strftime('%Y-%m-%d'),
53
+ "BookingContactID" => contact.member_code,
54
+ "TotalTickets" => total_tickets,
55
+ # "BalanceAmount" => nil,
56
+ # "ReceiptNo" => nil,
57
+ # "BookingId" => nil
58
+ }
59
+ ]
60
+ end
61
+
62
+ private
63
+
64
+ def options_hash
65
+ options_hash_no_json.to_json
66
+ end
67
+
68
+ def total_after_discounts
69
+ checkout_service.total_price_after_discounts
70
+ end
71
+
72
+ def total_paid
73
+ checkout_service.total
74
+ end
75
+
76
+ def total_taxes
77
+ checkout_service.total_taxes
78
+ end
79
+
80
+ def total_tickets
81
+ checkout_service.total_tickets
82
+ end
83
+
84
+ def calculators
85
+ checkout_service.ticket_calculators + checkout_service.extra_calculators
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,97 @@
1
+ module Centaman
2
+ class Service::TicketType < Centaman::Service
3
+ include Centaman::JsonWrapper
4
+ DISCOUNT = 0.0
5
+
6
+ attr_reader :booking_time_id
7
+
8
+ def after_init(args)
9
+ @booking_time_id = args[:booking_time_id]
10
+ require_args
11
+ end
12
+
13
+ def endpoint
14
+ '/ticket_services/TimedTicket'
15
+ end
16
+
17
+ def build_objects(resp)
18
+ ticket_types = super(resp)
19
+ # need to trigger the price method to have it returned in json
20
+ ticket_types.each(&:price)
21
+ ticket_types
22
+ end
23
+
24
+ def object_class
25
+ Centaman::Object::TicketType
26
+ end
27
+
28
+ def options
29
+ super + [
30
+ { key: "TimedTicketTypeId", value: booking_time_id }
31
+ ]
32
+ end
33
+
34
+ def objects
35
+ Rails.env.test? ? build_objects(sample_response) : super
36
+ end
37
+
38
+ # rubocop:disable Metrics/MethodLength
39
+ def sample_response
40
+ # when passing a timed_booking_time_id
41
+ [
42
+ {
43
+ 'TicketId'=>504,
44
+ 'TicketDescription'=>' Adult 19 64yrs online w tax 42 56 90m',
45
+ 'TicketPrice'=>42.56,
46
+ 'TicketBookingFee'=>0.0,
47
+ 'TicketFeeItemId'=>501,
48
+ 'DepositPercentage'=>100.0,
49
+ 'IsTaxInclusive'=>true,
50
+ 'TaxPercentage'=>12.0
51
+ },
52
+ {
53
+ 'TicketId'=>505,
54
+ 'TicketDescription'=>' Senior 65yrs online w tax 35 84 90m',
55
+ 'TicketPrice'=>35.84,
56
+ 'TicketBookingFee'=>0.0,
57
+ 'TicketFeeItemId'=>501,
58
+ 'DepositPercentage'=>100.0,
59
+ 'IsTaxInclusive'=>true,
60
+ 'TaxPercentage'=>12.0},
61
+ {
62
+ 'TicketId'=>506,
63
+ 'TicketDescription'=>' Youth 7 18yrs online w tax 17 92 90m',
64
+ 'TicketPrice'=>17.92,
65
+ 'TicketBookingFee'=>0.0,
66
+ 'TicketFeeItemId'=>501,
67
+ 'DepositPercentage'=>100.0,
68
+ 'IsTaxInclusive'=>true,
69
+ 'TaxPercentage'=>12.0},
70
+ {
71
+ 'TicketId'=>524,
72
+ 'TicketDescription'=>' Child 7years accompanying booking ',
73
+ 'TicketPrice'=>0.0,
74
+ 'TicketBookingFee'=>0.0,
75
+ 'TicketFeeItemId'=>501,
76
+ 'DepositPercentage'=>100.0,
77
+ 'IsTaxInclusive'=>true,
78
+ 'TaxPercentage'=>12.0},
79
+ {
80
+ 'TicketId'=>630,
81
+ 'TicketDescription'=>'Percent Pound Two Adult Ticket Groupon Special ',
82
+ 'TicketPrice'=>85.12,
83
+ 'TicketBookingFee'=>0.0,
84
+ 'TicketFeeItemId'=>0,
85
+ 'DepositPercentage'=>100.0,
86
+ 'IsTaxInclusive'=>true,
87
+ 'TaxPercentage'=>12.0
88
+ }
89
+ ]
90
+ end
91
+ # rubocop:enable Metrics/MethodLength
92
+
93
+ def require_args
94
+ raise "booking_time_id is required for #{self.class.name}" if booking_time_id.nil?
95
+ end
96
+ end
97
+ end