Class: Gonebusy::BookingsController

Inherits:
BaseController show all
Defined in:
lib/gonebusy/controllers/bookings_controller.rb

Constant Summary collapse

@@instance =
BookingsController.new

Instance Attribute Summary

Attributes inherited from BaseController

#http_call_back, #http_client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

#execute_request, #initialize, #validate_parameters, #validate_response

Constructor Details

This class inherits a constructor from Gonebusy::BaseController

Class Method Details

.instanceObject

Singleton instance of the controller class



7
8
9
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 7

def self.instance
  @@instance
end

Instance Method Details

#cancel_booking_by_id(authorization, id, date = nil, end_date = nil, cancel_recurring = nil) ⇒ Object

Cancel a Booking by id

Parameters:

  • authorization (String)

    Required parameter: A valid API key, in the format ‘Token API_KEY’

  • id (String)

    Required parameter: Example:

  • date (Date) (defaults to: nil)

    Optional parameter: If a recurring booking, the date of an instance to cancel. Several formats are supported: ‘2014-10-31’, ‘October 31, 2014’

  • end_date (Date) (defaults to: nil)

    Optional parameter: If recurring, cancel up to :end_date or leave blank for infinite booking. Several formats are supported: ‘2014-10-31’, ‘October 31, 2014’.

  • cancel_recurring (String) (defaults to: nil)

    Optional parameter: When a recurring booking, one of: [‘instance’, ‘all’, ‘infinite’]

Returns:

  • CancelBookingByIdResponse response from the API call



18
19
20
21
22
23
24
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
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 18

def cancel_booking_by_id(authorization, 
                         id, 
                         date = nil, 
                         end_date = nil, 
                         cancel_recurring = nil)

  # prepare query url
  _query_builder = Configuration.get_base_uri()
  _query_builder << '/bookings/{id}'
  _query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
    'id' => id
  }
  _query_builder = APIHelper.append_url_with_query_parameters _query_builder, {
    'date' => date,
    'end_date' => end_date,
    'cancel_recurring' => cancel_recurring
  }, array_serialization: Configuration.array_serialization
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'accept' => 'application/json',
    'Authorization' => Configuration.authorization,
    'Authorization' => authorization
  }

  # prepare and execute HttpRequest
  _request = @http_client.delete _query_url, headers: _headers
  CustomAuth.apply(_request)
  _context = execute_request(_request)

  # validate response against endpoint and global error codes
  if _context.response.status_code == 400
    raise EntitiesErrorErrorException.new 'Bad Request', _context
  elsif _context.response.status_code == 401
    raise EntitiesErrorErrorException.new 'Unauthorized/Missing Token', _context
  elsif _context.response.status_code == 403
    raise EntitiesErrorErrorException.new 'Forbidden', _context
  elsif _context.response.status_code == 404
    raise EntitiesErrorErrorException.new 'Not Found', _context
  elsif !_context.response.status_code.between?(200, 208)
    raise APIException.new 'Unexpected error', _context
  end
  validate_response(_context)

  # return appropriate response type
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  return CancelBookingByIdResponse.from_hash(decoded)
end

#create_booking(authorization, create_booking_body = nil) ⇒ Object

Create a Booking with params

Parameters:

  • authorization (String)

    Required parameter: A valid API key, in the format ‘Token API_KEY’

  • create_booking_body (CreateBookingBody) (defaults to: nil)

    Optional parameter: the content of the request

Returns:

  • CreateBookingResponse response from the API call



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 225

def create_booking(authorization, 
                   create_booking_body = nil)

  # prepare query url
  _query_builder = Configuration.get_base_uri()
  _query_builder << '/bookings/new'
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8',
    'Authorization' => Configuration.authorization,
    'Authorization' => authorization
  }

  # prepare and execute HttpRequest
  _request = @http_client.post _query_url, headers: _headers, parameters: create_booking_body.to_json
  CustomAuth.apply(_request)
  _context = execute_request(_request)

  # validate response against endpoint and global error codes
  if _context.response.status_code == 400
    raise EntitiesErrorErrorException.new 'Bad Request', _context
  elsif _context.response.status_code == 401
    raise EntitiesErrorErrorException.new 'Unauthorized/Missing Token', _context
  elsif _context.response.status_code == 403
    raise EntitiesErrorErrorException.new 'Forbidden', _context
  elsif _context.response.status_code == 422
    raise EntitiesErrorErrorException.new 'Unprocessable Entity', _context
  elsif !_context.response.status_code.between?(200, 208)
    raise APIException.new 'Unexpected error', _context
  end
  validate_response(_context)

  # return appropriate response type
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  return CreateBookingResponse.from_hash(decoded)
end

#get_booking_by_id(authorization, id) ⇒ Object

Return a Booking by id.

Parameters:

  • authorization (String)

    Required parameter: A valid API key, in the format ‘Token API_KEY’

  • id (String)

    Required parameter: Example:

Returns:

  • GetBookingByIdResponse response from the API call



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 179

