Class: Booker::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/booker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/booker.rb', line 11

def access_token
  @access_token
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



11
12
13
# File 'lib/booker.rb', line 11

def expires_in
  @expires_in
end

#server_time_offsetObject (readonly)

Returns the value of attribute server_time_offset.



11
12
13
# File 'lib/booker.rb', line 11

def server_time_offset
  @server_time_offset
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/booker.rb', line 11

def url
  @url
end

Instance Method Details

#all(method, result_name, options = {}) ⇒ Object

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’)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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 = {}) ⇒ Object



241
242
243
244
245
246
247
248
# 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 = {}) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# 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 = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 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 = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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 = {}) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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 = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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) ⇒ Object



299
300
301
302
303
# 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) ⇒ Object



286
287
288
289
# 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) ⇒ Object



292
293
294
295
296
# 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_informationObject



306
307
308
309
# 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) ⇒ Object



266
267
268
269
# 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) ⇒ Object



272
273
274
275
276
# 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) ⇒ Object



279
280
281
282
283
# 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

#loggerObject



12
13
14
# File 'lib/booker.rb', line 12

def logger
  @logger ||= Logger.new STDOUT
end

#logger=(logger) ⇒ Object



16
17
18
# File 'lib/booker.rb', line 16

def logger=logger
  @logger = logger
end

#run_multi_service_availability(options = {}, pass_response = false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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 = {}, pass_response = false) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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 = {}, pass_response = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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