domoscio_admin 0.2.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa06e4ae7a619c9aff98963f81a181fd223caff8bfc2b918c9bae2f2c2d11547
4
- data.tar.gz: 70f0295138008845cd776db1cd34a6e164436e5c41bddb7d78846e96f5736508
3
+ metadata.gz: 5214e024b43a24c60d9809df58679f069fddb06eefc4c77854f65b71a1a396e9
4
+ data.tar.gz: 6d94481bf938506dbb0ecfc101923b915c66f38b3de172be0d3eeca5729f14b9
5
5
  SHA512:
6
- metadata.gz: 3dc3c6d2116930d2b29df39fe51eebd225f74c3837b4707fb37ec6bd5c8b224bfd550df41f50ffabcab50899948509f6e32c3c31e13752d2620a1fc6aaa3ed8b
7
- data.tar.gz: 33cf012a480e5fb6e9b48b5ace7deff1095c95c3be5ada2e0df6ddd141c386112e0005156f9297d3fd48635d15a1eebe36ef961857d332205d50e7d8aec03f18
6
+ metadata.gz: 4a59bc2a5132a3dc3b797c388738aa47172a3185bae38d4b1e2d886f69e2b9e53b656d41a10089d769dad94462d4b1808cfd92b8c2acbe93ff15d60838574eda
7
+ data.tar.gz: 5460680c16b4b5825e80f577ad811fff669456e2db9359b39b71ccdf9a7a5c517f7fdc16032b41078d64fe94333a306a8d486d72a8aff41252dc944b81b33d83
@@ -1,54 +1,53 @@
1
1
  module DomoscioAdmin
2
2
  module AuthorizationToken
3
+ # Token Manager
3
4
  class Manager
4
5
  class << self
5
6
  def storage
6
- @@storage ||= FileStorage.new
7
+ @storage ||= DomoscioAdmin.configuration.file_storage ? FileStorage.new : ManualSetting.new
7
8
  end
8
9
 
9
- def storage=(storage)
10
- @@storage = storage
11
- end
12
-
13
- def get_token
10
+ def token
14
11
  storage.get
15
12
  end
16
13
  end
17
14
  end
18
15
 
19
- class StaticStorage
16
+ # Manages tokens with configuration
17
+ class ManualSetting
20
18
  def get
21
- @@token ||= nil
19
+ {
20
+ access_token: DomoscioAdmin.configuration.access_token,
21
+ refresh_token: DomoscioAdmin.configuration.refresh_token
22
+ }
22
23
  end
23
24
 
24
25
  def store(token)
25
- @@token = token
26
+ DomoscioAdmin.configuration.access_token = token[:access_token]
27
+ DomoscioAdmin.configuration.refresh_token = token[:refresh_token]
26
28
  end
27
29
  end
28
30
 
31
+ # Manages tokens with local File
29
32
  class FileStorage
30
33
  require 'yaml'
31
- @temp_dir
32
34
 
33
- def initialize(temp_dir = nil)
34
- @temp_dir = temp_dir || DomoscioAdmin.configuration.temp_dir
35
- raise "Path to temporary folder is not defined" unless @temp_dir
35
+ def initialize
36
+ raise 'Path to temporary folder is not defined' unless DomoscioAdmin.configuration.temp_dir
36
37
  end
37
38
 
38
39
  def get
39
- begin
40
- f = File.open(file_path, File::RDONLY)
41
- f.flock(File::LOCK_SH)
42
- txt = f.read
43
- f.close
44
- YAML.load(txt) || nil
45
- rescue Errno::ENOENT
46
- nil
47
- end
40
+ f = File.open(file_path, File::RDONLY)
41
+ f.flock(File::LOCK_SH)
42
+ txt = f.read
43
+ f.close
44
+ YAML.safe_load(txt) || nil
45
+ rescue Errno::ENOENT
46
+ nil
48
47
  end
49
48
 
50
49
  def store(token)
51
- File.open(file_path, File::RDWR | File::CREAT, 0644) do |f|
50
+ File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
52
51
  f.flock(File::LOCK_EX)
53
52
  f.truncate(0)
54
53
  f.rewind
@@ -57,7 +56,7 @@ module DomoscioAdmin
57
56
  end
58
57
 
59
58
  def file_path
60
- File.join(@temp_dir, "DomoscioAdmin.AuthorizationToken.FileStore.tmp")
59
+ File.join(DomoscioAdmin.configuration.temp_dir, 'DomoscioAdmin.AuthorizationToken.FileStore.tmp')
61
60
  end
62
61
  end
63
62
  end
@@ -1,20 +1,24 @@
1
1
  module DomoscioAdmin
2
2
  # Generic error superclass for MangoPay specific errors.
3
- # Currently never instantiated directly.
4
- # Currently only single subclass used.
5
3
  class Error < StandardError
6
4
  end
7
5
 
8
- # ResponseError from DomoscioAdmin
6
+ # Error Message from DomoAdmin
9
7
  class ResponseError < Error
10
8
  attr_reader :request_url, :code, :details, :body, :request_params
11
9
 
12
10
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
13
- @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
11
+ @request_url = request_url
12
+ @code = code
13
+ @details = details
14
+ @body = body
15
+ @request_params = request_params
14
16
  super(message) if message
15
17
  end
16
18
 
17
- def message; @details.is_a?(Hash) ? @details.dig(:error, :message) : @details; end
19
+ def message
20
+ @details.is_a?(Hash) && @details[:error].is_a?(Hash) ? @details.dig(:error, :message) : @details
21
+ end
18
22
  end
19
23
 
20
24
  # ProcessingError from DomoscioAdmin
@@ -22,10 +26,16 @@ module DomoscioAdmin
22
26
  attr_reader :request_url, :code, :details, :body, :request_params
23
27
 
24
28
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
25
- @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
29
+ @request_url = request_url
30
+ @code = code
31
+ @details = details
32
+ @body = body
33
+ @request_params = request_params
26
34
  super(message) if message
27
35
  end
28
36
 
29
- def message; @details.message; end
37
+ def message
38
+ @details.message
39
+ end
30
40
  end
31
41
  end
@@ -0,0 +1,8 @@
1
+ module DomoscioAdmin
2
+ class StudentGroup < ResourceInstance
3
+ include DomoscioAdmin::HTTPCalls::Create
4
+ include DomoscioAdmin::HTTPCalls::Fetch
5
+ include DomoscioAdmin::HTTPCalls::Update
6
+ include DomoscioAdmin::HTTPCalls::Destroy
7
+ end
8
+ end
@@ -2,13 +2,19 @@ module DomoscioAdmin
2
2
  class ResourceInstance < Resource
3
3
  class << self
4
4
  def url(id = nil)
5
- if self == Resource
6
- raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
7
- end
5
+ raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource
8
6
 
9
- build_url = "/instances/#{DomoscioAdmin.configuration.client_id}/#{class_name.underscore}s"
10
- build_url << "/#{CGI.escape(id.to_s)}" if id
11
- return build_url
7
+ build_url = "/instances/#{DomoscioAdmin.configuration.client_id}/#{underscore(class_name)}s"
8
+ build_url << "/#{CGI.escape(id.to_s.replace('.', '%2E'))}" if id
9
+ build_url
10
+ end
11
+
12
+ def underscore(string)
13
+ string.gsub(/::/, '/')
14
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
15
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
16
+ .tr('-', '_')
17
+ .downcase
12
18
  end
13
19
  end
14
20
  end
@@ -7,7 +7,7 @@ module DomoscioAdmin
7
7
  end
8
8
 
9
9
  build_url = "/users"
10
- build_url << "/#{CGI.escape(id.to_s)}" if id
10
+ build_url << "/#{CGI.escape(id.to_s.replace('.', '%2E'))}" if id
11
11
  return build_url
12
12
  end
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module DomoscioAdmin
2
- VERSION = "0.2.0"
2
+ VERSION = '0.3.4'.freeze
3
3
  end
@@ -1,28 +1,27 @@
1
1
  require 'net/https'
2
2
  require 'cgi/util'
3
3
  require 'multi_json'
4
- # helpers
5
4
  require 'domoscio_admin/version'
6
5
  require 'domoscio_admin/json'
7
6
  require 'domoscio_admin/errors'
8
7
  require 'domoscio_admin/authorization_token'
9
- # generators
10
- require 'domoscio_admin/generators/install_generator'
11
- # resources
12
8
  require 'domoscio_admin/http_calls'
13
9
  require 'domoscio_admin/resources/resource'
14
10
  require 'domoscio_admin/resources/resource_user'
15
11
  require 'domoscio_admin/resources/resource_instance'
16
12
  require 'domoscio_admin/resources/instances/instance_parameter'
17
13
  require 'domoscio_admin/resources/instances/instance_user'
14
+ require 'domoscio_admin/resources/instances/student_group'
18
15
  require 'domoscio_admin/resources/users/user'
19
16
 
20
17
  module DomoscioAdmin
21
18
  class Configuration
22
- attr_accessor :root_url, :client_id, :client_passphrase, :temp_dir
19
+ attr_accessor :client_id, :client_passphrase, :temp_dir, :root_url,
20
+ :file_storage, :access_token, :refresh_token
23
21
 
24
- def root_url
25
- @root_url ||= ""
22
+ def initialize
23
+ @root_url = ''
24
+ @file_storage ||= true
26
25
  end
27
26
  end
28
27
 
@@ -55,45 +54,48 @@ module DomoscioAdmin
55
54
  uri = api_uri(url)
56
55
 
57
56
  response = DomoscioAdmin.send_request(uri, method, params, headers)
58
- return response if response.kind_of? DomoscioAdmin::ProcessingError
57
+ return response if response.is_a? DomoscioAdmin::ProcessingError
59
58
 
60
59
  begin
61
60
  raise_http_failure(uri, response, params)
62
61
  data = DomoscioAdmin::JSON.load(response.body.nil? ? '' : response.body)
63
- DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) if store_tokens
64
- rescue MultiJson::LoadError => exception
65
- data = ProcessingError.new(uri, 500, exception, response.body, params)
66
- rescue ResponseError => exception
67
- data = exception
62
+ if store_tokens
63
+ DomoscioAdmin::AuthorizationToken::Manager.storage.store({
64
+ access_token: response['Accesstoken'],
65
+ refresh_token: response['Refreshtoken']
66
+ })
67
+ end
68
+ rescue MultiJson::LoadError => e
69
+ data = ProcessingError.new(uri, 500, e, response.body, params)
70
+ rescue ResponseError => e
71
+ data = e
68
72
  end
69
73
 
70
74
  data
71
75
  end
72
76
 
73
- private
74
-
75
77
  # This function catches usual Http errors during calls
76
78
  #
77
79
  def self.send_request(uri, method, params, headers)
78
- begin
79
- response = perform_call(uri, method, params, headers)
80
- response = retry_call_and_store_tokens(uri, method, params, headers) if ['401', '403'].include? response.code
81
- response
82
- rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
83
- ProcessingError.new(uri, 500, exception, response, params)
84
- end
80
+ response = perform_call(uri, method, params, headers)
81
+ response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code
82
+ response
83
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET,
84
+ EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
85
+ ProcessingError.new(uri, 500, e, response, params)
85
86
  end
86
87
 
87
88
  # This helper will check the response status and build the correcponding DomoscioAdmin::ResponseError
88
89
  #
89
90
  def self.raise_http_failure(uri, response, params)
90
- unless response.kind_of? Net::HTTPSuccess
91
- if response.blank?
92
- raise ResponseError.new(uri, 500, { error: { status: 500, message: 'DomoscioAdmin not available' } }, {}, params)
93
- else
94
- raise ResponseError.new(uri, response.code.to_i, DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
95
- end
96
- end
91
+ return if response.is_a? Net::HTTPSuccess
92
+
93
+ raise ResponseError.new(
94
+ uri,
95
+ response.code.to_i,
96
+ DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true),
97
+ response.body, params
98
+ )
97
99
  end
98
100
 
99
101
  # Actual HTTP call is performed here
@@ -108,26 +110,29 @@ module DomoscioAdmin
108
110
 
109
111
  # This method is called when AdaptiveEngine returns tokens errors
110
112
  # Action on those errors is to retry and request new tokens, those new token are then stored
111
- def self.retry_call_and_store_tokens(uri, method, params, headers)
113
+ def self.retry_call_and_store_tokens(uri, method, params)
112
114
  headers = request_new_tokens
113
115
  response = perform_call(uri, method, params, headers)
114
- DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] })
116
+ DomoscioAdmin::AuthorizationToken::Manager.storage.store({
117
+ access_token: response['Accesstoken'],
118
+ refresh_token: response['Refreshtoken']
119
+ })
115
120
  response
116
121
  end
117
122
 
118
123
  def self.user_agent
