locomotive_wubook_plugin 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7c5d6c6828d7c446b2a73a2ccd4f1ffb5ff771a6
4
+ data.tar.gz: 78d588cbd959466f7ea3117e2fdd737cff2eef1f
5
+ SHA512:
6
+ metadata.gz: a66f644a2c60f7ff058b7feb2b4e8baac005aa8f53ecedd04f8de413feeed2a4f85eb92c48ac357220ef8946b708f2cb4e49c5ca2708b0ce256000669b9ca4b0
7
+ data.tar.gz: 0a8e3b23488214e9a2633f35bf44af66c62a2ede2f4f24394b3b9b9977b68a33d7a145e44c5839fde0133411a1be34ce8968b0687ed6372550219ada39635cdc
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rspec', '~> 2.12'
6
+ gem 'mocha', '~> 0.13'
7
+ gem 'wubook_wired', '~> 1.0'
@@ -0,0 +1,23 @@
1
+
2
+ %li
3
+ %label{ name: 'account_code' } Account Code:
4
+ %input{ type: 'text', name: 'account_code' }
5
+
6
+ %li
7
+ %label{ name: 'password' } Password:
8
+ %input{ type: 'password', name: 'password' }
9
+
10
+ %li
11
+ %label{ name: 'provider_key' } Provider Key:
12
+ %input{ type: 'text', name: 'provider_key' }
13
+ %p.inline-hints Please make sure that it is exactly as provided.
14
+
15
+ %li
16
+ %label{ name: 'lcode' } Property Identifier:
17
+ %input{ type: 'text', name: 'lcode' }
18
+ %p.inline-hints This is the lcode.
19
+
20
+ %li
21
+ %label{ name: 'months_ahead' } Booking months ahead:
22
+ %input{ type: 'text', name: 'months_ahead' }
23
+ %p.inline-hints How many months in the future is booking allowed.
@@ -0,0 +1,29 @@
1
+ require 'wired'
2
+
3
+ module PluginHelper
4
+ def fetch_room_id(wired, lcode, room_id)
5
+ # Start with finding the room-id for the room with a special name
6
+ rooms = wired.fetch_rooms(lcode)
7
+ filtered_rooms = rooms.select { |room_hash| room_hash['shortname'] == room_id }
8
+ ::Locomotive.log "**> Filtered rooms #{filtered_rooms} "
9
+ raise "Unable to find a room with identifier: #{room_id}" if filtered_rooms.length == 0
10
+ room_identifier = filtered_rooms[0]['id']
11
+ raise "Unable to get the room id." if room_identifier == nil
12
+
13
+ room_identifier
14
+ end
15
+
16
+ def request_room_data(wired, lcode, room_id, startDate, endDate)
17
+ # Fetch availability data for given room identifier
18
+ room_identifier = fetch_room_id(wired, lcode, room_id)
19
+ # As the room id is not visible in the web interface, we have to find it first. We use the short name as identifier.
20
+ ::Locomotive.log "**> room_ident #{room_id}"
21
+
22
+ # Now we will request the room values. Start will be startDate with data for the next 2 years
23
+ room_values = wired.fetch_rooms_values(lcode, startDate, endDate, [room_identifier])
24
+ room_data = room_values[room_identifier.to_s]
25
+ raise "Missing room data from server." if room_data == nil
26
+
27
+ room_data
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Locomotive
3
+ module WuBook
4
+ VERSION = '1.0.1'
5
+ end
6
+ end
@@ -0,0 +1,224 @@
1
+
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'date'
5
+
6
+ require 'wired'
7
+ require 'locomotive_plugins'
8
+ require_relative 'plugin/plugin_helper'
9
+
10
+ module Locomotive
11
+ module WuBook
12
+ class Plugin
13
+ include Locomotive::Plugin
14
+
15
+ def self.default_plugin_id
16
+ 'wubook'
17
+ end
18
+
19
+ def initialize
20
+
21
+ end
22
+
23
+ def config_template_file
24
+ File.join(File.dirname(__FILE__), 'plugin', 'config.haml')
25
+ end
26
+
27
+ def self.liquid_tags
28
+ {
29
+ :available => AvailableDaysBlock,
30
+ :checkInterval => CheckInterval,
31
+ :setAsBooked => SetAsBooked
32
+ }
33
+ end
34
+
35
+ protected
36
+
37
+ end
38
+
39
+ class AvailableDaysBlock < ::Liquid::Tag
40
+ include PluginHelper
41
+ def initialize(tag_name, markup, tokens, context)
42
+ @options = {
43
+ room_ident: ''
44
+ }
45
+
46
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
47
+ super
48
+ end
49
+
50
+ def render(context)
51
+ @plugin_obj = context.registers[:plugin_object]
52
+ config = @plugin_obj.config
53
+
54
+ returned_string = "["
55
+
56
+ # Evaluate variables and use the return of the evaluation if it exists..
57
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
58
+ room_ident_evaluated = context[@options[:room_ident]]
59
+ @options[:room_ident] = room_ident_evaluated unless room_ident_evaluated.nil? || room_ident_evaluated.empty?
60
+ ::Locomotive.log "**> AvailableDaysBlock room_ident: #{@options[:room_ident]}"
61
+
62
+ today = Date.today
63
+ last_day = today.next_month(config['months_ahead'].to_i)
64
+
65
+ wired = Wired.new(config)
66
+ wired.aquire_token
67
+
68
+ room_data = request_room_data(wired, config['lcode'], @options[:room_ident], today, last_day)
69
+
70
+ # Create one entry for each day from now to then.. put a 1 if the day is available or 0 if not.
71
+ (today .. last_day).each_with_index do |date, i|
72
+ returned_string += "," if i > 0
73
+
74
+ if room_data[i] != nil && room_data[i]['avail'] === 1
75
+ then
76
+ returned_string += "1"
77
+ else
78
+ returned_string += "0"
79
+ end
80
+ end
81
+
82
+ wired.release_token
83
+
84
+ returned_string + "]"
85
+ end
86
+
87
+ def render_disabled(context)
88
+ "[]"
89
+ end
90
+ end
91
+
92
+ class CheckInterval < ::Liquid::Tag
93
+ include PluginHelper
94
+ def initialize(tag_name, markup, tokens, context)
95
+ @options = {
96
+ room_ident: '',
97
+ date_start: '',
98
+ date_end: ''
99
+ }
100
+
101
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
102
+ super
103
+ end
104
+
105
+ def render(context)
106
+ @plugin_obj = context.registers[:plugin_object]
107
+ config = @plugin_obj.config
108
+
109
+ # Evaluate variables and use the return of the evaluation if it exists..
110
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
111
+ room_ident_evaluated = context[@options[:room_ident]]
112
+ @options[:room_ident] = room_ident_evaluated unless room_ident_evaluated.nil? || room_ident_evaluated.empty?
113
+ ::Locomotive.log "**> CheckInterval room_ident: #{@options[:room_ident]}"
114
+
115
+ raise "Missing parameter 'date_start'" if @options[:date_start].empty?
116
+ date_start_evaluated = context[@options[:date_start]]
117
+ @options[:date_start] = date_start_evaluated unless date_start_evaluated.nil?
118
+ ::Locomotive.log "**> CheckInterval date_start_evaluated: #{@options[:date_start]}"
119
+
120
+ raise "Missing parameter 'date_end'" if @options[:date_end].empty?
121
+ date_end_evaluated = context[@options[:date_end]]
122
+ @options[:date_end] = date_end_evaluated unless date_end_evaluated.nil?
123
+ ::Locomotive.log "**> CheckInterval date_end: #{@options[:date_end]}"
124
+
125
+ start_day = @options[:date_start]
126
+ last_day = @options[:date_end]
127
+ ::Locomotive.log "**> CheckInterval: Date Interval: #{start_day} - #{last_day}"
128
+
129
+ wired = Wired.new(config)
130
+ wired.aquire_token
131
+ room_data = request_room_data(wired, config['lcode'], @options[:room_ident], start_day, last_day)
132
+ wired.release_token
133
+
134
+ # Check whether first day is available. If this is _not_ the case we have to add one day (vacation == departure is allowed)
135
+ is_first_available = room_data[0]['avail'] === 1
136
+
137
+ # Check wheter the last day is not available. If this is _not_ the case we have to reduce one day (vacation == departure is allowed)
138
+ is_last_available = room_data[last_day.mjd - start_day.mjd]['avail'] === 1
139
+
140
+ # Remove the first or last day from the array.
141
+ room_data.shift unless is_first_available # Remove first element
142
+ room_data[0..-1] unless is_last_available # Remove last element
143
+
144
+ # Now check the (modified) interval regarding availability
145
+ is_available = true
146
+ room_data.each do |data|
147
+ ::Locomotive.log "**> CheckInterval: check: #{data}"
148
+ if data['avail'] === 0 then
149
+ is_available = false;
150
+ break
151
+ end
152
+ end
153
+
154
+ is_available ? "Ok" : "Err"
155
+ end
156
+
157
+ def render_disabled(context)
158
+ "Ok"
159
+ end
160
+ end
161
+
162
+ # This tag sets the availability of the given days to 0 (false)
163
+ class SetAsBooked < ::Liquid::Tag
164
+ include PluginHelper
165
+ def initialize(tag_name, markup, tokens, context)
166
+ @options = {
167
+ room_ident: '',
168
+ date_start: '',
169
+ date_end: ''
170
+ }
171
+
172
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
173
+ super
174
+ end
175
+
176
+ def render(context)
177
+ @plugin_obj = context.registers[:plugin_object]
178
+ config = @plugin_obj.config
179
+
180
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
181
+ room_ident_evaluated = context[@options[:room_ident]]
182
+ @options[:room_ident] = room_ident_evaluated unless room_ident_evaluated.nil? || room_ident_evaluated.empty?
183
+ ::Locomotive.log "**> SetAsBooked room_ident: #{@options[:room_ident]}"
184
+
185
+ raise "Missing parameter 'date_start'" if @options[:date_start].empty?
186
+ date_start_evaluated = context[@options[:date_start]]
187
+ @options[:date_start] = date_start_evaluated unless date_start_evaluated.nil?
188
+ ::Locomotive.log "**> SetAsBooked date_start_evaluated: #{@options[:date_start_evaluated]}"
189
+
190
+ raise "Missing parameter 'date_end'" if @options[:date_end].empty?
191
+ date_end_evaluated = context[@options[:date_end]]
192
+ @options[:date_end] = date_end_evaluated unless date_end_evaluated.nil?
193
+ ::Locomotive.log "**> SetAsBooked date_end: #{@options[:date_end]}"
194
+
195
+ start_day = @options[:date_start]
196
+ last_day = @options[:date_end]
197
+ ::Locomotive.log "**> SetAsBooked: Date Interval: #{start_day} - #{last_day}"
198
+
199
+ wired = Wired.new(config)
200
+ wired.aquire_token
201
+ room_id = fetch_room_id(wired, config['lcode'], @options[:room_ident])
202
+
203
+ # Set availability for given date interval to 0
204
+ days = []
205
+ (start_day .. last_day).each do |date|
206
+ # Ignore date.. I will just iterate over every day..
207
+ days.push({ 'avail' => 0 })
208
+ end
209
+ avail_data = [ {'id' => room_id, 'days' => days} ]
210
+ ::Locomotive.log "**> SetAsBooked: Set avail to 0 for: #{start_day}: #{avail_data}"
211
+ wired.update_rooms_values(config['lcode'], start_day, avail_data)
212
+
213
+ wired.release_token
214
+
215
+ # Return a html comment that shows us that everything is fine.
216
+ "<!-- Availability was set to 0 for room #{@options[:room_ident]} from #{@options[:date_start]} to #{@options[:date_end]} -->"
217
+ end
218
+
219
+ def render_disabled(context)
220
+ "<!-- Locomotive_wubook_plugin is disabled! -->"
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,194 @@
1
+
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'date'
5
+
6
+ require 'wired'
7
+ require 'locomotive_plugins'
8
+
9
+ module Locomotive
10
+ module WuBook
11
+ class Plugin
12
+ include Locomotive::Plugin
13
+
14
+ def self.default_plugin_id
15
+ 'wubook'
16
+ end
17
+
18
+ def initialize
19
+
20
+ end
21
+
22
+ def config_template_file
23
+ File.join(File.dirname(__FILE__), 'plugin', 'config.haml')
24
+ end
25
+
26
+ def self.liquid_tags
27
+ { :available => AvailableDaysBlock }
28
+ end
29
+
30
+ protected
31
+
32
+ end
33
+
34
+ class AvailableDaysBlock < ::Liquid::Tag
35
+ def initialize(tag_name, markup, tokens, context)
36
+ @options = {
37
+ room_ident: ''
38
+ }
39
+
40
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
41
+ super
42
+ end
43
+
44
+ def render(context)
45
+ @plugin_obj = context.registers[:plugin_object]
46
+ config = @plugin_obj.config
47
+
48
+ returned_string = "["
49
+
50
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
51
+
52
+ # Fetch availability data for given room identifier
53
+ # As the room id is not visible in the web interface, we have to find it first. We use the short name as identifier.
54
+ wired = Wired.new(config)
55
+ wired.aquire_token
56
+
57
+ # Start with finding the room-id for the room with a special name
58
+ rooms = wired.fetch_rooms(config['lcode'])
59
+ filtered_rooms = rooms.select { |room_hash| room_hash['shortname'].casecmp @options['room_ident'.to_sym] }
60
+ raise "Unable to find a room with identifier: #{@options['room_ident']}" if filtered_rooms.length == 0
61
+ room_identifier = filtered_rooms[0]['id']
62
+ raise "Unable to get the room id." if room_identifier == nil
63
+
64
+ # Now we will request the room values. Start will be today with data for the next 2 years
65
+ today = Date.today
66
+ last_day = today.next_month(config['months_ahead'].to_i)
67
+ room_values = wired.fetch_rooms_values(config['lcode'], today, last_day, [room_identifier])
68
+ room_data = room_values[room_identifier.to_s]
69
+ raise "Missing room data from server." if room_data == nil
70
+
71
+ # Create one entry for each day from now to then.. put a 1 if the day is available or 0 if not.
72
+ (today .. last_day).each_with_index do |date, i|
73
+ returned_string += "," if i > 0
74
+
75
+ if room_data[i] != nil && room_data[i]['avail'] === 1
76
+ then
77
+ returned_string += "1"
78
+ else
79
+ returned_string += "0"
80
+ end
81
+ end
82
+
83
+ wired.release_token
84
+
85
+ returned_string + "]"
86
+ end
87
+ end
88
+ <<<<<<< HEAD
89
+ =======
90
+
91
+ class CheckInterval < ::Liquid::Tag
92
+ include PluginHelper
93
+ def initialize(tag_name, markup, tokens, context)
94
+ @options = {
95
+ room_ident: '',
96
+ date_start: '',
97
+ date_end: ''
98
+ }
99
+
100
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
101
+ super
102
+ end
103
+
104
+ def render(context)
105
+ @plugin_obj = context.registers[:plugin_object]
106
+ config = @plugin_obj.config
107
+
108
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
109
+ raise "Missing parameter 'date_start'" if @options[:date_start].empty?
110
+ raise "Missing parameter 'date_end'" if @options[:date_end].empty?
111
+
112
+ start_day = Date.strptime(@options[:date_start], '%d.%m.%Y')
113
+ last_day = Date.strptime(@options[:date_end], '%d.%m.%Y')
114
+ ::Locomotive.log "**> CheckInterval: Date Interval: #{start_day} - #{last_day}"
115
+
116
+ wired = Wired.new(config)
117
+ wired.aquire_token
118
+ room_data = request_room_data(wired, config['lcode'], @options[:room_ident], start_day, last_day)
119
+ wired.release_token
120
+
121
+ # Check whether first day is available. If this is _not_ the case we have to add one day (vacation == departure is allowed)
122
+ is_first_available = room_data[0]['avail'] === 1
123
+
124
+ # Check wheter the last day is not available. If this is _not_ the case we have to reduce one day (vacation == departure is allowed)
125
+ is_last_available = room_data[last_day.mjd - start_day.mjd]['avail'] === 1
126
+
127
+ # Remove the first or last day from the array.
128
+ room_data.shift unless is_first_available # Remove first element
129
+ room_data[0..-1] unless is_last_available # Remove last element
130
+
131
+ # Now check the (modified) interval regarding availability
132
+ is_available = true
133
+ room_data.each do |data|
134
+ ::Locomotive.log "**> CheckInterval: check: #{data}"
135
+ if data['avail'] === 0 then
136
+ is_available = false;
137
+ break
138
+ end
139
+ end
140
+
141
+ is_available ? "Ok" : "Err"
142
+ end
143
+ end
144
+
145
+ # This tag sets the availability of the given days to 0 (false)
146
+ class SetAsBooked < ::Liquid::Tag
147
+ include PluginHelper
148
+ def initialize(tag_name, markup, tokens, context)
149
+ @options = {
150
+ room_ident: '',
151
+ date_start: '',
152
+ date_end: ''
153
+ }
154
+
155
+ markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
156
+ super
157
+ end
158
+
159
+ def render(context)
160
+ @plugin_obj = context.registers[:plugin_object]
161
+ config = @plugin_obj.config
162
+
163
+ raise "Missing parameter 'room_ident'" if @options[:room_ident].empty?
164
+ raise "Missing parameter 'date_start'" if @options[:date_start].empty?
165
+ raise "Missing parameter 'date_end'" if @options[:date_end].empty?
166
+
167
+ start_day = Date.strptime(@options[:date_start], '%d.%m.%Y')
168
+ last_day = Date.strptime(@options[:date_end], '%d.%m.%Y')
169
+ ::Locomotive.log "**> SetAsBooked: Date Interval: #{start_day} - #{last_day}"
170
+
171
+ wired = Wired.new(config)
172
+ wired.aquire_token
173
+ room_id = fetch_room_id(wired, config['lcode'], @options[:room_ident])
174
+
175
+ # Set availability for given date interval to 0
176
+ days = []
177
+ (start_day .. last_day).each do |date|
178
+ # Ignore date.. I will just iterate over every day..
179
+ days.push({ 'avail' => 0 })
180
+ end
181
+ avail_data = [ {'id' => room_id, 'days' => days} ]
182
+ ::Locomotive.log "**> SetAsBooked: Set avail to 0 for: #{start_day}: #{avail_data}"
183
+ wired.update_rooms_values(config['lcode'], start_day, avail_data)
184
+
185
+ wired.release_token
186
+
187
+ # Return a html comment that shows us that everything is fine.
188
+ "<!-- Availability was set to 0 for room #{@options[:room_ident]} from #{@options[:date_start]} to #{@options[:date_end]} -->"
189
+ end
190
+
191
+ end
192
+ >>>>>>> 06db809... ddd
193
+ end
194
+ end
@@ -0,0 +1 @@
1
+ require 'locomotive/wubook/plugin'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locomotive_wubook_plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Eilers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: locomotive_plugins
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wubook_wired
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Locomotive plugin for accessing WuBook.net channel manager
42
+ email: se@intelligentmobiles.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - Gemfile
48
+ - lib/locomotive/wubook/plugin.rb
49
+ - lib/locomotive/wubook/plugin.rb.orig
50
+ - lib/locomotive/wubook/plugin/config.haml
51
+ - lib/locomotive/wubook/plugin/plugin_helper.rb
52
+ - lib/locomotive/wubook/plugin/version.rb
53
+ - lib/locomotive_wubook_plugin.rb
54
+ homepage: https://github.com/eilers/locomotive_wubook_plugin
55
+ licenses:
56
+ - Private
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.6
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Integrates some of the WuBooks 'Wired' interface.
78
+ test_files: []