Class: BookingSync::API::Client
- Inherits:
-
Object
- Object
- BookingSync::API::Client
- Extended by:
- Forwardable
- Includes:
- Accounts, Amenities, Availabilities, BillingAddresses, Bookings, BookingsFees, BookingsPayments, BookingsTaxes, Clients, Destinations, Fees, Inquiries, InstantBookings, Payments, Periods, Photos, Rates, RatesRules, RatesTables, RentalAgreements, Rentals, RentalsAmenities, RentalsFees, Reviews, Seasons, Sources, SpecialOffers, Taxes
- Defined in:
- lib/bookingsync/api/client.rb,
lib/bookingsync/api/client/fees.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/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/rentals_fees.rb,
lib/bookingsync/api/client/bookings_fees.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/instant_bookings.rb,
lib/bookingsync/api/client/billing_addresses.rb,
lib/bookingsync/api/client/bookings_payments.rb,
lib/bookingsync/api/client/rental_agreements.rb,
lib/bookingsync/api/client/rentals_amenities.rb
Defined Under Namespace
Modules: Accounts, Amenities, Availabilities, BillingAddresses, Bookings, BookingsFees, BookingsPayments, BookingsTaxes, Clients, Destinations, Fees, Inquiries, InstantBookings, NoopInstrumenter, Payments, Periods, Photos, Rates, RatesRules, RatesTables, RentalAgreements, Rentals, RentalsAmenities, RentalsFees, Reviews, Seasons, Sources, SpecialOffers, Taxes
Constant Summary collapse
- MEDIA_TYPE =
"application/vnd.api+json"
Instance Attribute Summary collapse
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#api_endpoint ⇒ String
Return API endpoint.
-
#call(method, path, data = nil, options = nil) ⇒ BookingSync::API::Response
Make a HTTP request to given path and returns Response object.
-
#decode_body(str) ⇒ Object
Decode a String response body to a Resource.
-
#delete(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP DELETE request.
-
#encode_body(data) ⇒ String
Encode an object to a string for the API request.
-
#get(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP GET request.
-
#initialize(token, options = {}) {|@conn| ... } ⇒ BookingSync::API::Client
constructor
Initialize new Client.
-
#paginate(path, options = {}, &block) ⇒ Array<BookingSync::API::Resource>
Make a HTTP GET request to a path with pagination support.
-
#post(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP POST request.
-
#put(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP PUT request.
-
#request(method, path, data = nil, options = nil) ⇒ Array<BookingSync::API::Resource>
Make a HTTP request to a path and returns an Array of Resources.
Methods included from Taxes
Methods included from Sources
#create_source, #edit_source, #sources
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
Methods included from RentalAgreements
#create_rental_agreement, #create_rental_agreement_for_booking, #create_rental_agreement_for_rental, #rental_agreements
Methods included from RentalsAmenities
#create_rentals_amenity, #rentals_amenities, #rentals_amenity
Methods included from RentalsFees
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_tables
Methods included from RatesRules
Methods included from Rates
Methods included from Photos
#create_photo, #delete_photo, #edit_photo, #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 InstantBookings
Methods included from Inquiries
Methods included from Fees
Methods included from Destinations
Methods included from Clients
#clients, #create_client, #edit_client
Methods included from BookingsTaxes
Methods included from BookingsPayments
Methods included from BookingsFees
Methods included from Bookings
#booking, #bookings, #cancel_booking, #create_booking, #edit_booking
Methods included from BillingAddresses
Methods included from Availabilities
#availabilities, #availability
Methods included from Amenities
Methods included from Accounts
Constructor Details
#initialize(token, options = {}) {|@conn| ... } ⇒ BookingSync::API::Client
Initialize new Client
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bookingsync/api/client.rb', line 86 def initialize(token, = {}) @token = token @logger = [:logger] || default_logger @instrumenter = [:instrumenter] || NoopInstrumenter @base_url = [:base_url] @serializer = Serializer.new @conn = Faraday.new() @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_response ⇒ Object (readonly)
Returns the value of attribute last_response.
72 73 74 |
# File 'lib/bookingsync/api/client.rb', line 72 def last_response @last_response end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
72 73 74 |
# File 'lib/bookingsync/api/client.rb', line 72 def logger @logger end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
72 73 74 |
# File 'lib/bookingsync/api/client.rb', line 72 def token @token end |
Instance Method Details
#api_endpoint ⇒ String
Return API endpoint
139 140 141 |
# File 'lib/bookingsync/api/client.rb', line 139 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.
216 217 218 219 220 221 222 223 224 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 |
# File 'lib/bookingsync/api/client.rb', line 216 def call(method, path, data = nil, = nil) instrument("call.bookingsync_api", method: method, path: path) do if [:get, :head].include?(method) = data data = nil end ||= {} [:headers] ||= {} [:headers]["Authorization"] = "Bearer #{token}" if .has_key?(:query) if [:query].has_key?(:ids) ids = Array([:query].delete(:ids)).join(',') path = "#{path}/#{ids}" end [:query].keys.each do |key| if [:query][key].is_a?(Array) [:query][key] = [:query][key].join(",") end end end url = (path, [:uri]) res = @conn.send(method, url) do |req| if data req.body = data.is_a?(String) ? data : encode_body(data) end if params = [:query] req.params.update params end req.headers.update [:headers] end handle_response(res) end end |
#decode_body(str) ⇒ Object
Decode a String response body to a Resource.
155 156 157 |
# File 'lib/bookingsync/api/client.rb', line 155 def decode_body(str) @serializer.decode(str) end |
#delete(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP DELETE request
132 133 134 |
# File 'lib/bookingsync/api/client.rb', line 132 def delete(path, = {}) request :delete, path, end |
#encode_body(data) ⇒ String
Encode an object to a string for the API request.
147 148 149 |
# File 'lib/bookingsync/api/client.rb', line 147 def encode_body(data) @serializer.encode(data) end |
#get(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP GET request
105 106 107 |
# File 'lib/bookingsync/api/client.rb', line 105 def get(path, = {}) request :get, path, query: end |
#paginate(path, options = {}, &block) ⇒ Array<BookingSync::API::Resource>
Make a HTTP GET request to a path with pagination support.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/bookingsync/api/client.rb', line 184 def paginate(path, = {}, &block) instrument("paginate.bookingsync_api", path: path) do auto_paginate = .delete(:auto_paginate) response = call(:get, path, query: ) data = response.resources.dup if (block_given? or auto_paginate) && response.relations[: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.relations[:next] response = response.relations[:next].get end end data end end |
#post(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP POST request
114 115 116 |
# File 'lib/bookingsync/api/client.rb', line 114 def post(path, = {}) request :post, path, end |
#put(path, options = {}) ⇒ Array<BookingSync::API::Resource>
Make a HTTP PUT request
123 124 125 |
# File 'lib/bookingsync/api/client.rb', line 123 def put(path, = {}) request :put, path, 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
167 168 169 170 171 172 |
# File 'lib/bookingsync/api/client.rb', line 167 def request(method, path, data = nil, = nil) instrument("request.bookingsync_api", method: method, path: path) do response = call(method, path, data, ) response.respond_to?(:resources) ? response.resources : response end end |