class Booker::Client

Attributes

access_token[R]
expires_in[R]
server_time_offset[R]
url[R]

Public Class Methods

new(key, secret, options = {}) click to toggle source
# File lib/booker.rb, line 20
def initialize(key, secret, options = {})
  @production = options.fetch(:production) { false }
  @log_level = options.fetch(:log_level) { Logger::DEBUG }
  @key = key
  @secret = secret
  set_log_level!
  set_access_token!
  set_server_time_offset!
end

Public Instance Methods

all(method, result_name, options = {}) click to toggle source

Useful to pull all of paged results and return them as if you did one request for them all

ex: client.all(:find_locations, ‘Results’)

# File lib/booker.rb, line 34
def all method, result_name, options = {}
  page_number = 1
  results = []

  # this allows page size to be overidden but NOT page_number as I want to
  # control that to know when we have all results
  options = {"PageSize" => 500}.merge options

  begin
    options.merge!({
      "PageNumber" => page_number,
    })

    last_result = self.send(method, options)

    results << last_result[result_name]
    results.flatten!


    if last_result['TotalResultsCount']
      total_results  = last_result['TotalResultsCount']
    else
      total_results = 0
    end
    logger.debug "#{results.length} / #{total_results}"
    page_number+=1
  end while results.length < total_results-1

  last_result.merge({
    result_name => results
  })
end
create_appointment(options = {}) click to toggle source
# File lib/booker.rb, line 241
def create_appointment options = {}
  url = build_url "/appointment/create"
  defaults = {
    "access_token" => @access_token,
  }
  convert_time_to_booker_format! options
  return_post_response url, defaults, options
end
create_customer(options = {}) click to toggle source
# File lib/booker.rb, line 250
def create_customer options = {}
  url = build_url "/customer"

  defaults = {
    'FirstName' => '',
    'LastName' => '',
    'HomePhone' => '',
    'LocationID' => '',
    'Email' => '',
    "access_token" => @access_token,
  }

  return_post_response url, defaults, Booker::Helpers.new_client_params(options)
end
find_employees(options = {}) click to toggle source
# File lib/booker.rb, line 198
def find_employees options = {}
  url = build_url '/employees'

  defaults = {
    "IgnoreFreelancers" => true,
    "LocationID" => nil,
    "OnlyIncludeActiveEmployees" => true,
    "PageNumber" => 1,
    "PageSize" => 10,
    "SortBy" => [
      {
        "SortBy" => "LastName",
        "SortDirection" => 0
      }
    ],
    "TreatmentID" => nil,
    "UsePaging" => true,
    "access_token" => @access_token
  }

  return_post_response url, defaults, options
end
find_locations(options = {}) click to toggle source

apidoc.booker.com/Method/Detail/852

# File lib/booker.rb, line 178
def find_locations options = {}
  url = build_url "/locations"
  defaults = {
    "access_token" => @access_token,
    "BrandID" => nil,
    "BusinessName" => nil,
    "FeatureLevel" => nil,
    "PageNumber" => 1,
    "PageSize" => 5,
    "SortBy" => [
      {
        "SortBy" => "Name",
        "SortDirection" => 0
      }
    ],
    "UsePaging" => true, # throws a weird exception about null arguments
  }
  return_post_response url, defaults, options
end
find_locations_partial(options = {}) click to toggle source

apidoc.booker.com/Method/Detail/853

# File lib/booker.rb, line 222
def find_locations_partial options = {}
  url = build_url "/locations/partial"
  defaults = {
    "access_token" => @access_token,
    "BusinessTypeId" => nil,
    "PageNumber" => 1,
    "PageSize" => 5,
    "SortBy" => [
      {
        "SortBy" => "Name",
        "SortDirection" => 0
      }
    ],
    #"UsePaging" => true, # throws a weird exception about null arguments
    "Name" => nil
  }
  return_post_response url, defaults, options
end
find_treatments(options = {}) click to toggle source

apidoc.booker.com/Method/Detail/123

# File lib/booker.rb, line 151
def find_treatments options = {}
  raise Booker::ArgumentError, 'LocationID is required' unless options['LocationID']
  url = build_url "/treatments"
  defaults = {
    "access_token" => @access_token,
    "AllowOnGiftCertificateSale" => nil,
    "CategoryID" => nil,
    "EmployeeID" => nil,
    "LocationID" => nil,
    "PageNumber" => 1,
    "PageSize" => 100,
    "SortBy" => [
      {
        "SortBy" => "Name",
        "SortDirection" => 0
      }
    ],
    "SubCategoryID" => nil,
    "UsePaging" => true,
    "ExcludeClassesAndWorkshops" => nil,
    "OnlyClassesAndWorkshops" => nil,
    "SkipLoadingRoomsAndEmployees" => nil,
  }
  return_post_response url, defaults, options
