Class: BookingSync::API::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Accounts, Amenities, Applications, ApplicationsPeriodsRentals, Attachments, Availabilities, Bathrooms, Bedrooms, BookingComments, Bookings, BookingsFees, BookingsPayments, BookingsTags, BookingsTaxes, ChangeOvers, Clients, Contacts, Conversations, Destinations, Fees, Hosts, Inquiries, LivingRooms, Messages, NightlyRateMaps, Participants, PaymentGateways, Payments, Periods, Photos, PreferencesGeneralSettings, Rates, RatesRules, RatesTables, RentalAgreements, RentalCancelationPolicies, RentalCancelationPolicyItems, RentalUrls, Rentals, RentalsAmenities, RentalsContentsOverrides, RentalsFees, Reviews, Seasons, Sources, SpecialOffers, StrictBookings, Taxes
Defined in:
lib/bookingsync/api/client.rb,
lib/bookingsync/api/client/fees.rb,
lib/bookingsync/api/client/hosts.rb,
lib/bookingsync/api/client/rates.rb,
lib/bookingsync/api/client/taxes.rb,
lib/bookingsync/api/client/photos.rb,
lib/bookingsync/api/client/clients.rb,
lib/bookingsync/api/client/periods.rb,
lib/bookingsync/api/client/rentals.rb,
lib/bookingsync/api/client/reviews.rb,
lib/bookingsync/api/client/seasons.rb,
lib/bookingsync/api/client/sources.rb,
lib/bookingsync/api/client/accounts.rb,
lib/bookingsync/api/client/bedrooms.rb,
lib/bookingsync/api/client/bookings.rb,
lib/bookingsync/api/client/contacts.rb,
lib/bookingsync/api/client/messages.rb,
lib/bookingsync/api/client/payments.rb,
lib/bookingsync/api/client/amenities.rb,
lib/bookingsync/api/client/bathrooms.rb,
lib/bookingsync/api/client/inquiries.rb,
lib/bookingsync/api/client/attachments.rb,
lib/bookingsync/api/client/rates_rules.rb,
lib/bookingsync/api/client/rental_urls.rb,
lib/bookingsync/api/client/applications.rb,
lib/bookingsync/api/client/change_overs.rb,
lib/bookingsync/api/client/destinations.rb,
lib/bookingsync/api/client/living_rooms.rb,
lib/bookingsync/api/client/participants.rb,
lib/bookingsync/api/client/rates_tables.rb,
lib/bookingsync/api/client/rentals_fees.rb,
lib/bookingsync/api/client/bookings_fees.rb,
lib/bookingsync/api/client/bookings_tags.rb,
lib/bookingsync/api/client/conversations.rb,
lib/bookingsync/api/client/availabilities.rb,
lib/bookingsync/api/client/bookings_taxes.rb,
lib/bookingsync/api/client/special_offers.rb,
lib/bookingsync/api/client/strict_bookings.rb,
lib/bookingsync/api/client/booking_comments.rb,
lib/bookingsync/api/client/payment_gateways.rb,
lib/bookingsync/api/client/bookings_payments.rb,
lib/bookingsync/api/client/nightly_rate_maps.rb,
lib/bookingsync/api/client/rental_agreements.rb,
lib/bookingsync/api/client/rentals_amenities.rb,
lib/bookingsync/api/client/rentals_contents_overrides.rb,
lib/bookingsync/api/client/rental_cancelation_policies.rb,
lib/bookingsync/api/client/applications_periods_rentals.rb,
lib/bookingsync/api/client/preferences_general_settings.rb,
lib/bookingsync/api/client/rental_cancelation_policy_items.rb

Defined Under Namespace

Modules: Accounts, Amenities, Applications, ApplicationsPeriodsRentals, Attachments, Availabilities, Bathrooms, Bedrooms, BookingComments, Bookings, BookingsFees, BookingsPayments, BookingsTags, BookingsTaxes, ChangeOvers, Clients, Contacts, Conversations, Destinations, Fees, Hosts, Inquiries, LivingRooms, Messages, NightlyRateMaps, NoopInstrumenter, Participants, PaymentGateways, Payments, Periods, Photos, PreferencesGeneralSettings, Rates, RatesRules, RatesTables, RentalAgreements, RentalCancelationPolicies, RentalCancelationPolicyItems, RentalUrls, Rentals, RentalsAmenities, RentalsContentsOverrides, RentalsFees, Reviews, Seasons, Sources, SpecialOffers, StrictBookings, Taxes

