Class: Grouper::Rest::Client::Resource
- Inherits:
-
Object
- Object
- Grouper::Rest::Client::Resource
- Defined in:
- lib/grouper-rest-client/resource.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#response ⇒ Object
readonly
Response object from last request.
Instance Method Summary collapse
-
#_debug(result) ⇒ Object
Emit raw result if debuging is enabled.
-
#call(resource, method = :get, request = nil) ⇒ Object
Make WS call and return JSON response.
-
#error? ⇒ Boolean
Was there an error on the last WS call?.
-
#group(name) ⇒ Object
Get
Grouper::Rest::Client::Resource::Group
matching name ornil
. -
#groups(group, filter_type = 'FIND_BY_GROUP_NAME_APPROXIMATE') ⇒ Object
Get group(s) matching identifier.
-
#initialize(url, options = {}) {|_self| ... } ⇒ Resource
constructor
Create Grouper REST client.
-
#ok? ⇒ Boolean
Get group(s) matching identifier.
-
#stem(name) ⇒ Object
Get
Grouper::Rest::Client::Resource::Stem
matchingname
ornil
. -
#stems(stem, filter_type = 'FIND_BY_STEM_NAME_APPROXIMATE') ⇒ Object
Get stem(s) matching identifier.
-
#subject(subject) ⇒ Object
Get
Grouper::Rest::Client::Resource::Subject
matching identifier ornil
. -
#subjects(subject) ⇒ Object
Get subject(s) matching identifier.
Constructor Details
#initialize(url, options = {}) {|_self| ... } ⇒ Resource
Create Grouper REST client. Params:
url
-
Base URL for Grouper WS API.
- :user => user
-
Authenticate to the API as this user.
- :password => password
-
Authenticate to the API with this password.
20 21 22 23 24 25 26 27 28 |
# File 'lib/grouper-rest-client/resource.rb', line 20 def initialize( url, ={} ) raise ArgumentError if url.nil? || url.empty? @debug = false @error = false @client = RestClient::Resource.new( url, .merge!( :headers => { :content_type => 'text/x-json' } ) ) @response = nil yield self if block_given? self end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
10 11 12 |
# File 'lib/grouper-rest-client/resource.rb', line 10 def debug @debug end |
#response ⇒ Object (readonly)
Response object from last request.
13 14 15 |
# File 'lib/grouper-rest-client/resource.rb', line 13 def response @response end |
Instance Method Details
#_debug(result) ⇒ Object
Emit raw result if debuging is enabled.
50 51 52 |
# File 'lib/grouper-rest-client/resource.rb', line 50 def _debug(result) warn result if @debug && !result.nil? end |
#call(resource, method = :get, request = nil) ⇒ Object
Make WS call and return JSON response. Returns JSON response.
resource
-
Make call against this resource.
method
-
Optional HTTP method type. Defaults to
:get
. request
-
Optional.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/grouper-rest-client/resource.rb', line 35 def call(resource, method = :get, request = nil ) if request @response = @client[resource].send(method, JSON.generate(request) ) else @response = @client[resource].send(method) end _debug(@response) @error = false JSON.parse( @response.body ) rescue RestClient::Exception => e @error = true return JSON.parse( e.response ) end |
#error? ⇒ Boolean
Was there an error on the last WS call?
55 |
# File 'lib/grouper-rest-client/resource.rb', line 55 def error?; @error; end |
#group(name) ⇒ Object
Get Grouper::Rest::Client::Resource::Group
matching name or nil
.
58 59 60 61 62 |
# File 'lib/grouper-rest-client/resource.rb', line 58 def group(name) groups = groups( name, 'FIND_BY_GROUP_NAME_EXACT' ) return groups.first if ok? && 1 == groups.size return nil end |
#groups(group, filter_type = 'FIND_BY_GROUP_NAME_APPROXIMATE') ⇒ Object
Get group(s) matching identifier. Returns list of Grouper::Rest::Client::Resource::Group objects OR JSON error response. Params:
group
-
Get group(s) matching this identifier.
filter_type
-
Query groups with this filter type. Defaults to ‘FIND_BY_GROUP_NAME_APPROXIMATE’. (optional)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/grouper-rest-client/resource.rb', line 69 def groups( group, filter_type = 'FIND_BY_GROUP_NAME_APPROXIMATE' ) raise ArgumentError if group.nil? || group.empty? k1, k2 = 'WsFindGroupsResults', 'groupResults' request = { 'WsRestFindGroupsLiteRequest' => { 'groupName' => group.rstrip, # TODO Seems like a hack 'queryFilterType' => filter_type } } result = call('groups', :post, request) if result.key?(k1) && result[k1].key?(k2) return result[k1][k2].collect { |r| Grouper::Rest::Client::Resource::Group.new(self, r) } end [] end |
#ok? ⇒ Boolean
Get group(s) matching identifier. Was the last WS call successful?
87 |
# File 'lib/grouper-rest-client/resource.rb', line 87 def ok?; !error?; end |
#stem(name) ⇒ Object
Get Grouper::Rest::Client::Resource::Stem
matching name
or nil
.
90 91 92 93 94 |
# File 'lib/grouper-rest-client/resource.rb', line 90 def stem(name) stems = stems( name, 'FIND_BY_STEM_NAME' ) return stems.first if ok? && 1 == stems.size return nil end |
#stems(stem, filter_type = 'FIND_BY_STEM_NAME_APPROXIMATE') ⇒ Object
Get stem(s) matching identifier. Returns list of Grouper::Rest::Client::Resource::Stem objects OR JSON error response. Params:
stem
-
Get stem(s) matching this identifier.
filter_type
-
Query stems with this filter type. Defaults to ‘FIND_BY_STEM_NAME_APPROXIMATE’. (optional)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/grouper-rest-client/resource.rb', line 101 def stems( stem, filter_type = 'FIND_BY_STEM_NAME_APPROXIMATE' ) raise ArgumentError if stem.nil? || stem.empty? k1, k2 = 'WsFindStemsResults', 'stemResults' request = { 'WsRestFindStemsLiteRequest' => { 'stemName' => stem, 'stemQueryFilterType' => filter_type } } result = call("stems", :post, request) if result.key?(k1) && result[k1].key?(k2) return result[k1][k2].collect { |r| Grouper::Rest::Client::Resource::Stem.new(self, r) } end [] end |
#subject(subject) ⇒ Object
Get Grouper::Rest::Client::Resource::Subject
matching identifier or nil
.
119 120 121 122 123 |
# File 'lib/grouper-rest-client/resource.rb', line 119 def subject(subject) subjects = subjects(subject) return subjects.first if ok? && 1 == subjects.size return nil end |
#subjects(subject) ⇒ Object
Get subject(s) matching identifier. Returns list of Grouper::Rest::Client::Resource::Subject objects OR JSON error response.
subjects
-
Get subject(s) matching this identifier.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/grouper-rest-client/resource.rb', line 128 def subjects(subject) raise ArgumentError if subject.nil? || subject.empty? k1, k2 = 'WsGetSubjectsResults', 'wsSubjects' result = call("subjects/#{subject}") if result.key?(k1) && result[k1].key?(k2) return result[k1][k2].select { |tmp| tmp['success'] == 'T' }.collect do |r| Grouper::Rest::Client::Resource::Subject.new(self, r) end end result end |