Class: Locomotive::WuBook::PricesTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
PluginHelper
Defined in:
lib/locomotive/wubook/plugin/prices_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) ⇒ PricesTag

Returns a new instance of PricesTag.



11
12
13
14
15
16
17
18
# File 'lib/locomotive/wubook/plugin/prices_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
62
63
64
65
66
# File 'lib/locomotive/wubook/plugin/prices_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

  base_room_data = fetch_room_base_data(wired, config['lcode'], @options[:room_ident])
  base_price = base_room_data[0]["price"]

  room_data = Rails.cache.fetch(config['lcode'] + @options[:room_ident] + today.to_s + last_day.to_s + "/room_data", expires_in: 1.hours) do 
    ::Locomotive.log "**> Cache fetch for key: #{config['lcode'] + @options[:room_ident] + today.to_s + last_day.to_s + "/room_data"}"
    request_room_data(wired, config['lcode'], @options[:room_ident], today, last_day)
  end

  # 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].has_key?("price")
    then
      returned_string += room_data[i]["price"].to_s
    else
      returned_string += base_price.to_s  
    end
  end

  wired.release_token

  returned_string + "]"
end

#render_disabled(context) ⇒ Object



68
69
70
# File 'lib/locomotive/wubook/plugin/prices_tag.rb', line 68

def render_disabled(context)
  "[]"
end