Constant Summary collapse

MEDIA_TYPE =
"application/vnd.api+json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Taxes

#tax, #taxes

Methods included from Sources

#create_source, #edit_source, #source, #sources

Methods included from SpecialOffers

#create_special_offer, #delete_special_offer, #edit_special_offer, #special_offer, #special_offers

Methods included from Seasons

#create_season, #delete_season, #edit_season, #season, #seasons

Methods included from Reviews

#create_review, #review, #reviews

Methods included from RentalUrls

#cancel_rental_url, #create_rental_url, #edit_rental_url, #rental_url, #rental_urls

Methods included from RentalsContentsOverrides

#create_rentals_contents_override, #delete_rentals_contents_override, #edit_rentals_contents_override, #rentals_contents_override, #rentals_contents_overrides

Methods included from RentalCancelationPolicyItems

#rental_cancelation_policy_item, #rental_cancelation_policy_items

Methods included from RentalCancelationPolicies

#rental_cancelation_policies, #rental_cancelation_policy

Methods included from RentalAgreements

#create_rental_agreement, #create_rental_agreement_for_booking, #create_rental_agreement_for_rental, #rental_agreement, #rental_agreements

Methods included from RentalsAmenities

#create_rentals_amenity, #delete_rentals_amenity, #edit_rentals_amenity, #rentals_amenities, #rentals_amenity

Methods included from RentalsFees

#create_rentals_fee, #rentals_fee, #rentals_fees

Methods included from Rentals

#create_rental, #delete_rental, #edit_rental, #rental, #rentals, #rentals_meta, #rentals_search

Methods included from RatesTables

#create_rates_table, #delete_rates_table, #edit_rates_table, #rates_table, #rates_tables

Methods included from RatesRules

#create_rates_rule, #delete_rates_rule, #edit_rates_rule, #rates_rule, #rates_rules

Methods included from Rates

#rate, #rates

Methods included from Photos

#create_photo, #delete_photo, #edit_photo, #photo, #photos

Methods included from PreferencesGeneralSettings

#edit_preferences_general_setting, #preferences_general_settings

Methods included from PaymentGateways

#payment_gateway, #payment_gateways

Methods included from Payments

#cancel_payment, #create_payment, #edit_payment, #payment, #payments

Methods included from Periods

#create_period, #delete_period, #edit_period, #period, #periods

Methods included from Participants

#create_participant, #edit_participant, #participant, #participants

Methods included from StrictBookings

#create_strict_booking

Methods included from NightlyRateMaps

#edit_nightly_rate_map, #nightly_rate_map, #nightly_rate_maps

Methods included from Messages

#add_attachment_to_message, #create_message, #edit_message, #message, #messages

Methods included from LivingRooms

#cancel_living_room, #create_living_room, #edit_living_room, #living_room, #living_rooms

Methods included from Inquiries

#create_inquiry, #inquiries, #inquiry

Methods included from Hosts

#create_host, #edit_host, #host, #hosts

Methods included from Fees

#create_fee, #fee, #fees

Methods included from Destinations

#destination, #destinations

Methods included from Contacts

#contact, #contacts, #create_contact, #delete_contact, #edit_contact

Methods included from Clients

#client, #clients, #create_client, #edit_client

Methods included from Conversations

#connect_booking_to_conversation, #conversation, #conversations, #create_conversation, #disconnect_booking_from_conversation, #edit_conversation

Methods included from ChangeOvers

#change_over, #change_overs

Methods included from BookingsTaxes

#bookings_tax, #bookings_taxes

Methods included from BookingsTags

#bookings_tag, #bookings_tags

Methods included from BookingsPayments

#bookings_payment, #bookings_payments

Methods included from BookingsFees

#bookings_fee, #bookings_fees

Methods included from BookingComments

#booking_comment, #booking_comments, #create_booking_comment, #delete_booking_comment, #edit_booking_comment

Methods included from Bookings

#add_bookings_fee, #booking, #bookings, #cancel_booking, #create_booking, #edit_booking, #remove_bookings_fee

