grouper-rest-client 0.0.1 → 0.1.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/HISTORY +5 -0
- data/README.rdoc +10 -1
- data/Rakefile +8 -0
- data/grouper-rest-client.gemspec +2 -0
- data/lib/grouper-rest-client.rb +84 -36
- data/lib/grouper-rest-client/version.rb +3 -3
- data/spec/resource_spec.rb +111 -0
- data/spec/spec_helper.rb +2 -0
- metadata +23 -6
data/HISTORY
CHANGED
data/README.rdoc
CHANGED
@@ -2,11 +2,20 @@
|
|
2
2
|
|
3
3
|
== Usage
|
4
4
|
|
5
|
-
Grouper::Rest::Client.new url, :user => user, :password => pass do |grouper|
|
5
|
+
Grouper::Rest::Client::Resource.new url, :user => user, :password => pass do |grouper|
|
6
6
|
# Get all group memberships.
|
7
7
|
groups = grouper.groups('blair')
|
8
8
|
|
9
9
|
# Get group memberships within stem prefix.
|
10
10
|
filtered_groups = grouper.groups('blair', 'some:stem:')
|
11
|
+
|
12
|
+
# Get subjects matching query.
|
13
|
+
subjects = grouper.subjects('blair')
|
14
|
+
|
15
|
+
# Was the last WS call successful?
|
16
|
+
grouper.ok?
|
17
|
+
|
18
|
+
# Was there an error on the last WS call?
|
19
|
+
grouper.error?
|
11
20
|
end
|
12
21
|
|
data/Rakefile
CHANGED
data/grouper-rest-client.gemspec
CHANGED
data/lib/grouper-rest-client.rb
CHANGED
@@ -2,48 +2,96 @@ require 'rubygems'
|
|
2
2
|
require 'json'
|
3
3
|
require 'rest_client'
|
4
4
|
|
5
|
-
module Grouper
|
6
|
-
module Rest
|
5
|
+
module Grouper # :nodoc:
|
6
|
+
module Rest # :nodoc:
|
7
7
|
# = Minimal REST client for interacting with the Grouper WS API.
|
8
8
|
#
|
9
9
|
# == Usage
|
10
|
-
# Grouper::Rest::Client.new url, :user => user, :password => pass do |grouper|
|
10
|
+
# Grouper::Rest::Client::Resource.new url, :user => user, :password => pass do |grouper|
|
11
11
|
# # Get all group memberships.
|
12
12
|
# groups = grouper.groups('blair')
|
13
13
|
#
|
14
14
|
# # Get group memberships within stem prefix.
|
15
15
|
# filtered_groups = grouper.groups('blair', 'some:stem:')
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
16
|
+
#
|
17
|
+
# # Get subjects matching query.
|
18
|
+
# subjects = grouper.subjects('blair')
|
19
|
+
#
|
20
|
+
# # Was the last WS call successful?
|
21
|
+
# grouper.ok?
|
22
|
+
#
|
23
|
+
# # Was there an error on the last WS call?
|
24
|
+
# grouper.error?
|
25
|
+
# end
|
26
|
+
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
|
+
end # module Client
|
95
|
+
end # module Rest
|
96
|
+
end # module Grouper
|
49
97
|
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Grouper::Rest::Client::Resource initialization" do
|
4
|
+
|
5
|
+
context "with no arguments" do
|
6
|
+
it "should raise ArgumentError" do
|
7
|
+
lambda { Grouper::Rest::Client::Resource.new }.should raise_error(ArgumentError)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with one argument" do
|
12
|
+
|
13
|
+
context "that is invalid" do
|
14
|
+
it "should raise ArgumentError when nil" do
|
15
|
+
lambda { Grouper::Rest::Client::Resource.new(nil) }.should raise_error(ArgumentError)
|
16
|
+
end
|
17
|
+
it "should raise ArgumentError when blank" do
|
18
|
+
lambda { Grouper::Rest::Client::Resource.new('') }.should raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "that is valid" do
|
23
|
+
it "should return Grouper::Rest::Client::Resource object" do
|
24
|
+
Grouper::Rest::Client::Resource.new('foo').should be_an_instance_of(Grouper::Rest::Client::Resource)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end # describe initialization
|
31
|
+
|
32
|
+
|
33
|
+
describe "Grouper::Rest::Client::Resource#groups()" do
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
@grouper = Grouper::Rest::Client::Resource.new('foo')
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with no arguments" do
|
40
|
+
it "should raise ArgumentError" do
|
41
|
+
lambda { @grouper.groups }.should raise_error(ArgumentError)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with one argument" do
|
46
|
+
context "that is invalid" do
|
47
|
+
it "should raise ArgumentError when nil" do
|
48
|
+
lambda { @grouper.groups(nil) }.should raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
it "should raise ArgumentError when blank" do
|
51
|
+
lambda { @grouper.groups('') }.should raise_error(ArgumentError)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end # describe #groups()
|
57
|
+
|
58
|
+
|
59
|
+
describe "Grouper::Rest::Client::Resource#error?()" do
|
60
|
+
context "before any WS calls have been made" do
|
61
|
+
it "should be false" do
|
62
|
+
Grouper::Rest::Client::Resource.new('foo').error?.should be_false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end # describe #error?()
|
66
|
+
|
67
|
+
|
68
|
+
describe "Grouper::Rest::Client::Resource#ok?()" do
|
69
|
+
context "before any WS calls have been made" do
|
70
|
+
it "should be true" do
|
71
|
+
Grouper::Rest::Client::Resource.new('foo').ok?.should be_true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end # describe #ok?()
|
75
|
+
|
76
|
+
|
77
|
+
describe "Grouper::Rest::Client::Resource#response" do
|
78
|
+
context "before any WS calls have been made" do
|
79
|
+
it "should be nil" do
|
80
|
+
Grouper::Rest::Client::Resource.new('foo').response.should be_nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end # describe #response
|
84
|
+
|
85
|
+
|
86
|
+
describe "Grouper::Rest::Client::Resource#subjects()" do
|
87
|
+
|
88
|
+
before(:each) do
|
89
|
+
@grouper = Grouper::Rest::Client::Resource.new('foo')
|
90
|
+
end
|
91
|
+
|
92
|
+
context "with no arguments" do
|
93
|
+
it "should raise ArgumentError" do
|
94
|
+
lambda { @grouper.subjects }.should raise_error(ArgumentError)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "with one argument" do
|
99
|
+
context "that is invalid" do
|
100
|
+
it "should raise ArgumentError when nil" do
|
101
|
+
lambda { @grouper.subjects(nil) }.should raise_error(ArgumentError)
|
102
|
+
end
|
103
|
+
it "should raise ArgumentError when blank" do
|
104
|
+
lambda { @grouper.subjects('') }.should raise_error(ArgumentError)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end # describe #subjects()
|
110
|
+
|
111
|
+
|
data/spec/spec_helper.rb
ADDED
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: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.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-18 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,20 @@ dependencies:
|
|
50
50
|
version: 1.6.1
|
51
51
|
type: :runtime
|
52
52
|
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
53
67
|
description: REST client for accessing the Grouper WS API
|
54
68
|
email:
|
55
69
|
- blair.christensen@gmail.com
|
@@ -68,6 +82,8 @@ files:
|
|
68
82
|
- grouper-rest-client.gemspec
|
69
83
|
- lib/grouper-rest-client.rb
|
70
84
|
- lib/grouper-rest-client/version.rb
|
85
|
+
- spec/resource_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
71
87
|
has_rdoc: true
|
72
88
|
homepage: https://github.com/blairc/
|
73
89
|
licenses: []
|
@@ -102,5 +118,6 @@ rubygems_version: 1.6.2
|
|
102
118
|
signing_key:
|
103
119
|
specification_version: 3
|
104
120
|
summary: REST client for accessing the Grouper WS API
|
105
|
-
test_files:
|
106
|
-
|
121
|
+
test_files:
|
122
|
+
- spec/resource_spec.rb
|
123
|
+
- spec/spec_helper.rb
|