Class: Booker::Models::Model
- Inherits:
-
Object
- Object
- Booker::Models::Model
show all
- Defined in:
- lib/booker/models/model.rb
Direct Known Subclasses
Address, Appointment, AppointmentTreatment, AvailableTime, ClassInstance, Customer, DynamicPrice, Employee, FeatureSettings, ItineraryTimeSlot, ItineraryTimeSlotsList, Location, LocationDaySchedule, MultiServiceAvailabilityResult, NotificationSettings, OnlineBookingSettings, Price, Room, SpaEmployeeAvailabilitySearchItem, TimeZone, Treatment, Type, User
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Model
Returns a new instance of Model.
4
5
6
|
# File 'lib/booker/models/model.rb', line 4
def initialize(options = {})
options.each { |key, value| send(:"#{key}=", value) }
end
|
Class Method Details
.constantize(key) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/booker/models/model.rb', line 58
def self.constantize(key)
begin
Booker::Models.const_get("Booker::Models::#{key.singularize}")
rescue NameError
nil
end
end
|
.from_hash(hash) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/booker/models/model.rb', line 31
def self.from_hash(hash)
model = self.new
hash.each do |key, value|
if model.respond_to?(:"#{key}")
constantized = self.constantize(key)
if constantized
if value.is_a?(Array) && value.first.is_a?(Hash)
model.send(:"#{key}=", constantized.from_list(value))
next
elsif value.is_a? Hash
model.send(:"#{key}=", constantized.from_hash(value))
next
end
end
if value.is_a?(String) && value.start_with?('/Date(')
model.send(:"#{key}=", time_from_booker_datetime(value))
elsif !value.nil?
model.send(:"#{key}=", value)
end
end
end
model
end
|
.from_list(array) ⇒ Object
56
|
# File 'lib/booker/models/model.rb', line 56
def self.from_list(array); array.map { |item| self.from_hash(item) }; end
|
.time_from_booker_datetime(booker_datetime) ⇒ Object
Booker’s API hands you back all time as if the business is in server time. First load the time in server time, then return the same hours and minutes in current time zone. Booker will hopefully fix this in a future API version. Sorry.
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/booker/models/model.rb', line 69
def self.time_from_booker_datetime(booker_datetime)
timestamp = booker_datetime[/\/Date\((.\d+)[\-\+]/, 1].to_i / 1000.to_f
original_tz = Time.zone
begin
Time.zone = Booker::Client::TimeZone
booker_time = Time.zone.at(timestamp)
ensure
Time.zone = original_tz
end
Time.zone.parse(booker_time.strftime('%Y-%m-%d %H:%M:%S'))
end
|
.time_to_booker_datetime(time) ⇒ Object
Booker’s API requires times to be sent in as if the business is in Eastern Time!
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/booker/models/model.rb', line 87
def self.time_to_booker_datetime(time)
original_tz = Time.zone
begin
Time.zone = Booker::Client::TimeZone
timestamp = (Time.zone.parse(time.strftime("%Y-%m-%dT%H:%M:%S")).to_f * 1000).to_i
ensure
Time.zone = original_tz
end
"/Date(#{timestamp})/"
end
|
.timezone_from_booker_offset!(booker_timezone_name) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/booker/models/model.rb', line 116
def self.timezone_from_booker_offset!(booker_timezone_name)
booker_offset_match = booker_timezone_name.scan(/(\A)(.*)(?=\))/).first
if booker_offset_match.present?
booker_offset = booker_offset_match.delete_if { |match| match.blank? }.first
if booker_offset
booker_timezone_map_key = Booker::Helpers::ActiveSupport.booker_timezone_names.find do |key|
key.start_with?(booker_offset)
end
return Booker::Helpers::ActiveSupport.to_active_support(booker_timezone_map_key) if booker_timezone_map_key
end
end
raise Booker::Error
end
|
.timezone_from_booker_timezone(booker_timezone_name) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/booker/models/model.rb', line 101
def self.timezone_from_booker_timezone(booker_timezone_name)
normalized_booker_timezone_name = Booker::Helpers::ActiveSupport.to_active_support(booker_timezone_name)
return normalized_booker_timezone_name if normalized_booker_timezone_name.present?
begin
Booker::Helpers::LoggingHelper.log_issue(
'Unable to find time zone name using Booker::Helpers::ActiveSupport.to_active_support',
booker_timezone_name: booker_timezone_name
)
rescue
end
timezone_from_booker_offset!(booker_timezone_name)
end
|
.to_wday(booker_wday) ⇒ Object
134
|
# File 'lib/booker/models/model.rb', line 134
def self.to_wday(booker_wday); Date.parse(booker_wday).wday; end
|
Instance Method Details
#to_hash ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/booker/models/model.rb', line 8
def to_hash
hash = {}
self.instance_variables.each do |var|
value = self.instance_variable_get var
if value.is_a? Array
new_value = hash_list(value)
elsif value.is_a? Booker::Models::Model
new_value = value.to_hash
elsif value.is_a? Time
new_value = self.class.time_to_booker_datetime(value)
elsif value.is_a? Date
time = value.in_time_zone
new_value = self.class.time_to_booker_datetime(time)
else
new_value = value
end
hash[var[1..-1]] = new_value
end
hash
end
|
#to_json ⇒ Object
29
|
# File 'lib/booker/models/model.rb', line 29
def to_json; Oj.dump(to_hash, mode: :compat); end
|