grouper-rest-client 0.1.0 → 0.2.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.
- data/.gitignore +1 -0
- data/HISTORY +13 -0
- data/README.rdoc +37 -9
- data/grouper-rest-client.gemspec +1 -1
- data/lib/grouper-rest-client.rb +37 -78
- data/lib/grouper-rest-client/resource.rb +214 -0
- data/lib/grouper-rest-client/version.rb +1 -1
- data/spec/resource_spec.rb +0 -1
- metadata +9 -6
data/.gitignore
CHANGED
data/HISTORY
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
2011-03-24 v0.2.0
|
2
|
+
- Added "Resource#group(name)" method
|
3
|
+
- Added "Resource#groups( group, [filter_type] )" method
|
4
|
+
- Added "Resource#stem(name)" method
|
5
|
+
- Added "Resource#stems( stem, [filter_type] )" method
|
6
|
+
- Added "Resource#subject(subject)" method
|
7
|
+
- Added "Resource::Group" class
|
8
|
+
- Added "Resource::Stem" class
|
9
|
+
- Added "Resource::Subject" class
|
10
|
+
- Changed "Resource#call( resource, [method] )" to be public.
|
11
|
+
- Changed "Resource#subjects(subject)" to return list of "Resource::Subject" objects or JSON error response.
|
12
|
+
- Moved "Resource#groups( subject, [stem_filter] )" to "Resource::Subject#group( [stem_filter] )"
|
13
|
+
|
1
14
|
2011-03-18 v0.1.0
|
2
15
|
- API namespace change.
|
3
16
|
- Added "#subject(subject)" WS call.
|
data/README.rdoc
CHANGED
@@ -3,15 +3,43 @@
|
|
3
3
|
== Usage
|
4
4
|
|
5
5
|
Grouper::Rest::Client::Resource.new url, :user => user, :password => pass do |grouper|
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
|
14
|
-
|
6
|
+
# Find all stems matching identifier.
|
7
|
+
stems = grouper.stems('some:approximate:stem')
|
8
|
+
|
9
|
+
# Find stem by name.
|
10
|
+
stem = grouper.stem('some:stem')
|
11
|
+
|
12
|
+
# UUID of stem.
|
13
|
+
stem['uuid']
|
14
|
+
|
15
|
+
|
16
|
+
# Find all groups matching identifier.
|
17
|
+
groups = grouper.groups('some:approximate:group')
|
18
|
+
|
19
|
+
# Find group by name.
|
20
|
+
group = grouper.group('some:group')
|
21
|
+
|
22
|
+
# UUID of group.
|
23
|
+
group['uuid']
|
24
|
+
|
25
|
+
|
26
|
+
# Find all subjects matching identifier.
|
27
|
+
subjects = grouper.subjects('foo')
|
28
|
+
|
29
|
+
# Find subject.
|
30
|
+
subject = grouper.subject('blair')
|
31
|
+
|
32
|
+
# Subject's name and id.
|
33
|
+
subject['name']
|
34
|
+
subject['id']
|
35
|
+
|
36
|
+
# In what groups is this subject a member?
|
37
|
+
my_groups = subject.groups()
|
38
|
+
|
39
|
+
# In what groups within the specified stem is this subject a member?
|
40
|
+
my_filtered_groups = subject.groups('some:stem')
|
41
|
+
|
42
|
+
|
15
43
|
# Was the last WS call successful?
|
16
44
|
grouper.ok?
|
17
45
|
|
data/grouper-rest-client.gemspec
CHANGED
data/lib/grouper-rest-client.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
require 'json'
|
3
|
-
require 'rest_client'
|
1
|
+
require File.join( File.expand_path( File.dirname(__FILE__) ), 'grouper-rest-client', 'resource' )
|
4
2
|
|
5
3
|
module Grouper # :nodoc:
|
6
4
|
module Rest # :nodoc:
|
@@ -8,89 +6,50 @@ module Grouper # :nodoc:
|
|
8
6
|
#
|
9
7
|
# == Usage
|
10
8
|
# Grouper::Rest::Client::Resource.new url, :user => user, :password => pass do |grouper|
|
11
|
-
# #
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# #
|
15
|
-
#
|
9
|
+
# # Find all stems matching identifier.
|
10
|
+
# stems = grouper.stems('some:approximate:stem')
|
11
|
+
#
|
12
|
+
# # Find stem by name.
|
13
|
+
# stem = grouper.stem('some:stem')
|
14
|
+
#
|
15
|
+
# # UUID of stem.
|
16
|
+
# stem['uuid']
|
17
|
+
#
|
18
|
+
#
|
19
|
+
# # Find all groups matching identifier.
|
20
|
+
# groups = grouper.groups('some:approximate:group')
|
21
|
+
#
|
22
|
+
# # Find group by name.
|
23
|
+
# group = grouper.group('some:group')
|
24
|
+
#
|
25
|
+
# # UUID of group.
|
26
|
+
# group['uuid']
|
27
|
+
#
|
28
|
+
#
|
29
|
+
# # Find all subjects matching identifier.
|
30
|
+
# subjects = grouper.subjects('foo')
|
31
|
+
#
|
32
|
+
# # Find subject.
|
33
|
+
# subject = grouper.subject('blair')
|
34
|
+
#
|
35
|
+
# # Subject's name and id.
|
36
|
+
# subject['name']
|
37
|
+
# subject['id']
|
38
|
+
#
|
39
|
+
# # In what groups is this subject a member?
|
40
|
+
# my_groups = subject.groups()
|
41
|
+
#
|
42
|
+
# # In what groups within the specified stem is this subject a member?
|
43
|
+
# my_filtered_groups = subject.groups('some:stem')
|
16
44
|
#
|
17
|
-
# # Get subjects matching query.
|
18
|
-
# subjects = grouper.subjects('blair')
|
19
45
|
#
|
20
46
|
# # Was the last WS call successful?
|
21
47
|
# grouper.ok?
|
22
|
-
#
|
48
|
+
#
|
23
49
|
# # Was there an error on the last WS call?
|
24
50
|
# grouper.error?
|
25
51
|
# end
|
26
52
|
module Client
|
27
|
-
class Resource
|
28
|
-
|
29
|
-
# Response object from last request.
|
30
|
-
attr_reader :response
|
31
|
-
|
32
|
-
# Create Grouper REST client.
|
33
|
-
# Params:
|
34
|
-
# +url+:: Base URL for Grouper WS API.
|
35
|
-
# +:user => user+:: Authenticate to the API as this user.
|
36
|
-
# +:password => password+:: Authenticate to the API with this password.
|
37
|
-
def initialize( url, options={} )
|
38
|
-
raise ArgumentError if url.nil? || url.empty?
|
39
|
-
@error = false
|
40
|
-
@client = RestClient::Resource.new( url, options.merge!( :headers => { :content_type => 'text/x-json' } ) )
|
41
|
-
@response = nil
|
42
|
-
yield self if block_given?
|
43
|
-
self
|
44
|
-
end
|
45
|
-
|
46
|
-
# Was there an error on the last WS call?
|
47
|
-
def error?; @error; end
|
48
|
-
|
49
|
-
# Get memberships for subject(s) matching identifier.
|
50
|
-
# Returns array of hashes of group memberships OR JSON error response.
|
51
|
-
# Params:
|
52
|
-
# +subject+:: Get groups for this subject.
|
53
|
-
# +stem_filter+:: Limit groups to those within this stem prefix. (optional)
|
54
|
-
def groups(subject, stem_filter=nil)
|
55
|
-
raise ArgumentError if subject.nil? || subject.empty?
|
56
|
-
k1, k2 = 'WsGetGroupsLiteResult', 'wsGroups'
|
57
|
-
result = call("subjects/#{subject}/groups")
|
58
|
-
if result.key?(k1) && result[k1].key?(k2)
|
59
|
-
groups = result['WsGetGroupsLiteResult']['wsGroups']
|
60
|
-
groups = groups.select { |group| group['name'] =~ /^#{stem_filter}/ } if stem_filter
|
61
|
-
return groups
|
62
|
-
end
|
63
|
-
return result
|
64
|
-
end
|
65
|
-
|
66
|
-
# Was the last WS call successful?
|
67
|
-
def ok?; !error?; end
|
68
|
-
|
69
|
-
# Get subject(s) matching identifier.
|
70
|
-
# Returns array of hashes of subjects OR JSON error response.
|
71
|
-
# Params:
|
72
|
-
# +subjects+:: Get subject(s) matching this identifier.
|
73
|
-
def subjects(subject)
|
74
|
-
raise ArgumentError if subject.nil? || subject.empty?
|
75
|
-
k1, k2 = 'WsGetSubjectsResults', 'wsSubjects'
|
76
|
-
result = call("subjects/#{subject}")
|
77
|
-
return result[k1][k2] if result.key?(k1) && result[k1].key?(k2)
|
78
|
-
return result
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
# Make WS call and return JSON response.
|
84
|
-
def call(resource, method = :get)
|
85
|
-
@response = @client[resource].send(method)
|
86
|
-
@error = false
|
87
|
-
JSON.parse( @response.body )
|
88
|
-
rescue RestClient::Exception => e
|
89
|
-
@error = true
|
90
|
-
return JSON.parse( e.response )
|
91
|
-
end
|
92
|
-
|
93
|
-
end # class Resource
|
94
53
|
end # module Client
|
95
54
|
end # module Rest
|
96
55
|
end # module Grouper
|
@@ -0,0 +1,214 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'json'
|
3
|
+
require 'rest_client'
|
4
|
+
|
5
|
+
module Grouper # :nodoc:
|
6
|
+
module Rest # :nodoc:
|
7
|
+
module Client # :nodoc:
|
8
|
+
class Resource
|
9
|
+
|
10
|
+
# Response object from last request.
|
11
|
+
attr_reader :response
|
12
|
+
|
13
|
+
# Create Grouper REST client.
|
14
|
+
# Params:
|
15
|
+
# +url+:: Base URL for Grouper WS API.
|
16
|
+
# +:user => user+:: Authenticate to the API as this user.
|
17
|
+
# +:password => password+:: Authenticate to the API with this password.
|
18
|
+
def initialize( url, options={} )
|
19
|
+
raise ArgumentError if url.nil? || url.empty?
|
20
|
+
@error = false
|
21
|
+
@client = RestClient::Resource.new( url, options.merge!( :headers => { :content_type => 'text/x-json' } ) )
|
22
|
+
@response = nil
|
23
|
+
yield self if block_given?
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# Make WS call and return JSON response.
|
28
|
+
# Returns JSON response.
|
29
|
+
# +resource+:: Make call against this resource.
|
30
|
+
# +method+:: Optional HTTP method type. Defaults to +:get+.
|
31
|
+
# +request+:: Optional.
|
32
|
+
def call(resource, method = :get, request = nil )
|
33
|
+
if request
|
34
|
+
@response = @client[resource].send(method, JSON.generate(request) )
|
35
|
+
else
|
36
|
+
@response = @client[resource].send(method)
|
37
|
+
end
|
38
|
+
@error = false
|
39
|
+
JSON.parse( @response.body )
|
40
|
+
rescue RestClient::Exception => e
|
41
|
+
@error = true
|
42
|
+
return JSON.parse( e.response )
|
43
|
+
end
|
44
|
+
|
45
|
+
# Was there an error on the last WS call?
|
46
|
+
def error?; @error; end
|
47
|
+
|
48
|
+
# Get +Grouper::Rest::Client::Resource::Group+ matching name or +nil+.
|
49
|
+
def group(name)
|
50
|
+
groups = groups( name, 'FIND_BY_GROUP_NAME' )
|
51
|
+
return groups.first if ok? && 1 == groups.size
|
52
|
+
return nil
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get group(s) matching identifier.
|
56
|
+
# Returns list of Grouper::Rest::Client::Resource::Group objects OR JSON error response.
|
57
|
+
# Params:
|
58
|
+
# +group+:: Get group(s) matching this identifier.
|
59
|
+
# +filter_type+:: Query groups with this filter type. Defaults to 'FIND_BY_GROUP_NAME_APPROXIMATE'. (optional)
|
60
|
+
def groups( group, filter_type = 'FIND_BY_GROUP_NAME_APPROXIMATE' )
|
61
|
+
raise ArgumentError if stem.nil? || group.empty?
|
62
|
+
k1, k2 = 'WsFindGroupsResults', 'groupResults'
|
63
|
+
request = {
|
64
|
+
'WsRestFindGroupsLiteRequest' => {
|
65
|
+
'groupName' => group,
|
66
|
+
'queryFilterType' => filter_type
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
result = call('groups', :post, request)
|
71
|
+
if result.key?(k1) && result[k1].key?(k2)
|
72
|
+
return result[k1][k2].collect { |r| Grouper::Rest::Client::Resource::Group.new(self, r) }
|
73
|
+
end
|
74
|
+
return result
|
75
|
+
end
|
76
|
+
|
77
|
+
# Was the last WS call successful?
|
78
|
+
def ok?; !error?; end
|
79
|
+
|
80
|
+
# Get +Grouper::Rest::Client::Resource::Stem+ matching +name+ or +nil+.
|
81
|
+
def stem(name)
|
82
|
+
stems = stems( name, 'FIND_BY_STEM_NAME' )
|
83
|
+
return stems.first if ok? && 1 == stems.size
|
84
|
+
return nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# Get stem(s) matching identifier.
|
88
|
+
# Returns list of Grouper::Rest::Client::Resource::Stem objects OR JSON error response.
|
89
|
+
# Params:
|
90
|
+
# +stem+:: Get stem(s) matching this identifier.
|
91
|
+
# +filter_type+:: Query stems with this filter type. Defaults to 'FIND_BY_STEM_NAME_APPROXIMATE'. (optional)
|
92
|
+
def stems( stem, filter_type = 'FIND_BY_STEM_NAME_APPROXIMATE' )
|
93
|
+
raise ArgumentError if stem.nil? || stem.empty?
|
94
|
+
k1, k2 = 'WsFindStemsResults', 'stemResults'
|
95
|
+
request = {
|
96
|
+
'WsRestFindStemsLiteRequest' => {
|
97
|
+
'stemName' => stem,
|
98
|
+
'stemQueryFilterType' => filter_type
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
result = call("stems", :post, request)
|
103
|
+
if result.key?(k1) && result[k1].key?(k2)
|
104
|
+
return result[k1][k2].collect { |r| Grouper::Rest::Client::Resource::Stem.new(self, r) }
|
105
|
+
end
|
106
|
+
return result
|
107
|
+
end
|
108
|
+
|
109
|
+
# Get +Grouper::Rest::Client::Resource::Subject+ matching identifier or +nil+.
|
110
|
+
def subject(subject)
|
111
|
+
subjects = subjects(subject)
|
112
|
+
return subjects.first if ok? && 1 == subjects.size
|
113
|
+
return nil
|
114
|
+
end
|
115
|
+
|
116
|
+
# Get subject(s) matching identifier.
|
117
|
+
# Returns list of Grouper::Rest::Client::Resource::Subject objects OR JSON error response.
|
118
|
+
# +subjects+:: Get subject(s) matching this identifier.
|
119
|
+
def subjects(subject)
|
120
|
+
raise ArgumentError if subject.nil? || subject.empty?
|
121
|
+
k1, k2 = 'WsGetSubjectsResults', 'wsSubjects'
|
122
|
+
result = call("subjects/#{subject}")
|
123
|
+
if result.key?(k1) && result[k1].key?(k2)
|
124
|
+
return result[k1][k2].collect { |r| Grouper::Rest::Client::Resource::Subject.new(self, r) }
|
125
|
+
end
|
126
|
+
return result
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# TODO DRY w/ "Subject" + "Stem"
|
131
|
+
class Group
|
132
|
+
|
133
|
+
def initialize(resource, json)
|
134
|
+
@resource = resource
|
135
|
+
@json = json
|
136
|
+
end
|
137
|
+
|
138
|
+
# Return attribute value for +key+ or nil.
|
139
|
+
def [](key)
|
140
|
+
return @json[key] if @json && @json.key?(key)
|
141
|
+
return nil
|
142
|
+
end
|
143
|
+
|
144
|
+
def to_s
|
145
|
+
return "#{ self.class.name}: #{ @json.keys.sort.collect { |k| "#{ k }=#{ @json[k] }" }.join(', ') }" if @json
|
146
|
+
super
|
147
|
+
end
|
148
|
+
|
149
|
+
end # class Group
|
150
|
+
|
151
|
+
|
152
|
+
# TODO DRY w/ "Group" + "Subject"
|
153
|
+
class Stem
|
154
|
+
|
155
|
+
def initialize(resource, json)
|
156
|
+
@resource = resource
|
157
|
+
@json = json
|
158
|
+
end
|
159
|
+
|
160
|
+
# Return attribute value for +key+ or nil.
|
161
|
+
def [](key)
|
162
|
+
return @json[key] if @json && @json.key?(key)
|
163
|
+
return nil
|
164
|
+
end
|
165
|
+
|
166
|
+
def to_s
|
167
|
+
return "#{ self.class.name}: #{ @json.keys.sort.collect { |k| "#{ k }=#{ @json[k] }" }.join(', ') }" if @json
|
168
|
+
super
|
169
|
+
end
|
170
|
+
|
171
|
+
end # class Stem
|
172
|
+
|
173
|
+
|
174
|
+
# TODO DRY w/ "Group" + "Stem"
|
175
|
+
class Subject
|
176
|
+
|
177
|
+
def initialize(resource, json)
|
178
|
+
@resource = resource
|
179
|
+
@json = json
|
180
|
+
end
|
181
|
+
|
182
|
+
# Return attribute value for +key+ or nil.
|
183
|
+
def [](key)
|
184
|
+
return @json[key] if @json && @json.key?(key)
|
185
|
+
return nil
|
186
|
+
end
|
187
|
+
|
188
|
+
# Get memberships for this subject.
|
189
|
+
# Returns list of hashes of group memberships OR JSON error response.
|
190
|
+
# Params:
|
191
|
+
# +stem_filter+:: Limit groups to those within this stem prefix. (optional)
|
192
|
+
def groups(stem_filter=nil)
|
193
|
+
k1, k2 = 'WsGetGroupsLiteResult', 'wsGroups'
|
194
|
+
result = @resource.call("subjects/#{ @json['id'] }/groups")
|
195
|
+
if result.key?(k1) && result[k1].key?(k2)
|
196
|
+
groups = result['WsGetGroupsLiteResult']['wsGroups']
|
197
|
+
groups = groups.select { |group| group['name'] =~ /^#{stem_filter}/ } if stem_filter
|
198
|
+
return groups.collect { |g| Grouper::Rest::Client::Resource::Group.new(@resource, g) }
|
199
|
+
end
|
200
|
+
return result
|
201
|
+
end
|
202
|
+
|
203
|
+
def to_s
|
204
|
+
return "#{ self.class.name}: #{ @json.keys.sort.collect { |k| "#{ k }=#{ @json[k] }" }.join(', ') }" if @json
|
205
|
+
super
|
206
|
+
end
|
207
|
+
|
208
|
+
end # class Subject
|
209
|
+
|
210
|
+
end # class Resource
|
211
|
+
end # module Client
|
212
|
+
end # module Rest
|
213
|
+
end # module Grouper
|
214
|
+
|
data/spec/resource_spec.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grouper-rest-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Blair Christensen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-24 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -58,10 +58,12 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
hash:
|
61
|
+
hash: 27
|
62
62
|
segments:
|
63
|
+
- 2
|
64
|
+
- 5
|
63
65
|
- 0
|
64
|
-
version:
|
66
|
+
version: 2.5.0
|
65
67
|
type: :development
|
66
68
|
version_requirements: *id003
|
67
69
|
description: REST client for accessing the Grouper WS API
|
@@ -81,6 +83,7 @@ files:
|
|
81
83
|
- Rakefile
|
82
84
|
- grouper-rest-client.gemspec
|
83
85
|
- lib/grouper-rest-client.rb
|
86
|
+
- lib/grouper-rest-client/resource.rb
|
84
87
|
- lib/grouper-rest-client/version.rb
|
85
88
|
- spec/resource_spec.rb
|
86
89
|
- spec/spec_helper.rb
|