Methods included from Bedrooms

#bedroom, #bedrooms, #cancel_bedroom, #create_bedroom, #edit_bedroom

Methods included from Bathrooms

#bathroom, #bathrooms, #cancel_bathroom, #create_bathroom, #edit_bathroom

Methods included from Availabilities

#availabilities, #availability

Methods included from Attachments

#attachment, #attachments, #create_attachment, #edit_attachment

Methods included from ApplicationsPeriodsRentals

#applications_periods_rental, #applications_periods_rentals, #create_applications_periods_rental, #edit_applications_periods_rental

Methods included from Applications

#application, #applications, #edit_application

Methods included from Amenities

#amenities, #amenity

Methods included from Accounts

#account, #accounts

Constructor Details

#initialize(token, options = {}) {|@conn| ... } ⇒ BookingSync::API::Client

Initialize new Client

Parameters:

  • token (String)

    OAuth token

  • options (Hash) (defaults to: {})

Options Hash (options):

  • base_url: (String)

    Base URL to BookingSync site

  • logger: (Logger)

    Logger where headers and body of every request and response will be logged.

  • instrumenter: (Module)

    A module that responds to instrument, usually ActiveSupport::Notifications.

Yields:

  • (@conn)


126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bookingsync/api/client.rb', line 126

def initialize(token, options = {})
  @token = token
  @logger = options[:logger] || default_logger
  @instrumenter = options[:instrumenter] || NoopInstrumenter
  @base_url = options[:base_url]
  @serializer = Serializer.new
  @conn = Faraday.new(faraday_options)
  @conn.headers[:accept] = MEDIA_TYPE
  @conn.headers[:content_type] = MEDIA_TYPE
  @conn.headers[:user_agent] = user_agent
  @conn.url_prefix = api_endpoint
  yield @conn if block_given?
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



112
113
114
# File 'lib/bookingsync/api/client.rb', line 112

def last_response
  @last_response
end

#loggerObject (readonly)

Returns the value of attribute logger.



112
113
114
# File 'lib/bookingsync/api/client.rb', line 112

def logger
  @logger
end

#pagination_first_responseObject (readonly)

Returns the value of attribute pagination_first_response.



112
113
114
# File 'lib/bookingsync/api/client.rb', line 112

def pagination_first_response
  @pagination_first_response
end

#tokenObject (readonly)

Returns the value of attribute token.



112
113
114
# File 'lib/bookingsync/api/client.rb', line 112

def token
  @token
end

Instance Method Details

#api_endpointString

Return API endpoint

Returns:

  • (String)

    URL to API endpoint



188
189
190
# File 'lib/bookingsync/api/client.rb', line 188

def api_endpoint
  URI.join(base_url, "api/v3").to_s
end

#call(method, path, data = nil, options = nil) ⇒ BookingSync::API::Response

Make a HTTP request to given path and returns Response object.

Parameters:

  • method (Symbol)

    HTTP verb to use.

  • path (String)

    The path, relative to #api_endpoint.

  • data (Hash) (defaults to: nil)

    Data to be send in the request’s body it can include query: key with requests params for GET requests

  • options (Hash) (defaults to: nil)

    A customizable set of request options.

Returns:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bookingsync/api/client.rb', line 258

def call(method, path, data = nil, options = nil)
  instrument("call.bookingsync_api", method: method, path: path) do
    if [:get, :head].include?(method)
      options = data
      data = {}
    end
    options ||= {}
    options[:headers] ||= {}
    options[:headers]["Authorization"] = "Bearer #{token}"

    if options.has_key?(:query)
      if options[:query].has_key?(:ids)
        ids = Array(options[:query].delete(:ids)).join(",")
        path = "#{path}/#{ids}"
      end
      options[:query].keys.each do |key|
        if options[:query][key].is_a?(Array)
          options[:query][key] = options[:query][key].join(",")
        end
      end
    end

    url = expand_url(path, options[:uri])
    res = @conn.send(method, url) do |req|
      if data
        req.body = data.is_a?(String) ? data : encode_body(data)
      end
      if params = options[:query]
        req.params.update params
      end
      req.headers.update options[:headers]
    end
    handle_response(res)
  end
end