end
get_credit_card_types(location_id) click to toggle source

apidoc.booker.com/Method/Detail/107

# File lib/booker.rb, line 299
def get_credit_card_types location_id
  url = build_url "/location/#{location_id}/creditcard_types",
          "?access_token=#{@access_token}"
  return_get_response url
end
get_location(location_id) click to toggle source

apidoc.booker.com/Method/Detail/153

# File lib/booker.rb, line 286
def get_location location_id
  url = build_url "/location/#{location_id}", "?access_token=#{@access_token}"
  return_get_response url
end
get_location_online_booking_settings(location_id) click to toggle source

apidoc.booker.com/Method/Detail/134

# File lib/booker.rb, line 292
def get_location_online_booking_settings location_id
  url = build_url "/location/#{location_id}/online_booking_settings",
          "?access_token=#{@access_token}"
  return_get_response url
end
get_server_information() click to toggle source

apidoc.booker.com/Method/Detail/147

# File lib/booker.rb, line 306
def get_server_information
  url = build_url "/server_information", "?access_token=#{@access_token}"
  return_get_response url
end
get_treatment(treatment_id) click to toggle source

apidoc.booker.com/Method/Detail/124

# File lib/booker.rb, line 266
def get_treatment treatment_id
  url = build_url "/treatment/#{treatment_id}", "?access_token=#{@access_token}"
  return_get_response url
end
get_treatment_categories(location_id) click to toggle source

apidoc.booker.com/Method/Detail/125

# File lib/booker.rb, line 272
def get_treatment_categories location_id
  url = build_url "/treatment_categories",
        "?access_token=#{@access_token}&culture_name=&location_id=#{location_id}"
  return_get_response url
end
get_treatment_sub_categories(location_id, category_id) click to toggle source

apidoc.booker.com/Method/Detail/126

# File lib/booker.rb, line 279
def get_treatment_sub_categories location_id, category_id
  url = build_url "/treatment_subcategories",
        "?access_token=#{@access_token}&culture_name=&location_id=#{location_id}&category_id=#{category_id}"
  return_get_response url
end
logger() click to toggle source
# File lib/booker.rb, line 12
def logger
  @logger ||= Logger.new STDOUT
end
logger=(logger) click to toggle source
# File lib/booker.rb, line 16
def logger=logger
  @logger = logger
end
run_multi_service_availability(options = {}) click to toggle source

apidoc.booker.com/Method/Detail/129

# File lib/booker.rb, line 87
def run_multi_service_availability options = {}, pass_response = false
  raise Booker::ArgumentError, 'Itineraries is required' unless options['Itineraries']
  url = build_url "/availability/multiservice"
  defaults =
    {
    "access_token" => @access_token,
    "StartDateTime" => Time.now,
    "Itineraries" => [
      #{
        #"IsPackage" => false,
        #"PackageID" => nil,
        #"Treatments" => [
          #{
            #"EmployeeID" => nil,
            #"TreatmentID" => nil
          #}
        #]
      #}
    ],
    "LocationID" => nil,
    "MaxTimesPerDay" => nil,
    "EndDateTime" => Time.now.to_i + 60 * 60 * 5,
  }
  convert_time_to_booker_format! options
  if pass_response
    request_params url, defaults, options
  else
    return_post_response url, defaults, options
  end
end
run_multi_spa_availability(options = {}) click to toggle source
# File lib/booker.rb, line 118
def run_multi_spa_availability options = {}, pass_response = false
  # TODO: Assert required fields are present
  url = build_url '/availability/multispa'
  defaults = {
    #"AirportCode" => "",
    #"BrandID" => null,
    #"CityName" => "New York City",
    #"CountryCode" => "USA",
    #"IsApiDistributionPartner" => null,
    #"Latitude" => null,
    #"Longitude" => null,
    "Radius" => 20,
    #"SpaExistsInSpaFinder" => null,
    #"StateAbbr" => "NY",
    "ZipCode" => "77057",
    "MaxNumberOfLocations" => 5,
    "EndDateTime" => Time.now.to_i + 60 * 60 * 5,
    #"LocationID" => 3749,
    #"MaxTimesPerTreatment" => 2,
    "StartDateTime" => Time.now.to_i,
    "TreatmentCategoryID" => 30,
    "TreatmentSubCategoryID" => 218,
    "access_token" => @access_token
  }
  convert_time_to_booker_format! options
  if pass_response
    request_params url, defaults, options
  else
    return_post_response url, defaults, options
  end
