authlete 0.3.6 → 0.3.7

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/lib/authlete.rb +14 -14
  3. data/lib/authlete/api.rb +115 -35
  4. data/lib/authlete/authentication-server.rb +4 -4
  5. data/lib/authlete/model/client-list.rb +127 -181
  6. data/lib/authlete/model/client.rb +444 -492
  7. data/lib/authlete/model/hashable.rb +65 -0
  8. data/lib/authlete/model/request/authentication-callback-request.rb +91 -0
  9. data/lib/authlete/model/request/authorization-fail-request.rb +107 -0
  10. data/lib/authlete/model/request/authorization-issue-request.rb +136 -0
  11. data/lib/authlete/model/request/authorization-request.rb +104 -0
  12. data/lib/authlete/model/request/developer-authentication-callback-request.rb +85 -0
  13. data/lib/authlete/model/request/introspection-request.rb +40 -0
  14. data/lib/authlete/model/request/token-fail-request.rb +104 -0
  15. data/lib/authlete/model/request/token-issue-request.rb +104 -0
  16. data/lib/authlete/model/request/token-request.rb +124 -0
  17. data/lib/authlete/model/response/authentication-callback-response.rb +54 -0
  18. data/lib/authlete/model/response/authorization-fail-response.rb +47 -0
  19. data/lib/authlete/model/response/authorization-issue-response.rb +47 -0
  20. data/lib/authlete/model/response/authorization-response.rb +146 -0
  21. data/lib/authlete/model/response/developer-authentication-callback-response.rb +56 -0
  22. data/lib/authlete/model/response/introspection-response.rb +129 -0
  23. data/lib/authlete/model/response/result.rb +45 -0
  24. data/lib/authlete/model/response/service-creatable-response.rb +51 -0
  25. data/lib/authlete/model/response/token-fail-response.rb +48 -0
  26. data/lib/authlete/model/response/token-issue-response.rb +48 -0
  27. data/lib/authlete/model/response/token-response.rb +69 -0
  28. data/lib/authlete/model/scope.rb +17 -42
  29. data/lib/authlete/model/service-list.rb +19 -74
  30. data/lib/authlete/model/service-owner.rb +16 -40
  31. data/lib/authlete/model/service.rb +20 -76
  32. data/lib/authlete/model/sns-credentials.rb +16 -41
  33. data/lib/authlete/model/tagged-value.rb +105 -135
  34. data/lib/authlete/utility.rb +29 -5
  35. data/lib/authlete/version.rb +1 -1
  36. metadata +24 -10
  37. data/lib/authlete/request/authentication-callback-request.rb +0 -90
  38. data/lib/authlete/request/developer-authentication-callback-request.rb +0 -84
  39. data/lib/authlete/response/authentication-callback-response.rb +0 -58
  40. data/lib/authlete/response/base-response.rb +0 -41
  41. data/lib/authlete/response/developer-authentication-callback-response.rb +0 -60
  42. data/lib/authlete/response/introspection-response.rb +0 -130
  43. data/lib/authlete/response/service-creatable-response.rb +0 -52
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84a8446a852c5b0aece8b3f18a299407ed29e6fa
4
- data.tar.gz: c5ead8e2f82f26ab7b9549a75cbd3cd1eb595dc9
3
+ metadata.gz: db827aa9d47c8832b6f2472046bdf4d7a2eb560a
4
+ data.tar.gz: 8ca80b2dcb5e7746c69775c860adc9041800cffe
5
5
  SHA512:
