Class: BookingSync::API::Client

Inherits:
Object
  • Object
show all
Includes:
Amenities, BillingAddresses, Bookings, BookingsPayments, Clients, Destinations, Inquiries, Payments, Periods, Photos, Rates, RatesRules, RatesTables, RentalAgreements, Rentals, Reviews, Seasons, SpecialOffers
Defined in:
lib/bookingsync/api/client.rb,
lib/bookingsync/api/client/rates.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/bookings.rb,
lib/bookingsync/api/client/payments.rb,
lib/bookingsync/api/client/amenities.rb,
lib/bookingsync/api/client/inquiries.rb,
lib/bookingsync/api/client/rates_rules.rb,
lib/bookingsync/api/client/destinations.rb,
lib/bookingsync/api/client/rates_tables.rb,
lib/bookingsync/api/client/special_offers.rb,
lib/bookingsync/api/client/billing_addresses.rb,
lib/bookingsync/api/client/bookings_payments.rb,
lib/bookingsync/api/client/rental_agreements.rb

Defined Under Namespace

Modules: Amenities, BillingAddresses, Bookings, BookingsPayments, Clients, Destinations, Inquiries, Payments, Periods, Photos, Rates, RatesRules, RatesTables, RentalAgreements, Rentals, Reviews, Seasons, SpecialOffers

Constant Summary collapse

MEDIA_TYPE =
"application/vnd.api+json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SpecialOffers

#create_special_offer, #delete_special_offer, #edit_special_offer, #special_offers

Methods included from Seasons

#create_season, #delete_season, #edit_season, #seasons

Methods included from Reviews

#create_review, #reviews

Methods included from RentalAgreements

#create_rental_agreement, #create_rental_agreement_for_booking, #create_rental_agreement_for_rental, #rental_agreements

Methods included from Rentals

#cancel_rental, #create_rental, #edit_rental, #rental, #rentals

Methods included from RatesTables

#create_rates_table, #delete_rates_table, #edit_rates_table, #rates_tables

Methods included from RatesRules

#rates_rules

Methods included from Rates

#rates

Methods included from Photos

#photos

Methods included from Payments

#cancel_payment, #create_payment, #edit_payment, #payments

Methods included from Periods

#create_period, #delete_period, #edit_period, #periods

Methods included from Inquiries

#create_inquiry, #inquiries

Methods included from Destinations

#destinations

Methods included from Clients

#clients, #create_client, #edit_client

Methods included from BookingsPayments

#bookings_payments

Methods included from Bookings

#bookings, #cancel_booking, #create_booking, #edit_booking

Methods included from BillingAddresses

#billing_addresses

Methods included from Amenities

#amenities

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

Yields:

  • (@conn)


57
58
59
60
61
62
63
64
65
66
# File 'lib/bookingsync/api/client.rb', line 57

def initialize(token, options = {})
  @token = token
  @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.url_prefix = api_endpoint
  yield @conn if block_given?
end

Instance Attribute Details

#token ⇒ Object (readonly)

Returns the value of attribute token.



49
50
51
# File 'lib/bookingsync/api/client.rb', line 49

def token
  @token
end

Instance Method Details

#api_endpoint ⇒ String

Return API endpoint

Returns:

  • (String) —

    URL to API endpoint



107
108
109
# File 'lib/bookingsync/api/client.rb', line 107

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:



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
# File 'lib/bookingsync/api/client.rb', line 180

def call(method, path, data = nil, options = nil)
  if [:get, :head].include?(method)
    options = data
    data = nil
  end
  options ||= {}

  if options.has_key?(:query)
    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
    if headers = options[:headers]
      req.headers.update headers
    end
  end
  handle_response(res)
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



123
124
125
# File 'lib/bookingsync/api/client.rb', line 123

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:



100
101
102
# File 'lib/bookingsync/api/client.rb', line 100

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



115
116
117
# File 'lib/bookingsync/api/client.rb', line 115

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:



73
74
75
# File 'lib/bookingsync/api/client.rb', line 73

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

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

Make a HTTP GET 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:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bookingsync/api/client.rb', line 150

def paginate(path, options = {}, &block)
  auto_paginate = options.delete(:auto_paginate)
  response = call(:get, path, query: options)
  data = response.resources.dup

  if (block_given? or auto_paginate) && response.rels[:next]
    first_request = true
    loop do
      if block_given?
        yield(response.resources)
      elsif auto_paginate
        data.concat(response.resources) unless first_request
        first_request = false
      end
      break unless response.rels[:next]
      response = response.rels[:next].get
    end
  end

  data
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:



82
83
84
# File 'lib/bookingsync/api/client.rb', line 82

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:



91
92
93
# File 'lib/bookingsync/api/client.rb', line 91

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:



135
136
137
138
# File 'lib/bookingsync/api/client.rb', line 135

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