end
run_service_availability(options = {}) click to toggle source
# File lib/booker.rb, line 67
def run_service_availability options = {}, pass_response = false
  url = build_url '/availability/service'
  defaults = {
    "EndDateTime" => Time.now.to_i + 60 * 60 * 5,
    "LocationID" => 3749,
    "MaxTimesPerTreatment" => 5,
    "StartDateTime" => Time.now,
    "TreatmentCategoryID" => 1,
    "TreatmentSubCategoryID" => 218,
    "access_token" => @access_token
  }
  convert_time_to_booker_format! options
  if pass_response
    request_params url, defaults, options
  else
    return_post_response url, defaults, options
  end
end

Private Instance Methods

base_url() click to toggle source
# File lib/booker.rb, line 372
def base_url
  "https://" + (@production ? Booker::PRODUCTION_BASE_HOST : Booker::STAGING_BASE_HOST) + Booker::BASE_PATH
end
build_url(path, query = '') click to toggle source
# File lib/booker.rb, line 376
def build_url path, query = ''
  base_url + path + query
end
convert_time_to_booker_format!(options) click to toggle source
# File lib/booker.rb, line 380
def convert_time_to_booker_format! options
  options['StartDateTime'] = Booker::Helpers.format_date options['StartDateTime'], server_time_offset
  options['EndDateTime'] = Booker::Helpers.format_date options['EndDateTime'], server_time_offset

  if options['ItineraryTimeSlotList']
    options['ItineraryTimeSlotList'].each do |time_slot_list|

      time_slot_list['StartDateTime'] = Booker::Helpers.format_date time_slot_list['StartDateTime'], server_time_offset

      time_slot_list['TreatmentTimeSlots'].each do |time_slot|
        time_slot['StartDateTime'] = Booker::Helpers.format_date time_slot['StartDateTime'], server_time_offset
      end
    end

  end
end
get(url) click to toggle source
# File lib/booker.rb, line 346
def get url
  HTTParty.get url
end
log_options(options) click to toggle source
# File lib/booker.rb, line 397
def log_options options
  msg = "-----------------------\n"
  msg << "Ruby-Booker Options:\n"
  msg << "#{options}"
  msg << "\n-----------------------"
  logger.debug msg
end
parse_body(body) click to toggle source
# File lib/booker.rb, line 332
def parse_body body
  body = JSON.parse(body)
  raise Booker::ApiSuccessFalseError, body if body['IsSuccess'] == false
  body
end
post(url, post_data) click to toggle source
# File lib/booker.rb, line 338
def post url, post_data
  options = {
    body: post_data.to_json,
    headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
  }
  HTTParty.post url, options
end
request_params(url, defaults, options) click to toggle source
# File lib/booker.rb, line 319
def request_params url, defaults, options
  options = defaults.merge(options)
  {
    url: url,
    options: options
  }
end
return_get_response(url) click to toggle source
# File lib/booker.rb, line 327
def return_get_response url
  response = get url
  parse_body response.body
end
return_post_response(url, defaults, options) click to toggle source
# File lib/booker.rb, line 312
def return_post_response url, defaults, options
  options = defaults.merge(options)
  log_options options
  response = post url, options
  parse_body response.body
end
set_access_token!() click to toggle source
# File lib/booker.rb, line 354
def set_access_token!
  url      = build_url '/access_token', "?client_id=#{@key}&client_secret=#{@secret}&grant_type=client_credentials"
  response = HTTParty.get(url)
  body     = JSON.parse(response.body)

  if body['error']
    raise body['error'] + ":" + body['error_description']
  else
    @access_token = body['access_token']
    @expires_in = body['expires_in']
  end
end
set_log_level!() click to toggle source
# File lib/booker.rb, line 350
def set_log_level!
  logger.level = @log_level
end
set_server_time_offset!() click to toggle source
# File lib/booker.rb, line 367
def set_server_time_offset!
  @server_time_offset = get_server_information['ServerTimeZoneOffset']
end