6
- metadata.gz: bf07053faa5d79fb84a58f71f70ed17a41a9700d7bffb8b9b6440ab8c9799f1a1589dd548198838f047bced62b5425d942f94b74cb79317883503aa908cc9290
7
- data.tar.gz: 55e6a410992f0e9c8019e70e175da08176bbf2d4adbd4a441e9f8011eb7601c560cd958896cb9f805e0a8ebc43785fdd88d221e38f62ac96146f93ba009bc2c0
6
+ metadata.gz: d55ae0fe32f6e14a1f79cb832b8554000015e897af58156aae7c8014e5c14d0c864a45941d3c902ea26f608f44f5b4ae05965011c52dfa36cb4d27e9894a0fda
7
+ data.tar.gz: 1f38e0904b2e68f7d2ef4ecd68311c472c5a3e69853dc00cbad084a3bc746f6524e93f98ab0489ce8c2721303d380c4d9830fffe9ce13e4eeaef41cd02d6c39d
data/lib/authlete.rb CHANGED
@@ -20,8 +20,7 @@ require 'authlete/version'
20
20
 
21
21
  # == Authlete Module
22
22
  #
23
- # A library for {Authlete Web APIs}[https://www.authlete.com/authlete_web_apis.html].
24
- #
23
+ # A library for Authlete Web APIs.
25
24
  module Authlete
26
25
  autoload :AuthenticationServer, 'authlete/authentication-server'
27
26
  autoload :Api, 'authlete/api'
@@ -30,6 +29,7 @@ module Authlete
30
29
  autoload :Utility, 'authlete/utility'
31
30
 
32
31
  module Model
32
+ autoload :Hashable, 'authlete/model/hashable'
33
33
  autoload :Client, 'authlete/model/client'
34
34
  autoload :ClientList, 'authlete/model/client-list'
35
35
  autoload :Scope, 'authlete/model/scope'
@@ -38,18 +38,18 @@ module Authlete
38
38
  autoload :ServiceOwner, 'authlete/model/service-owner'
39
39
  autoload :SnsCredentials, 'authlete/model/sns-credentials'
40
40
  autoload :TaggedValue, 'authlete/model/tagged-value'
41
- end
42
41
 
43
- module Request
44
- autoload :AuthenticationCallbackRequest, 'authlete/request/authentication-callback-request'
45
- autoload :DeveloperAuthenticationCallbackRequest, 'authlete/request/developer-authentication-callback-request'
46
- end
42
+ module Request
43
+ autoload :AuthenticationCallbackRequest, 'authlete/model/request/authentication-callback-request'
44
+ autoload :DeveloperAuthenticationCallbackRequest, 'authlete/model/request/developer-authentication-callback-request'
45
+ end
47
46
 
48
- module Response
49
- autoload :AuthenticationCallbackResponse, 'authlete/response/authentication-callback-response'
50
- autoload :BaseResponse, 'authlete/response/base-response'
51
- autoload :DeveloperAuthenticationCallbackResponse, 'authlete/response/developer-authentication-callback-response'
52
- autoload :IntrospectionResponse, 'authlete/response/introspection-response'
53
- autoload :ServiceCreatableResponse, 'authlete/response/service-creatable-response'
47
+ module Response
48
+ autoload :AuthenticationCallbackResponse, 'authlete/model/response/authentication-callback-response'
49
+ autoload :Result, 'authlete/model/response/result'
50
+ autoload :DeveloperAuthenticationCallbackResponse, 'authlete/model/response/developer-authentication-callback-response'
51
+ autoload :IntrospectionResponse, 'authlete/model/response/introspection-response'
52
+ autoload :ServiceCreatableResponse, 'authlete/model/response/service-creatable-response'
53
+ end
54
54
  end
55
- end
55
+ end
data/lib/authlete/api.rb CHANGED
@@ -69,13 +69,9 @@ module Authlete
69
69
  def call_api(method, path, content_type, payload, user, password)
70
70
  headers = {}
71
71
 
72
- if content_type.nil? == false
73
- headers.merge!(:content_type => content_type)
74
- end
72
+ headers.merge!(:content_type => content_type) unless content_type.nil?
75
73
 
76
- if @extra_headers.nil? == false
77
- headers.merge!(@extra_headers)
78
- end
74
+ headers.merge!(@extra_headers) unless @extra_headers.nil?
79
75
 
80
76
  response = execute(
81
77
  :method => method,
@@ -88,11 +84,7 @@ module Authlete
88
84
 
89
85
  body = body_as_string(response)
90
86
 
91
- if body.nil?
92
- return nil
93
- end
94
-
95
- JSON.parse(response.body.to_s, :symbolize_names => true)
87
+ body.nil? ? nil : JSON.parse(response.body.to_s, :symbolize_names => true)
96
88
  end
97
89
 
98
90
  def execute(parameters)
@@ -151,17 +143,11 @@ module Authlete
151
143
  end
152
144
 
153
145
  def body_as_string(response)
154
- if response.body.nil?
155
- return nil
156
- end
146
+ return nil if response.body.nil?
157
147
 
158
148
  body = response.body.to_s
159
149
 
160
- if body.length == 0
161
- return nil
162
- end
163
-
164
- return body
150
+ body.length == 0 ? nil : body
165
151
  end
166
152
 
167
153
  def call_api_service_owner(method, path, content_type, payload)
@@ -213,19 +199,19 @@ module Authlete
213
199
  array.push("#{key}=#{value}")
214
200
  end
215
201
 
216
- return "?" + array.join("&")
202
+ "?" + array.join("&")
217
203
  end
218
204
 
219
205
  public
220
206
 
221
207
  # Call Authlete's /api/service/creatable API.
222
208
  #
223
- # On success, an instance of Authlete::Response::ServiceCreatableResponse is returned.
209
+ # On success, an instance of Authlete::Model::Response::ServiceCreatableResponse is returned.
224
210
  # On error, Authlete::Exception is raised.
225
211
  def service_creatable(api_key)
226
212
  hash = call_api_service_owner(:get, "/api/service/creatable", nil, nil)
227
213
 
228
- Authlete::Response::ServiceCreatableResponse.new(hash)
214
+ Authlete::Model::Response::ServiceCreatableResponse.new(hash)
229
215
  end
230
216
 
231
217
  # Call Authlete's /api/service/create API.
@@ -390,9 +376,103 @@ module Authlete
390
376
  Authlete::Model::Client.new(hash)
391
377
  end
392
378
 
393
- # Call Authlete's {/auth/introspection}
394
- # [https://www.authlete.com/authlete_web_apis_introspection.html#auth_introspection]
395
- # API.
379
+ # Call Authlete's /api/auth/authorization API.
380
+ #
381
+ # On success, an instance of Authlete::Model::Response::AuthorizationResponse is returned.
382
+ # On error, Authlete::Exception is raised.
383
+ def authorization(request)
384
+ if request.kind_of?(Hash) == false
385
+ if request.respond_to?('to_hash')
386
+ request = request.to_hash
387
+ end
388
+ end
389
+
390
+ hash = call_api_json_service("/api/auth/authorization", request)
391
+
392
+ Authlete::Model::Response::AuthorizationResponse.new(hash)
393
+ end
394
+
395
+ # Call Authlete's /api/auth/authorization/issue API.
396
+ #
397
+ # On success, an instance of Authlete::Model::Response::AuthorizationIssueResponse is returned.
398
+ # On error, Authlete::Exception is raised.
399
+ def authorization_Issue(request)
400
+ if request.kind_of?(Hash) == false
401
+ if request.respond_to?('to_hash')
402
+ request = request.to_hash
403
+ end
404
+ end
405
+
406
+ hash = call_api_json_service("/api/auth/authorization/issue", request)
407
+
408
+ Authlete::Model::Response::AuthorizationIssueResponse.new(hash)
409
+ end
410
+
411
+ # Call Authlete's /api/auth/authorization/fail API.
412
+ #
413
+ # On success, an instance of Authlete::Model::Response::AuthorizationFailResponse is returned.
414
+ # On error, Authlete::Exception is raised.
415
+ def authorization_fail(request)
416
+ if request.kind_of?(Hash) == false
417
+ if request.respond_to?('to_hash')
418
+ request = request.to_hash
419
+ end
420
+ end
421
+
422
+ hash = call_api_json_service("/api/auth/authorization/fail", request)
423
+
424
+ Authlete::Model::Response::AuthorizationFailResponse.new(hash)
425
+ end
426
+
427
+ # Call Authlete's /api/auth/token API.
428
+ #
429
+ # On success, an instance of Authlete::Model::Response::TokenResponse is returned.
430
+ # On error, Authlete::Exception is raised.
431
+ def token(request)
432
+ if request.kind_of?(Hash) == false
433
+ if request.respond_to?('to_hash')
434
+ request = request.to_hash
435
+ end
436
+ end
437
+
438
+ hash = call_api_json_service("/api/auth/token", request)
439
+
440
+ Authlete::Model::Response::TokenResponse.new(hash)
441
+ end
442
+
443
+ # Call Authlete's /api/auth/token/issue API.
444
+ #
445
+ # On success, an instance of Authlete::Model::Response::TokenIssueResponse is returned.
446
+ # On error, Authlete::Exception is raised.
447
+ def token_issue(request)
448
+ if request.kind_of?(Hash) == false
449
+ if request.respond_to?('to_hash')
450
+ request = request.to_hash
451
+ end
452
+ end
453
+
454
+ hash = call_api_json_service("/api/auth/token/issue", request)
455
+
456
+ Authlete::Model::Response::TokenIssueResponse.new(hash)
457
+ end
458
+
459
+ # Call Authlete's /api/auth/token/fail API.
460
+ #
461
+ # On success, an instance of Authlete::Model::Response::TokenFailResponse is returned.
462
+ # On error, Authlete::Exception is raised.
463
+ def token_fail(request)
464
+ if request.kind_of?(Hash) == false
465
+ if request.respond_to?('to_hash')
466
+ request = request.to_hash
467
+ end
468
+ end
469
+
470
+ hash = call_api_json_service("/api/auth/token/fail", request)
471
+
472
+ Authlete::Model::Response::TokenFailResponse.new(hash)
473
+ end
474
+
475
+ # Call Authlete's /api/auth/introspection API.
396
476
  #
397
477
  # <tt>token</tt> is an access token presented by a client application.
398
478
  # This is a must parameter. In a typical case, a client application uses
@@ -409,13 +489,13 @@ module Authlete
409
489
  # subject, Authlete prepares the content of the error response.
410
490
  #
411
491
  # On success, this method returns an instance of
412
- # <tt>Authlete::Response::IntrospectionResponse</tt>. On error, this method
492
+ # <tt>Authlete::Model::Response::IntrospectionResponse</tt>. On error, this method
413
493
  # throws <tt>RestClient::Exception</tt>.
414
494
  def introspection(token, scopes = nil, subject = nil)
415
495
  hash = call_api_json_service('/api/auth/introspection',
416
496
  :token => token, :scopes => scopes, :subject => subject)
417
497
 
418
- Authlete::Response::IntrospectionResponse.new(hash)
498
+ Authlete::Model::Response::IntrospectionResponse.new(hash)
419
499
  end
420
500
 
421
501
 
@@ -423,7 +503,7 @@ module Authlete
423
503
  #
424
504
  # This method extracts an access token from the given request based on the
425
505
  # rules described in RFC 6750 and introspects the access token by calling
426
- # Authlete's /auth/introspection API.
506
+ # Authlete's /api/auth/introspection API.
427
507
  #
428
508
  # The first argument <tt>request</tt> is a Rack request.
429
509
  #
@@ -435,7 +515,7 @@ module Authlete
435
515
  # is optional.
436
516
  #
437
517
  # This method returns an instance of
438
- # <tt>Authlete::Response::IntrospectionResponse</tt>. If its <tt>action</tt>
518
+ # <tt>Authlete::Model::Response::IntrospectionResponse</tt>. If its <tt>action</tt>
439
519
  # method returns 'OK', it means that the access token exists, has not
440
520
  # expired, covers the requested scopes (if specified), and is associated
441
521
  # with the requested subject (if specified). Otherwise, it means that the
@@ -448,31 +528,31 @@ module Authlete
448
528
  # If the request does not contain any access token.
449
529
  if access_token.nil?
450
530
  # The request does not contain a valid access token.
451
- return Authlete::Response::IntrospectionResponse.new(
531
+ return Authlete::Model::Response::IntrospectionResponse.new(
452
532
  :action => 'BAD_REQUEST',
453
533
  :responseContent => 'Bearer error="invalid_token",error_description="The request does not contain a valid access token."'
454
534
  )
455
535
  end
456
536
 
457
537
  begin
458
- # Call Authlete's /auth/introspection API to introspect the access token.
538
+ # Call Authlete's /api/auth/introspection API to introspect the access token.
459
539
  result = introspection(access_token, scopes, subject)
460
540
  rescue => e
461
541
  # Error message.
462
- message = build_error_message('/auth/introspection', e)
542
+ message = build_error_message('/api/auth/introspection', e)
463
543
 
464
544
  # Emit a Rack error message.
465
545
  emit_rack_error_message(request, message)
466
546
 
467
547
  # Failed to introspect the access token.
468
- return Authlete::Response::IntrospectionResponse.new(
548
+ return Authlete::Model::Response::IntrospectionResponse.new(
469
549
  :action => 'INTERNAL_SERVER_ERROR',
470
550
  :responseContent => "Bearer error=\"server_error\",error_description=\"#{message}\""
471
551
  )
472
552
  end
473
553
 
474
- # Return the response from Authlete's /auth/introspection API.
554
+ # Return the response from Authlete's /api/auth/introspection API.
475
555
  result
476
556
  end
477
557
  end
478
- end
558
+ end
@@ -67,7 +67,7 @@ module Authlete
67
67
  end
68
68
 
69
69
  # Prepare an empty response.
70
- res = Authlete::Response::AuthenticationCallbackResponse.new
70
+ res = Authlete::Model::Response::AuthenticationCallbackResponse.new
71
71
 
72
72
  # Let the subclass authenticate the end-user.
73
73
  # When authenticated successfully, a non-nil value is returned.
@@ -163,7 +163,7 @@ module Authlete
163
163
  json = request.body.read
164
164
 
165
165
  # Parse the authentication callback request.
166
- Authlete::Request::AuthenticationCallbackRequest.parse(json)
166
+ Authlete::Model::Request::AuthenticationCallbackRequest.parse(json)
167
167
  end
168
168
 
169
169
  protected
@@ -184,7 +184,7 @@ module Authlete
184
184
  # This default implementation returns nil, meaning 'invalid end-user'.
185
185
  #
186
186
  # The argument of this method is an instance of
187
- # <tt>Authlete::Request::AuthenticationCallbackRequest</tt>.
187
+ # <tt>Authlete::Model::Request::AuthenticationCallbackRequest</tt>.
188
188
  # When the end-user is successfully authenticated, this method must
189
189
  # return a unique identifier (= subject) of the end-user. Otherwise,
190
190
  # nil must be returned to indicate authentication failure.
@@ -197,7 +197,7 @@ module Authlete
197
197
  # method. This default implementation returns nil, meaning 'no claim values'.
198
198
  #
199
199
  # The argument of this method is an instance of
200
- # <tt>Authlete::Request::AuthenticationCallbackRequest</tt> and the subject
200
+ # <tt>Authlete::Model::Request::AuthenticationCallbackRequest</tt> and the subject
201
201
  # which has been returned from the preceding call of <tt>authenticate_user</tt>
202
202
  # method.
203
203
  #
@@ -1,181 +1,127 @@
1
- # :nodoc:
2
- #
3
- # Copyright (C) 2014-2015 Authlete, Inc.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
-
18
- require 'set'
19
-
20
-
21
- module Authlete
22
- module Model
23
- class ClientList
24
- # The start index (inclusive) of the clients in this list.
25
- attr_accessor :start
26
-
27
- # The end index (exclusive) of the clients in this list.
28
- attr_accessor :end
29
-
30
- # The total count of clients.
31
- attr_accessor :totalCount
32
- alias_method :total_count, :totalCount
33
- alias_method :total_count=, :totalCount=
34
-
35
- # The list of clients.
36
- attr_accessor :clients
37
-
38
- private
39
-
40
- # Integer attributes.
41
- INTEGER_ATTRIBUTES = ::Set.new([:start, :end, :totalCount])
42
-
43
- # Mapping from snake cases to camel cases.
44
- SNAKE_TO_CAMEL = { :total_count => :totalCount }
45
-
46
- # The constructor
47
- def initialize(hash = nil)
48
- # Set default values to integer attributes.
49
- INTEGER_ATTRIBUTES.each do |attr|
50
- send("#{attr}=", 0)
51
- end
52
-
53
- @clients = nil
54
-
55
- # Set attribute values using the given hash.
56
- authlete_model_clientList_update(hash)
57
- end
58
-
59
- def authlete_model_clientList_to_key(key)
60
- key = key.to_sym
61
-
62
- # Convert snakecase to camelcase, if necessary.
63
- if SNAKE_TO_CAMEL.has_key?(key)
64
- key = SNAKE_TO_CAMEL[key]
65
- end
66
-
67
- return key
68
- end
69
-
70
- def authlete_model_clientList_simple_attribute?(key)
71
- INTEGER_ATTRIBUTES.include?(key)
72
- end
73
-
74
- def authlete_model_clientList_update(hash)
75
- if hash.nil?
76
- return
77
- end
78
-
79
- hash.each do |key, value|
80
- key = authlete_model_clientList_to_key(key)
81
-
82
- # If the attribute is a simple one.
83
- if authlete_model_clientList_simple_attribute?(key)
84
- send("#{key}=", value)
85
- next
86
- end
87
-
88
- if key == :clients
89
- # The attribute 'clients'.
90
- @clients = authlete_model_clientList_parse_array(value) do |element|
91
- Authlete::Model::Client.parse(element)
92
- end
93
- end
94
- end
95
-
96
- return self
97
- end
98
-
99
- def authlete_model_clientList_parse_array(array)
100
- if array.nil? or (array.kind_of?(Array) == false) or (array.length == 0)
101
- return nil
102
- end
103
-
104
- elements = []
105
-
106
- array.each do |element|
107
- parsed_element = yield(element)
108
-
109
- if parsed_element.nil? == false
110
- elements.push(parsed_element)
111
- end
112
- end
113
-
114
- if elements.length == 0
115
- return nil
116
- end
117
-
118
- return elements
119
- end
120
-
121
- public
122
-
123
- # Construct an instance from the given hash.
124
- #
125
- # If the given argument is nil or is not a Hash, nil is returned.
126
- # Otherwise, ClientList.new(hash) is returned.
127
- def self.parse(hash)
128
- if hash.nil? or (hash.kind_of?(Hash) == false)
129
- return nil
130
- end
131
-
132
- return ClientList.new(hash)
133
- end
134
-
135
- # Set attribute values using the given hash.
136
- def update(hash)
137
- authlete_model_clientList_update(hash)
138
- end
139
-
140
- # Convert this object into a hash.
141
- def to_hash
142
- hash = {}
143
-
144
- instance_variables.each do |var|
145
- key = var.to_s.delete("@").to_sym
146
- val = instance_variable_get(var)
147
-
148
- if authlete_model_clientList_simple_attribute?(key) or val.nil?
149
- hash[key] = val
150
- elsif val.kind_of?(Array)
151
- hash[key] = val.map {|element| element.to_hash}
152
- end
153
- end
154
-
155
- return hash
156
- end
157
-
158
- def [](key)
159
- key = authlete_model_clientList_to_key(key)
160
-
161
- if respond_to?(key)
162
- return send(key)
163
- else
164
- return nil
165
- end
166
- end
167
-
168
- def []=(key, value)
169
- key = authlete_model_clientList_to_key(key)
170
- method = "#{key}="
171
-
172
- if respond_to?(method)
173
- return send(method, value)
174
- else
175
- return nil
176
- end
177
- end
178
- end
179
- end
180
- end
181
-
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2014-2015 Authlete, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ require 'set'
19
+
20
+
21
+ module Authlete
22
+ module Model
23
+ class ClientList < Authlete::Model::Hashable
24
+ include Authlete::Utility
25
+ # The start index (inclusive) of the clients in this list.
26
+ attr_accessor :start
27
+
28
+ # The end index (exclusive) of the clients in this list.
29
+ attr_accessor :end
30
+
31
+ # The total count of clients.
32
+ attr_accessor :totalCount
33
+ alias_method :total_count, :totalCount
34
+ alias_method :total_count=, :totalCount=
35
+
36
+ # The list of clients.
37
+ attr_accessor :clients
38
+
39
+ private
40
+
41
+ # Integer attributes.
42
+ INTEGER_ATTRIBUTES = ::Set.new([ :start, :end, :totalCount ])
43
+
44
+ # Mapping from snake cases to camel cases.
45
+ SNAKE_TO_CAMEL = { :total_count => :totalCount }
46
+
47
+ # The constructor
48
+ def initialize(hash = nil)
49
+ # Set default values to integer attributes.
50
+ INTEGER_ATTRIBUTES.each do |attr|
51
+ send("#{attr}=", 0)
52
+ end
53
+
54
+ @clients = nil
55
+
56
+ # Set attribute values using the given hash.
57
+ authlete_model_update(hash)
58
+ end
59
+
60
+ def authlete_model_convert_key(key)
61
+ key = key.to_sym
62
+
63
+ # Convert snakecase to camelcase, if necessary.
64
+ if SNAKE_TO_CAMEL.has_key?(key)
65
+ key = SNAKE_TO_CAMEL[key]
66
+ end
67
+
68
+ key
69
+ end
70
+
71
+ def authlete_model_simple_attribute?(key)
72
+ INTEGER_ATTRIBUTES.include?(key)
73
+ end
74
+
75
+ def authlete_model_update(hash)
76
+ return if hash.nil?
77
+
78
+ hash.each do |key, value|
79
+ key = authlete_model_convert_key(key)
80
+
81
+ if authlete_model_simple_attribute?(key)
82
+ send("#{key}=", value)
83
+ elsif key == :clients
84
+ # Get an array consisting of "Client" objects.
85
+ @clients = get_parsed_array(value) do |element|
86
+ Authlete::Model::Client.parse(element)
87
+ end
88
+ end
89
+ end
90
+
91
+ self
92
+ end
93
+
94
+ public
95
+
96
+ # Construct an instance from the given hash.
97
+ #
98
+ # If the given argument is nil or is not a Hash, nil is returned.
99
+ # Otherwise, ClientList.new(hash) is returned.
100
+ def self.parse(hash)
101
+ if hash.nil? or (hash.kind_of?(Hash) == false)
102
+ return nil
103
+ end
104
+
105
+ return ClientList.new(hash)
106
+ end
107
+
108
+ # Convert this object into a hash.
109
+ def to_hash
110
+ hash = {}
111
+
112
+ instance_variables.each do |var|
113
+ key = var.to_s.delete("@").to_sym
114
+ val = instance_variable_get(var)
115
+
116
+ if authlete_model_simple_attribute?(key) or val.nil?
117
+ hash[key] = val
118
+ elsif val.kind_of?(Array)
119
+ hash[key] = val.map { |element| element.to_hash }
120
+ end
121
+ end
122
+
123
+ hash
124
+ end
125
+ end
126
+ end
127
+ end