class CobotClient::ApiClient
Attributes
retry_time[RW]
user_agent[RW]
Public Class Methods
new(access_token)
click to toggle source
# File lib/cobot_client/api_client.rb, line 13 def initialize(access_token) @access_token = access_token end
Public Instance Methods
create_booking(subdomain, resource_id, attributes)
click to toggle source
# File lib/cobot_client/api_client.rb, line 21 def create_booking(subdomain, resource_id, attributes) post subdomain, "/resources/#{resource_id}/bookings", attributes end
delete(*args)
click to toggle source
args: either a full URL or subdomain, path
# File lib/cobot_client/api_client.rb, line 59 def delete(*args) url, subdomain, path, _ = parse_args(*args) rewrap_errors do RestClient.delete(build_url(url || subdomain, path), headers) end end
delete_booking(subdomain, id)
click to toggle source
# File lib/cobot_client/api_client.rb, line 29 def delete_booking(subdomain, id) delete subdomain, "/bookings/#{id}" end
get(*args)
click to toggle source
args: either a full URL or subdomain, path, plus an optional params hash
# File lib/cobot_client/api_client.rb, line 48 def get(*args) url, subdomain, path, params = parse_args(*args) JSON.parse( rewrap_errors do RestClient.get( build_url(url || subdomain, path, params), headers).body end, symbolize_names: true) end
get_resources(subdomain)
click to toggle source
# File lib/cobot_client/api_client.rb, line 17 def get_resources(subdomain) get subdomain, '/resources' end
patch(*args)
click to toggle source
# File lib/cobot_client/api_client.rb, line 43 def patch(*args) request :patch, *args end
post(*args)
click to toggle source
args: either a full URL or subdomain, path, plus a body as hash
# File lib/cobot_client/api_client.rb, line 34 def post(*args) request :post, *args end
put(*args)
click to toggle source
args: either a full URL or subdomain, path, plus a body as hash
# File lib/cobot_client/api_client.rb, line 39 def put(*args) request :put, *args end
update_booking(subdomain, id, attributes)
click to toggle source
# File lib/cobot_client/api_client.rb, line 25 def update_booking(subdomain, id, attributes) put subdomain, "/bookings/#{id}", attributes end
Private Instance Methods
build_url(subdomain_or_url, path, params = {})
click to toggle source
# File lib/cobot_client/api_client.rb, line 118 def build_url(subdomain_or_url, path, params = {}) if path cobot_url(subdomain_or_url, "/api#{path}", params: params) else uri = URI.parse(subdomain_or_url) uri.query = URI.encode_www_form(params) if params && params.any? uri.to_s end end
content_type_header()
click to toggle source
# File lib/cobot_client/api_client.rb, line 128 def content_type_header {'Content-Type' => 'application/json'} end
headers()
click to toggle source
# File lib/cobot_client/api_client.rb, line 132 def headers { 'Authorization' => "Bearer #{@access_token}", 'User-Agent' => self.class.user_agent || "Cobot Client #{CobotClient::VERSION}" } end
parse_args(*args)
click to toggle source
# File lib/cobot_client/api_client.rb, line 100 def parse_args(*args) if args.size == 3 || (args.size == 2 && args[0].match(%r{https?://})) params = args.pop else params = {} end if args.size == 1 url = args[0] path = nil subdomain = nil else subdomain = args[0] path = args[1] url = nil end [url, subdomain, path, params] end
request(method, *args)
click to toggle source
# File lib/cobot_client/api_client.rb, line 68 def request(method, *args) url, subdomain, path, body = parse_args(*args) rewrap_errors do response = RestClient.public_send(method, build_url(url || subdomain, path), body.to_json, headers.merge(content_type_header)) JSON.parse response.body, symbolize_names: true unless response.code == 204 end end
retry_errors() { || ... }
click to toggle source
# File lib/cobot_client/api_client.rb, line 85 def retry_errors retries = 0 begin yield rescue RestClient::BadGateway, SocketError, RestClient::RequestTimeout, CobotClient::InternalServerError => e if retries < 3 sleep self.class.retry_time retries += 1 retry else raise e end end end
rewrap_errors(&block)
click to toggle source
# File lib/cobot_client/api_client.rb, line 79 def rewrap_errors(&block) retry_errors(&block) rescue RestClient::Exception => e fail CobotClient::Exceptions::EXCEPTIONS_MAP[e.class].new(e.response) end