-
Notifications
You must be signed in to change notification settings - Fork 46
Getting Started with the MindBody API gem
Marshal Linfoot edited this page Feb 20, 2014
·
2 revisions
I needed to integrate with the MindBody API and found this gem, still in development. Through the kindness of the developer answering a few emails I was able to get integrated with MindBody in a matter of hours. This entry hopes to save others some time.
My need was to pull a client calendar, by week, and display it in a Rails 4 application.
- In the Gemfile:
gem 'mindbody-api', :git => 'git://github.com/wingrunr21/mindbody-api.git'
- In lib/mind_body_api.rb:
def self.fetch_mindbody_schedule(*opts)
options = opts.extract_options!
start_date = options[:start_date] || Date.today.beginning_of_week
end_date = options[:end_date] || Date.today.end_of_week
message = {
'StartDateTime' => start_date,
'EndDateTime' => end_date,
'HideCanceledClasses' => true
}
message['ClientID'] = options[:user_id] unless options[:user_id].nil?
res = ClassService.get_classes(message)
classes = res.result[:classes]
classes.sort!{|a,b| a.start_date_time <=> b.start_date_time}
return classes
end
- In initializers/mind_body_init.rb:
require "#{Rails.application.root}/lib/mind_body_api.rb"
MindBody.configuration.source_name = "SOURCE_NAME" # from MindBody Developer Account (free)
MindBody.configuration.source_key = "SOURCE_KEY" # from MindBody Developer Account
MindBody.configuration.site_ids = [11111] # array of Site IDs - or just 1
- Now you can get the URL for the client to activate your project as a MindBody App - try this from Rails' console:
MindBody::Services::SiteService.get_activation_code
Grab the URL out of there and forward it, or if you have the client's MindBody credentials, activate it yourself.
- Once the site is activated, you can
MindBodySchedules.fetch_mindbody_schedule(:start_date => start_date, :end_date => end_date)
- This concludes the MindBody API basics - but if anyone needs an awesome calendar view to go with this, check out FullCalendar. This is a jquery plugin but someone has rails'd it:
Gemfile:
gem 'fullcalendar-rails'
View:
#calendar
:javascript
$(document).ready(function() {
// page is now ready, initialize the calendar...
$('#calendar').fullCalendar({
events: '/calendar_feed',
defaultView: 'basicWeek'
})
});
Controller:
def calendar
end
def calendar_feed
start_date = Time.at(params[:start].to_i).to_date
end_date = Time.at(params[:end].to_i).to_date
time_zone_offset = 7 #fix MindBody lack of TZ support
courses = MindBodySchedules.fetch_mindbody_schedule(:start_date => start_date, :end_date => end_date)
response = []
courses.each_with_index do |course,index|
response << {
:id => index.to_s,
:title => "#{course.class_description.name}: #{course.staff.name}",
:start => (course.start_date_time + time_zone_offset.hours).to_i.to_s,
:end => (course.end_date_time + time_zone_offset.hours).to_i.to_s,
:allDay => false
}
end
render :json => response
end
Hope this helps someone!