Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Last active December 25, 2015 18:49
Show Gist options
  • Save ssaunier/7023158 to your computer and use it in GitHub Desktop.
Save ssaunier/7023158 to your computer and use it in GitHub Desktop.
Using MobiCheckin API to create 210 check-in points representing a time and room to be booked.
#!/usr/bin/env ruby
# Usage: MOBICHECKIN_API_TOKEN=#### MOBICHECKIN_EVENT_ID=##### ./rooms.rb
require "rubygems"
require "httparty"
class MobiCheckin
include HTTParty
# base_uri "localhost:5000/api/v1"
base_uri "https://app.mobicheckin.com/api/v1"
def initialize(api_token, event_id)
@auth_token = api_token
@event_id = event_id
end
def create_checkin_point(checkin_point)
body = { :accesspoint => checkin_point, :auth_token => @auth_token }
self.class.post "/events/#{@event_id}/accesspoints.json", :body => body
end
end
rooms = [
{ :name => "Curie", :hall => 3, :min => 2, :max => 14 },
{ :name => "Einstein", :hall => 3, :min => 2, :max => 14 },
{ :name => "Vinci", :hall => 3, :min => 2, :max => 14 },
{ :name => "Edison", :hall => 5, :min => 2, :max => 12 },
{ :name => "Franklin", :hall => 5, :min => 2, :max => 12 },
{ :name => "Galilee", :hall => 5, :min => 2, :max => 12 },
{ :name => "Newton", :hall => 5, :min => 10, :max => 25 }
]
timeslots = []
[ "Nov 19th (Tue)", "Nov 20th (Wed)", "Nov 21st (Thu)" ].each_with_index do |day, i|
(9..18).each do |hour|
rooms.each do |room|
timeslots << { :day => day, :day_number => i + 1, :hour => hour, :room => room }
end
end
end
def checkin_point_from_slot(slot)
name = "#{slot[:day]} #{"%02d" % slot[:hour]}-#{"%02d" % (slot[:hour] + 1)}h - Hall #{slot[:room][:hall]}, #{slot[:room][:name]}"
{
:name => name,
:capacity => 1, # Only one person can book the timeslot!
:traits => slot
}
end
puts "Will create #{timeslots.size} slots"
mobicheckin = MobiCheckin.new ENV['MOBICHECKIN_API_TOKEN'], ENV['MOBICHECKIN_EVENT_ID']
timeslots.each do |timeslot|
checkin_point = checkin_point_from_slot timeslot
puts "Creating #{checkin_point[:name]}"
mobicheckin.create_checkin_point checkin_point
end
{% guest_select_field participants "Number of Participants", required: true %}
{% option "Choose", value: "" %}
{% option 2 %}
{% option 3 %}
{% option 4 %}
{% option 5 %}
{% option 6 %}
{% option 7 %}
{% option 8 %}
{% option 9 %}
{% option 10 %}
{% endguest_select_field %}
<div class="control-group">
<div class="control-label">Book a room</div>
<div class="controls">
<p id="please" class="help-inline" style="padding-top: 0.4em">
Please choose a number of participants to book a room.
</p>
<div id="rooms" style="display: none; padding-top: 0.4em">
{{ assign previous = 0 }}
{% for point in checkin_points %}
{% unless point.capacity_reached? %}
{% capture index %}{{ point.traits.day_number }}{{ point.traits.hour - 9 }}{% endcapture %}
{% unless previous %}
{% assign previous = index %} <!-- Do not put a <hr /> at the beginning -->
{% endunless %}
{% if previous != index %}
{% assign previous = index %}
<hr />
{% endif %}
<label class="radio" for="{{ forloop.index }}"
data-min="{{ point.traits.room.min }}"
data-max="{{ point.traits.room.max }}">
<input name="guest[access_privileges_attributes][0][accesspoint_id]"
type="radio" value="{{ point.id }}" id="{{ forloop.index }}" />
<input name="guest[access_privileges_attributes][0][access_once]"
type="hidden" value="false" />
{{ point.name }}
</label>
{% endunless %}
{% endfor %}
</div>
</div>
</div>
<script type="text/javascript">
function hideUnusedHr() {
$('hr').each(function(i, item) {
if ($(item).nextUntil('hr').find(':visible').length == 0) {
$(item).hide();
} else {
$(item).show();
}
});
}
function displayRooms(participants) {
$('#rooms').find('label.radio').each(function(i, item) {
var $item = $(item);
var min = $item.data('min');
var max = $item.data('max');
if (min <= participants && participants <= max) {
$item.show();
} else {
$item.hide();
}
});
setTimeout(hideUnusedHr, 1);
}
function readParticipants($target) {
var participants = parseInt($target.val());
if (participants > 0) {
displayRooms(participants);
$('#rooms').show();
$('#please').hide();
} else {
$('#rooms').hide();
$('#please').show();
}
}
$(document).ready(function() {
$('select').change(function(e) {
readParticipants($(e.target));
});
if (parseInt($('select').val()) > 0) {
readParticipants($('select'));
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment