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.



13
14
15
16
17
18
19
# File 'lib/booker.rb', line 13

def initialize(key, secret, options = {})
  @production = options.fetch(:production) { false }
  @key = key
  @secret = secret
  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’)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/booker.rb', line 25

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

    page_number+=1
  end while results.length < total_results-1

  last_result.merge({
    result_name => results
  })
end

#create_appointment(options = {}) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/booker.rb', line 196

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

#find_locations(options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/booker.rb', line 156

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/booker.rb', line 177

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/booker.rb', line 129

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



233
234
235
236
237
# File 'lib/booker.rb', line 233

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



220
221
222
223
# File 'lib/booker.rb', line 220

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



226
227
228
229
230
# File 'lib/booker.rb', line 226

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



240
241
242
243
# File 'lib/booker.rb', line 240

def get_server_information
  url = build_url "/server_information", "?access_token=#{@access_token}"
  return_get_response url
end

#get_treatment_categories(location_id) ⇒ Object



206
207
208
209
210
# File 'lib/booker.rb', line 206

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



213
214
215
216
217
# File 'lib/booker.rb', line 213

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

#run_multi_service_availability(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/booker.rb', line 73

def run_multi_service_availability options = {}
  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
  return_post_response url, defaults, options
end

#run_multi_spa_availability(options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/booker.rb', line 100

def run_multi_spa_availability options = {}
  # 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
  return_post_response url, defaults, options
end

#run_service_availability(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/booker.rb', line 57

def run_service_availability options = {}
  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
  return_post_response url, defaults, options
end