119
- @uname ||= get_uname
124
+ @uname ||= uname
120
125
  {
121
126
  bindings_version: DomoscioAdmin::VERSION,
122
127
  lang: 'ruby',
123
128
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
124
129
  platform: RUBY_PLATFORM,
125
130
  uname: @uname
126
- }
131
+ }.to_s
127
132
  end
128
133
 
129
- def self.get_uname
130
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
134
+ def self.uname
135
+ `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i
131
136
  rescue Errno::ENOMEM
132
137
  'uname lookup failed'
133
138
  end
@@ -152,10 +157,10 @@ module DomoscioAdmin
152
157
  #
153
158
  def self.send_current_tokens(auth_token)
154
159
  {
155
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
156
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
157
- 'AccessToken' => "#{auth_token[:access_token]}",
158
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
160
+ 'user_agent' => DomoscioAdmin.user_agent,
161
+ 'ClientId' => DomoscioAdmin.configuration.client_id,
162
+ 'AccessToken' => auth_token[:access_token],
163
+ 'RefreshToken' => auth_token[:refresh_token],
159
164
  'Content-Type' => 'application/json'
160
165
  }
161
166
  end
@@ -163,17 +168,10 @@ module DomoscioAdmin
163
168
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
164
169
  def self.request_new_tokens
165
170
  {
166
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
167
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
171
+ 'user_agent' => DomoscioAdmin.user_agent,
172
+ 'ClientId' => DomoscioAdmin.configuration.client_id,
168
173
  'Authorization' => "Token token=#{DomoscioAdmin.configuration.client_passphrase}",
169
174
  'Content-Type' => 'application/json'
170
175
  }
171
176
  end
172
-
173
- DomoscioAdmin.configure do |c|
174
- c.client_id = nil
175
- c.client_passphrase = nil
176
- c.temp_dir = File.expand_path('../tmp', __FILE__)
177
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
178
- end
179
177
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domoscio_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pascal Lim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-24 00:00:00.000000000 Z
11
+ date: 2022-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: multi_json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
19
+ version: 1.15.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.2'
26
+ version: 1.15.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: yaml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
27
41
  description: Ruby client to interact with Domoscio Admin.
28
42
  email:
29
43
  - pascal.lim@domoscio.com
@@ -37,12 +51,11 @@ files:
37
51
  - lib/domoscio_admin.rb
38
52
  - lib/domoscio_admin/authorization_token.rb
39
53
  - lib/domoscio_admin/errors.rb
40
- - lib/domoscio_admin/generators/install_generator.rb
41
- - lib/domoscio_admin/generators/templates/install.rb
42
54
  - lib/domoscio_admin/http_calls.rb
43
55
  - lib/domoscio_admin/json.rb
44
56
  - lib/domoscio_admin/resources/instances/instance_parameter.rb
45
57
  - lib/domoscio_admin/resources/instances/instance_user.rb
58
+ - lib/domoscio_admin/resources/instances/student_group.rb
46
59
  - lib/domoscio_admin/resources/resource.rb
47
60
  - lib/domoscio_admin/resources/resource_instance.rb
48
61
  - lib/domoscio_admin/resources/resource_user.rb
@@ -61,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
74
  requirements:
62
75
  - - ">="
63
76
  - !ruby/object:Gem::Version
64
- version: '0'
77
+ version: 2.5.0
65
78
  required_rubygems_version: !ruby/object:Gem::Requirement
66
79
  requirements:
67
80
  - - ">="
68
81
  - !ruby/object:Gem::Version
69
82
  version: '0'
70
83
  requirements: []
71
- rubygems_version: 3.0.8
84
+ rubygems_version: 3.2.22
72
85
  signing_key:
73
86
  specification_version: 4
74
87
  summary: Summary of DomoscioAdmin.
@@ -1,10 +0,0 @@
1
- require 'rails/generators'
2
- module DomoscioAdmin
3
- class InstallGenerator < ::Rails::Generators::Base
4
- source_root File.expand_path('../templates', __FILE__)
5
- desc "Generate config file for DomoscioAdmin configuration"
6
- def install
7
- copy_file "install.rb", "config/initializers/domoscio_admin.rb"
8
- end
9
- end
10
- end
@@ -1,6 +0,0 @@
1
- DomoscioAdmin.configure do |c|
2
- c.client_id = ENV['DOMOSCIO_ID']
3
- c.client_passphrase = ENV['DOMOSCIO_PASSWORD']
4
- c.temp_dir = File.expand_path('../tmp', __FILE__)
5
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
6
- end