def get_booking_by_id(authorization, 
                      id)

  # prepare query url
  _query_builder = Configuration.get_base_uri()
  _query_builder << '/bookings/{id}'
  _query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
    'id' => id
  }
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'accept' => 'application/json',
    'Authorization' => Configuration.authorization,
    'Authorization' => authorization
  }

  # prepare and execute HttpRequest
  _request = @http_client.get _query_url, headers: _headers
  CustomAuth.apply(_request)
  _context = execute_request(_request)

  # validate response against endpoint and global error codes
  if _context.response.status_code == 400
    raise EntitiesErrorErrorException.new 'Bad Request', _context
  elsif _context.response.status_code == 401
    raise EntitiesErrorErrorException.new 'Unauthorized/Missing Token', _context
  elsif _context.response.status_code == 403
    raise EntitiesErrorErrorException.new 'Forbidden', _context
  elsif _context.response.status_code == 404
    raise EntitiesErrorErrorException.new 'Not Found', _context
  elsif !_context.response.status_code.between?(200, 208)
    raise APIException.new 'Unexpected error', _context
  end
  validate_response(_context)

  # return appropriate response type
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  return GetBookingByIdResponse.from_hash(decoded)
end

#get_bookings(authorization, user_id = nil, states = nil, booker_id = nil, page = 1, per_page = 10) ⇒ Object

Return list of Bookings.

Parameters:

  • authorization (String)

    Required parameter: A valid API key, in the format ‘Token API_KEY’

  • user_id (Integer) (defaults to: nil)

    Optional parameter: Retrieve Bookings for Resources/Services owned by this User Id. You must be authorized to manage this User Id.

  • states (String) (defaults to: nil)

    Optional parameter: Comma-separated list of Booking states to retrieve only Bookings in those states. Leave blank to retrieve all Bookings.

  • booker_id (Integer) (defaults to: nil)

    Optional parameter: Retrieve Bookings made by Booker Id. Only Bookings for Services/Resources you are authorized to manage will be returned.

  • page (Integer) (defaults to: 1)

    Optional parameter: Page offset to fetch.

  • per_page (Integer) (defaults to: 10)

    Optional parameter: Number of results to return per page.

Returns:

  • GetBookingsResponse response from the API call



76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 76

def get_bookings(authorization, 
                 user_id = nil, 
                 states = nil, 
                 booker_id = nil, 
                 page = 1, 
                 per_page = 10)

  # prepare query url
  _query_builder = Configuration.get_base_uri()
  _query_builder << '/bookings'
  _query_builder = APIHelper.append_url_with_query_parameters _query_builder, {
    'user_id' => user_id,
    'states' => states,
    'booker_id' => booker_id,
    'page' => page,
    'per_page' => per_page
  }, array_serialization: Configuration.array_serialization
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'accept' => 'application/json',
    'Authorization' => Configuration.authorization,
    'Authorization' => authorization
  }

  # prepare and execute HttpRequest
  _request = @http_client.get _query_url, headers: _headers
  CustomAuth.apply(_request)
  _context = execute_request(_request)

  # validate response against endpoint and global error codes
  if _context.response.status_code == 401
    raise EntitiesErrorErrorException.new 'Unauthorized/Missing Token', _context
  elsif _context.response.status_code == 403
    raise EntitiesErrorErrorException.new 'Forbidden', _context
  elsif _context.response.status_code == 404
    raise EntitiesErrorErrorException.new 'Not Found', _context
  elsif !_context.response.status_code.between?(200, 208)
    raise APIException.new 'Unexpected error', _context
  end
  validate_response(_context)

  # return appropriate response type
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  return GetBookingsResponse.from_hash(decoded)
end

#update_booking_by_id(authorization, id, update_booking_by_id_body = nil) ⇒ Object

Update a Booking by id

Parameters:

  • authorization (String)

    Required parameter: A valid API key, in the format ‘Token API_KEY’

  • id (String)

    Required parameter: Example:

  • update_booking_by_id_body (UpdateBookingByIdBody) (defaults to: nil)

    Optional parameter: the content of the request

Returns:

  • UpdateBookingByIdResponse response from the API call



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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gonebusy/controllers/bookings_controller.rb', line 129

def update_booking_by_id(authorization, 
                         id, 
                         update_booking_by_id_body = nil)

  # prepare query url
  _query_builder = Configuration.get_base_uri()
  _query_builder << '/bookings/{id}'
  _query_builder = APIHelper.append_url_with_template_parameters _query_builder, {
    'id' => id
  }
  _query_url = APIHelper.clean_url _query_builder

  # prepare headers
  _headers = {
    'accept' => 'application/json',
    'content-type' => 'application/json; charset=utf-8',
    'Authorization' => Configuration.authorization,
    'Authorization' => authorization
  }

  # prepare and execute HttpRequest
  _request = @http_client.put _query_url, headers: _headers, parameters: update_booking_by_id_body.to_json
  CustomAuth.apply(_request)
  _context = execute_request(_request)

  # validate response against endpoint and global error codes
  if _context.response.status_code == 400
    raise EntitiesErrorErrorException.new 'Bad Request', _context
  elsif _context.response.status_code == 401
    raise EntitiesErrorErrorException.new 'Unauthorized/Missing Token', _context
  elsif _context.response.status_code == 403
    raise EntitiesErrorErrorException.new 'Forbidden', _context
  elsif _context.response.status_code == 404
    raise EntitiesErrorErrorException.new 'Not Found', _context
  elsif _context.response.status_code == 422
    raise EntitiesErrorErrorException.new 'Unprocessable Entity', _context
  elsif !_context.response.status_code.between?(200, 208)
    raise APIException.new 'Unexpected error', _context
  end
  validate_response(_context)

  # return appropriate response type
  decoded = APIHelper.json_deserialize(_context.response.raw_body)
  return UpdateBookingByIdResponse.from_hash(decoded)
end