Class: Booker::Models::Model

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
# File 'lib/booker/models/model.rb', line 5

def initialize(options = {})
  options.each do |key, value|
    send(:"#{key}=", value)
  end
end

Class Method Details

.constantize(key) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/booker/models/model.rb', line 65

def self.constantize(key)
  begin
    Booker::Models.const_get("Booker::Models::#{key.singularize}")
  rescue NameError
    nil
  end
end

.from_hash(hash) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/booker/models/model.rb', line 36

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



61
62
63
# File 'lib/booker/models/model.rb', line 61

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/booker/models/model.rb', line 76

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::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!



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/booker/models/model.rb', line 94

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

  begin
    # Booker's server is always EST
    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

Instance Method Details

#to_hashObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/booker/models/model.rb', line 11

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_jsonObject



32
33
34
# File 'lib/booker/models/model.rb', line 32

def to_json
  Oj.dump(to_hash, mode: :compat)
end