#decode_body(str) ⇒ Object

Decode a String response body to a Resource.

Parameters:

  • str (String)

    The String body from the response.

Returns:

  • (Object)

    Object resource



204
205
206
# File 'lib/bookingsync/api/client.rb', line 204

def decode_body(str)
  @serializer.decode(str)
end

#delete(path, options = {}) ⇒ Array<BookingSync::API::Resource>

Make a HTTP DELETE request

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body params for the request

Returns:



181
182
183
# File 'lib/bookingsync/api/client.rb', line 181

def delete(path, options = {})
  request :delete, path, options
end

#encode_body(data) ⇒ String

Encode an object to a string for the API request.

Parameters:

  • data (Object)

    The Hash or Resource that is being sent.

Returns:

  • (String)

    Object encoded into JSON string



196
197
198
# File 'lib/bookingsync/api/client.rb', line 196

def encode_body(data)
  @serializer.encode(data)
end

#get(path, options = {}) ⇒ Array<BookingSync::API::Resource>

Make a HTTP GET request

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Query params for the request

Returns:



145
146
147
# File 'lib/bookingsync/api/client.rb', line 145

def get(path, options = {})
  request :get, path, query: options
end

#paginate(path, options = {}, &block) ⇒ Array<BookingSync::API::Resource>

Make a HTTP GET or POST request to a path with pagination support.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • per_page: (Integer)

    Number of resources per page

  • page: (Integer)

    Number of page to return

  • auto_paginate: (Boolean)

    If true, all resources will be returned. It makes multiple requestes underneath and joins the results.

Yield Returns:

Returns:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/bookingsync/api/client.rb', line 233

def paginate(path, options = {}, &block)
  instrument("paginate.bookingsync_api", path: path) do
    request_settings = {
      auto_paginate: options.delete(:auto_paginate),
      request_method: options.delete(:request_method) || :get
    }

    if block_given?
      data = fetch_with_block(path, options, request_settings, &block)
    else
      data = fetch_with_paginate(path, options, request_settings)
    end

    data
  end
end

#patch(path, options = {}) ⇒ Array<BookingSync::API::Resource>

Make a HTTP PATCH request

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body params for the request

Returns:



172
173
174
# File 'lib/bookingsync/api/client.rb', line 172

def patch(path, options = {})
  request :patch, path, options
end

#post(path, options = {}) ⇒ Array<BookingSync::API::Resource>

Make a HTTP POST request

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body params for the request

Returns:



154
155
156
# File 'lib/bookingsync/api/client.rb', line 154

def post(path, options = {})
  request :post, path, options
end

#put(path, options = {}) ⇒ Array<BookingSync::API::Resource>

Make a HTTP PUT request

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body params for the request

Returns:



163
164
165
# File 'lib/bookingsync/api/client.rb', line 163

def put(path, options = {})
  request :put, path, options
end

#request(method, path, data = nil, options = nil) ⇒ Array<BookingSync::API::Resource>

Make a HTTP request to a path and returns an Array of Resources

Parameters:

  • method (Symbol)

    HTTP verb to use.

  • path (String)

    The path, relative to #api_endpoint.

  • data (Hash) (defaults to: nil)

    Data to be send in the request’s body it can include query: key with requests params for GET requests

  • options (Hash) (defaults to: nil)

    A customizable set of request options.

Returns:



216
217
218
219
220
221
# File 'lib/bookingsync/api/client.rb', line 216

def request(method, path, data = nil, options = nil)
  instrument("request.bookingsync_api", method: method, path: path) do
    response = call(method, path, data, options)
    response.respond_to?(:resources) ? response.resources : response
  end
end

#with_headers(extra_headers = {}, &block) ⇒ Array<BookingSync::API::Resource>|BookingSync::API::Resource|String|Object

Yields client with temporarily modified headers.

Parameters:

  • extra_headers (Hash) (defaults to: {})

    Additional headers added to next request.

Yield Returns:

Returns:



299
300
301
302
303
304
305
# File 'lib/bookingsync/api/client.rb', line 299

def with_headers(extra_headers = {}, &block)
  original_headers = @conn.headers.dup
  @conn.headers.merge!(extra_headers)
  result = yield self
  @conn.headers = original_headers
  result
end