class UCLAPI::Client
Constants
- ENDPOINT
Attributes
debug[RW]
http[RW]
token[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/uclapi/client.rb, line 15 def initialize(options = {}) @token = options.fetch(:token) do ENV['UCLAPI_TOKEN'] || raise(KeyError, "Missing required argument: token") end @debug = (options[:debug] || ENV['UCLAPI_DEBUG']) ? true : false @http = Net::HTTP.new(ENDPOINT, Net::HTTP.https_default_port) http.use_ssl = true http.set_debug_output $stderr if debug end
Public Instance Methods
get(path, params = {})
click to toggle source
# File lib/uclapi/client.rb, line 35 def get(path, params = {}) uri = uri_for(path, params) message = Net::HTTP::Get.new(uri.request_uri) transmit(message) end
roombookings()
click to toggle source
# File lib/uclapi/client.rb, line 27 def roombookings UCLAPI::Client::Roombookings.new(self) end
search()
click to toggle source
# File lib/uclapi/client.rb, line 31 def search UCLAPI::Client::Search.new(self) end
Private Instance Methods
json?(response)
click to toggle source
# File lib/uclapi/client.rb, line 76 def json?(response) content_type = response['Content-Type'] json_header = content_type && content_type.split(';').first == 'application/json' has_body = response.body && response.body.length > 0 json_header && has_body end
parse(response)
click to toggle source
# File lib/uclapi/client.rb, line 55 def parse response data = json?(response) ? JSON.parse(response.body) : response.body case response when Net::HTTPSuccess data when Net::HTTPNotFound raise NotFoundError, "Record not found (404)" when Net::HTTPBadRequest raise BadRequestError, data when Net::HTTPUnauthorized raise UnauthorizedError, "Access Denied (401)" else if json?(response) raise ServerError, "HTTP #{response.code}: #{JSON.parse(response.body)}" else raise ServerError, "HTTP #{response.code}: #{response.body}" end end end
transmit(message)
click to toggle source
# File lib/uclapi/client.rb, line 51 def transmit(message) parse(http.request(message)) end
uri_for(path, params = {})
click to toggle source
# File lib/uclapi/client.rb, line 43 def uri_for(path, params = {}) uri = URI("https://#{ENDPOINT}#{path}") uri.query = URI.encode_www_form(params.merge({ token: token })) uri end