bookingsync_portal 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ce67c591be627737c6dc3ba40d3bc3ad316303f
4
- data.tar.gz: a13364a65827af2512a202158a570bf4c100bd99
3
+ metadata.gz: f422146aee05f0a7289012fbc8813d5a7f962892
4
+ data.tar.gz: 96b7383428c7b3ff53e242b090ff44f1a3440934
5
5
  SHA512:
6
- metadata.gz: 6611d07bf33fc1017d31b1d9eb2be1e879cca68507d2e4da848762f40f266be3abae1253c035e0e318866131bb6da55040d81b6a8b6c2f1e8274d46c0b561aa9
7
- data.tar.gz: 1e221d50347a5a285398a53e75906b56176e52f8c8b5d98ac13e376778d3f292e6591350c3ee86804768b2e9c9624253cfeb3e255c0a2b0aea0b9296723e6a4e
6
+ metadata.gz: e2429dd49609564b364bd5558f8d9023cbc12a3625f639d938ce2d2a9b87c32c6b79214ee5031599207292768d3a4ecc150a8854b393ea265a5b7ae8e4b3d91a
7
+ data.tar.gz: c44a4559ae9d402f217122f28235edd5a073e0cae320577cc1aab390f758ab593dd715decc193af72262ea8a1787b3dd50d33a77ce53a6e5ee4925be1a346ab8
data/README.md CHANGED
@@ -7,7 +7,7 @@ A Rails engine to simplify building BookingSync Portal Applications.
7
7
 
8
8
  ## Requirements
9
9
 
10
- This engine requires Rails `>= 4.0.0` and Ruby `>= 2.0.0`.
10
+ This engine requires Rails `>= 4.0.0` and Ruby `>= 2.1.0`.
11
11
 
12
12
  ## Documentation
13
13
 
@@ -15,7 +15,7 @@ This engine requires Rails `>= 4.0.0` and Ruby `>= 2.0.0`.
15
15
 
16
16
  ## Installation
17
17
 
18
- BookingSync Portal works with Rails 4.0 onwards and Ruby 2.0 onwards. To get started, add it to your Gemfile with:
18
+ BookingSync Portal works with Rails 4.0 onwards and Ruby 2.1 onwards. To get started, add it to your Gemfile with:
19
19
 
20
20
  ```ruby
21
21
  gem 'bookingsync_portal'
@@ -1,6 +1,7 @@
1
1
  require 'bookingsync_application'
2
2
  require 'bookingsync_portal/engine'
3
3
  require 'bookingsync_portal/mash_serializer'
4
+ require 'bookingsync_portal/booking_map'
4
5
  require 'message_bus'
5
6
 
6
7
  # FIXME requires below should get removed when ember frontend is added
@@ -0,0 +1,109 @@
1
+ class BookingsyncPortal::BookingMap
2
+ class InvalidLength < StandardError
3
+ end
4
+
5
+ attr_reader :bookings, :statuses, :from, :to, :length
6
+
7
+ def initialize(bookings, options = {})
8
+ @from = options.fetch(:from) { Date.today.beginning_of_month }
9
+ @from = @from.to_date
10
+
11
+ @length = options.fetch(:length) { 1096 }
12
+ @to = @from.advance(days: @length.to_i)
13
+ @statuses = options.fetch(:statuses) { {
14
+ available: "0",
15
+ tentative: "0",
16
+ booked: "1",
17
+ unavailable: "1"
18
+ } }
19
+ @bookings = bookings_for_map(bookings, @from, @to)
20
+ end
21
+
22
+ def map
23
+ days = get_days_hash(from, to, statuses[:available].to_s)
24
+
25
+ bookings.each do |booking|
26
+ (booking.start_at.to_date...booking.end_at.to_date).each do |day|
27
+ if day >= from && day <= to
28
+ days[day] = statuses[booking.status.downcase.to_sym]
29
+ end
30
+ end
31
+ end
32
+ days.values.join
33
+ end
34
+
35
+ class << self
36
+ # Diff maps
37
+ #
38
+ # @param source [String] The source map (expecting a map of `1` and `0` only)
39
+ # @param destination [String] The destination map (expecting a map of `1` and `0` only)
40
+ # @return [String] A map with the changes between both maps:
41
+ # _ - Identical
42
+ # 1 - Create (present on source only)
43
+ # 0 - Destroy (present on destination only)
44
+ def diff(source, destination)
45
+ raise InvalidLength if source.size != destination.size
46
+
47
+ new_result = "_" * source.size
48
+ source.chars.each_with_index do |char, index|
49
+ new_result[index] = char if destination[index] != char
50
+ end
51
+
52
+ new_result
53
+ end
54
+
55
+ # Extract ranges
56
+ #
57
+ # Returns all the ranges with their statuses.
58
+ # The `_` is considered as empty and won't be returned as a range.
59
+ # @param map [String] A map String
60
+ # @param from [Date] The day that the map starts
61
+ # @return [Array<Hash>] Returns an Array of Hashes with all the ranges
62
+ def extract_ranges(map, from)
63
+ ranges = []
64
+ previous_char = nil
65
+ range = nil
66
+ map_length = map.length
67
+ map.chars.each_with_index do |char, index|
68
+ if char != previous_char && char != "_" # finished
69
+ ranges << range if range
70
+
71
+ range = {
72
+ start_at: from.advance(days: index),
73
+ end_at: from.advance(days: index + 1)
74
+ }.merge(status: char)
75
+ elsif char != "_" # continuing
76
+ range[:end_at] = from.advance(days: index + 1)
77
+ end
78
+
79
+ if index == map_length - 1
80
+ ranges << range if range
81
+ end
82
+
83
+ previous_char = char
84
+ end
85
+
86
+ ranges
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def bookings_for_map(bookings, from, to)
93
+ if bookings.is_a?(ActiveRecord::Relation)
94
+ bookings.where("end_at > ? AND start_at <= ?", from.end_of_day, to)
95
+ .order(:start_at)
96
+ else
97
+ bookings.find_all do |booking|
98
+ booking.end_at > from.end_of_day && booking.start_at <= to
99
+ end.sort_by { |booking| booking.start_at }
100
+ end
101
+ end
102
+
103
+ def get_days_hash(start_at, end_at, default_value = nil)
104
+ period = start_at...end_at # should not include end_at
105
+ period.each_with_object({}) do |day, days|
106
+ days[day] = default_value
107
+ end
108
+ end
109
+ end
@@ -1,3 +1,3 @@
1
1
  module BookingsyncPortal
2
- VERSION = '0.8.3'
2
+ VERSION = '0.8.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookingsync_portal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Marciniak
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-02-24 00:00:00.000000000 Z
13
+ date: 2016-03-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -389,6 +389,7 @@ files:
389
389
  - db/migrate/20150521091056_create_rates.rb
390
390
  - db/migrate/20151210164752_add_synced_source_id_to_account.rb
391
391
  - lib/bookingsync_portal.rb
392
+ - lib/bookingsync_portal/booking_map.rb
392
393
  - lib/bookingsync_portal/engine.rb
393
394
  - lib/bookingsync_portal/mash_serializer.rb
394
395
  - lib/bookingsync_portal/version.rb