Class: OpenbookingClientRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/openbooking_client_ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, logger = Logger.new(STDOUT)) ⇒ OpenbookingClientRuby

Returns a new instance of OpenbookingClientRuby.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/openbooking_client_ruby.rb', line 10

def initialize(uri, logger=Logger.new(STDOUT))
	RestClient.proxy = ENV["PROXY"] unless ENV["PROXY"].nil?

	@logger = logger
	@opts = {}
	ob_url = URI::parse(uri)

	if ob_url.userinfo
		auth = ob_url.userinfo.split(':')
		@opts[:user] = auth[0]
		@opts[:pass] = auth[1]
	end

	@opts[:url] = "#{ob_url.scheme}://#{ob_url.host}:#{ob_url.port}"
end

Instance Method Details

#authObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/openbooking_client_ruby.rb', line 26

def auth
	@logger.debug 'Get OpenBooking Token'
	response = post '/authenticate', {
			:user => @opts[:user],
			:pass => @opts[:pass]
	}

	@logger.debug "Got OpenBooking Token, ID: #{response[:access_token]}"
	@token = response[:access_token]
end

#auth_as(username) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/openbooking_client_ruby.rb', line 37

def auth_as(username)
	auth
	all_users = get "/users/all?user=#{username}&fields=id,user,name,group"
	user = all_users[0]
	token = post("/tokens/"+user[:id], {scopes: ["master"]})
	use_token(token[:access_token])
end

#closeObject



112
113
114
115
# File 'lib/openbooking_client_ruby.rb', line 112

def close
	del "/token/#{@token}"
	remove_instance_variable :@token
end

#del(path, params = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/openbooking_client_ruby.rb', line 101

def del(path, params = {})
	qs = URI.encode_www_form params
	uri = "#{@opts[:url]}#{path}?#{qs}"
	headers = { :accept => :json }
	headers[:authorization] = "Bearer #{@token}"
	JSON.parse RestClient.send('delete', uri, headers), :symbolize_names => true
rescue Exception => e
	@logger.fatal "Couldn't request delete: #{path} #{params}"
	pp e
end

#get(path, params = {}, headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openbooking_client_ruby.rb', line 50

def get(path, params = {}, headers = {})
	qs = URI.encode_www_form params
	uri = "#{@opts[:url]}#{path}?#{qs}"
	@logger.debug "GET #{uri}"
	headers[:accept] = "json"
	headers[:authorization] = "Bearer #{@token}"
	JSON.parse RestClient.send('get', uri, headers), :symbolize_names => true
rescue Exception => e
	puts "Couldn't request get: #{path} #{params}"
	pp e
end

#patch(path, params = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/openbooking_client_ruby.rb', line 89

def patch(path, params = {})
	uri = "#{@opts[:url]}#{path}"
	headers = { :accept => :json, :content_type => :json }
	data = params.to_json
	headers[:authorization] = "Bearer #{@token}"
	JSON.parse RestClient.send('patch', uri, data, headers), :symbolize_names => true
rescue Exception => e
	@logger.fatal "Couln't request patch: #{path} #{params}"
	pp e
end

#post(path, params = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openbooking_client_ruby.rb', line 62

def post(path, params = {})
	uri = "#{@opts[:url]}#{path}"

	headers = { :accept => :json, :content_type => :json }
	headers[:authorization] = "Bearer #{@token}"

	data = params.to_json
	if !data
		data = {}.to_json
	end
	JSON.parse RestClient.send('post', uri, data, headers), :symbolize_names => true
rescue Exception => e
	@logger.fatal "Couln't request post: #{path} #{params}"
	pp e
end

#put(path, params = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/openbooking_client_ruby.rb', line 78

def put(path, params = {})
	uri = "#{@opts[:url]}#{path}"
	headers = { :accept => :json, :content_type => :json }
	data = params.to_json
	headers[:authorization] = "Bearer #{@token}"
	JSON.parse RestClient.send('put', uri, data, headers), :symbolize_names => true
rescue Exception => e
	@logger.fatal "Couln't request put: #{path} #{params}"
	pp e
end

#use_token(token) ⇒ Object



45
46
47
48
# File 'lib/openbooking_client_ruby.rb', line 45

def use_token(token)
	@logger.debug "Using Token #{token}"
	@token = token
end