openbooking_client_ruby 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/openbooking_client_ruby.rb +119 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 00c9ff4eea2a67cdfb800826b8a34444e9aa1211
|
4
|
+
data.tar.gz: 854813662191bc44a444721f667858c7f0468934
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26674e24b9bc449e95cf1eca8cd464275758d012852340e89c133259f177d34b747f868e6d20fb82dc57dfefda9eaddd59cd26339b9cec20099773c45da1214d
|
7
|
+
data.tar.gz: 758052ae015e5142eac925d434c2b09cd9b04ad7a910b59bda4e67f2f9e1cbb5249ef0397fc45209f1debe2520212cb7220c04871f1a097ff9692819b55a2fda
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
require 'pp'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
|
8
|
+
class OpenbookingClientRuby
|
9
|
+
|
10
|
+
def initialize(uri, logger=Logger.new(STDOUT))
|
11
|
+
RestClient.proxy = ENV["PROXY"] unless ENV["PROXY"].nil?
|
12
|
+
|
13
|
+
@logger = logger
|
14
|
+
@opts = {}
|
15
|
+
ob_url = URI::parse(uri)
|
16
|
+
|
17
|
+
if ob_url.userinfo
|
18
|
+
auth = ob_url.userinfo.split(':')
|
19
|
+
@opts[:user] = auth[0]
|
20
|
+
@opts[:pass] = auth[1]
|
21
|
+
end
|
22
|
+
|
23
|
+
@opts[:url] = "#{ob_url.scheme}://#{ob_url.host}:#{ob_url.port}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def auth
|
27
|
+
@logger.debug 'Get OpenBooking Token'
|
28
|
+
response = post '/authenticate', {
|
29
|
+
:user => @opts[:user],
|
30
|
+
:pass => @opts[:pass]
|
31
|
+
}
|
32
|
+
|
33
|
+
@logger.debug "Got OpenBooking Token, ID: #{response[:access_token]}"
|
34
|
+
@token = response[:access_token]
|
35
|
+
end
|
36
|
+
|
37
|
+
def auth_as(username)
|
38
|
+
auth
|
39
|
+
all_users = get "/users/all?user=#{username}&fields=id,user,name,group"
|
40
|
+
user = all_users[0]
|
41
|
+
token = post("/tokens/"+user[:id], {scopes: ["master"]})
|
42
|
+
use_token(token[:access_token])
|
43
|
+
end
|
44
|
+
|
45
|
+
def use_token(token)
|
46
|
+
@logger.debug "Using Token #{token}"
|
47
|
+
@token = token
|
48
|
+
end
|
49
|
+
|
50
|
+
def get(path, params = {}, headers = {})
|
51
|
+
qs = URI.encode_www_form params
|
52
|
+
uri = "#{@opts[:url]}#{path}?#{qs}"
|
53
|
+
@logger.debug "GET #{uri}"
|
54
|
+
headers[:accept] = "json"
|
55
|
+
headers[:authorization] = "Bearer #{@token}"
|
56
|
+
JSON.parse RestClient.send('get', uri, headers), :symbolize_names => true
|
57
|
+
rescue Exception => e
|
58
|
+
puts "Couldn't request get: #{path} #{params}"
|
59
|
+
pp e
|
60
|
+
end
|
61
|
+
|
62
|
+
def post(path, params = {})
|
63
|
+
uri = "#{@opts[:url]}#{path}"
|
64
|
+
|
65
|
+
headers = { :accept => :json, :content_type => :json }
|
66
|
+
headers[:authorization] = "Bearer #{@token}"
|
67
|
+
|
68
|
+
data = params.to_json
|
69
|
+
if !data
|
70
|
+
data = {}.to_json
|
71
|
+
end
|
72
|
+
JSON.parse RestClient.send('post', uri, data, headers), :symbolize_names => true
|
73
|
+
rescue Exception => e
|
74
|
+
@logger.fatal "Couln't request post: #{path} #{params}"
|
75
|
+
pp e
|
76
|
+
end
|
77
|
+
|
78
|
+
def put(path, params = {})
|
79
|
+
uri = "#{@opts[:url]}#{path}"
|
80
|
+
headers = { :accept => :json, :content_type => :json }
|
81
|
+
data = params.to_json
|
82
|
+
headers[:authorization] = "Bearer #{@token}"
|
83
|
+
JSON.parse RestClient.send('put', uri, data, headers), :symbolize_names => true
|
84
|
+
rescue Exception => e
|
85
|
+
@logger.fatal "Couln't request put: #{path} #{params}"
|
86
|
+
pp e
|
87
|
+
end
|
88
|
+
|
89
|
+
def patch(path, params = {})
|
90
|
+
uri = "#{@opts[:url]}#{path}"
|
91
|
+
headers = { :accept => :json, :content_type => :json }
|
92
|
+
data = params.to_json
|
93
|
+
headers[:authorization] = "Bearer #{@token}"
|
94
|
+
JSON.parse RestClient.send('patch', uri, data, headers), :symbolize_names => true
|
95
|
+
rescue Exception => e
|
96
|
+
@logger.fatal "Couln't request patch: #{path} #{params}"
|
97
|
+
pp e
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def del(path, params = {})
|
102
|
+
qs = URI.encode_www_form params
|
103
|
+
uri = "#{@opts[:url]}#{path}?#{qs}"
|
104
|
+
headers = { :accept => :json }
|
105
|
+
headers[:authorization] = "Bearer #{@token}"
|
106
|
+
JSON.parse RestClient.send('delete', uri, headers), :symbolize_names => true
|
107
|
+
rescue Exception => e
|
108
|
+
@logger.fatal "Couldn't request delete: #{path} #{params}"
|
109
|
+
pp e
|
110
|
+
end
|
111
|
+
|
112
|
+
def close
|
113
|
+
del "/token/#{@token}"
|
114
|
+
remove_instance_variable :@token
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
end
|
119
|
+
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openbooking_client_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Louis Brauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
description: Easy to use
|
28
|
+
email: louis.brauer@pixeltex.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/openbooking_client_ruby.rb
|
34
|
+
homepage: http://www.openbooking.ch
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.5.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: OpenBooking Client for Ruby
|
58
|
+
test_files: []
|