Class: Locomotive::WuBook::AvailableDaysTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
PluginHelper
Defined in:
lib/locomotive/wubook/plugin/available_days_tag.rb

Instance Method Summary collapse

Methods included from PluginHelper

#fetch_room_base_data, #fetch_room_id, #request_room_data

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ AvailableDaysTag

Returns a new instance of AvailableDaysTag.



11
12
13
14
15
16
17
18
# File 'lib/locomotive/wubook/plugin/available_days_tag.rb', line 11

def initialize(tag_name, markup, tokens, context)
  @options = {
    room_ident: ''
  }

  markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
  super
end

Instance Method Details

#render(context) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/locomotive/wubook/plugin/available_days_tag.rb', line 20

def render(context)
  @plugin_obj = context.registers[:plugin_object]
  config = @plugin_obj.config

  returned_string = "["

  # Evaluate variables and use the return of the evaluation if it exists..
  raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
  room_ident_evaluated = context[@options[:room_ident]]
  unless room_ident_evaluated.nil? || room_ident_evaluated.empty? then
    @options[:room_ident] = room_ident_evaluated
  else
    return "[]"
  end
  ::Locomotive.log "**> AvailableDaysTag room_ident: #{@options[:room_ident]}"



  today = Date.today
  last_day = today.next_month(config['months_ahead'].to_i)

  wired = Wired.new(config)
  wired.aquire_token

  room_data = request_room_data(wired, config['lcode'], @options[:room_ident], today, last_day)

  # Create one entry for each day from now to then.. put a 1 if the day is available or 0 if not.
  (today .. last_day).each_with_index do |date, i|
    returned_string += "," if i > 0

    if room_data[i] != nil && room_data[i]['avail'] >= 1 
    then
      returned_string += "1"
    else
      returned_string += "0"  
    end
  end

  wired.release_token

  returned_string + "]"
end

#render_disabled(context) ⇒ Object



63
64
65
# File 'lib/locomotive/wubook/plugin/available_days_tag.rb', line 63

def render_disabled(context)
  "[]"
end