Class: Booker::V4::Models::Model

Inherits:
Model
  • Object
show all
Defined in:
lib/booker/v4/models/model.rb

Constant Summary collapse

CONSTANTIZE_MODULE =
Booker::V4::Models

Class Method Summary collapse

Methods inherited from Model

constantize, from_hash, from_list, #initialize, #to_hash, #to_json

Constructor Details

This class inherits a constructor from Booker::Model

Class Method Details

.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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/booker/v4/models/model.rb', line 9

def self.time_from_booker_datetime(booker_datetime)
  timestamp = booker_datetime[/\/Date\((.\d+)[\-\+]/, 1].to_i / 1000.to_f

  original_tz = Time.zone
  begin
    # Booker's server is always EST
    Time.zone = Booker::Client::BOOKER_SERVER_TIMEZONE

    booker_time = Time.zone.at(timestamp)
  ensure
    Time.zone = original_tz
  end

  # Convert it back to location time without changing hours and minutes
  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!



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/booker/v4/models/model.rb', line 27

def self.time_to_booker_datetime(time)
  original_tz = Time.zone

  begin
    # Booker's server is always EST
    Time.zone = Booker::Client::BOOKER_SERVER_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

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/booker/v4/models/model.rb', line 56

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/booker/v4/models/model.rb', line 41

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



74
# File 'lib/booker/v4/models/model.rb', line 74

def self.to_wday(booker_wday); Date.parse(booker_wday).wday; end