domoscio_admin 0.1.0 → 0.3.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a790c17d0b0a88271416a5f4a7fa5f2a06e4bb5b0a751b9d1e91483bc006d864
4
- data.tar.gz: bbd25ad9fb705fbea08d2e6099355c31e39c6cece6b3c07d0673d75f6887a422
3
+ metadata.gz: 9b05bb26fa031c8583ae9251bde136530c0d17f4da1263ab37d0fe40838fb6c0
4
+ data.tar.gz: ba80e651fb29852d63120262a8677970314ae6461da2bb9b0bd6b9a1bedda7ca
5
5
  SHA512:
6
- metadata.gz: a7f80348978cb8817f9b4bdbea72d96108226bd76090a2cf5181054f364ce010f1ef6fcc8e55c15f3c2f0601fd152e2717f8c674a7480abca0cb75e8296b3eee
7
- data.tar.gz: edf7e8648248865b3e5a96aa704f896ceb786b073aa906b507b7b92f0ab1a1a1d8124264a0bc537aedaa6804aa01af3a8f331fd85ca7abd2da0bacb91822b28b
6
+ metadata.gz: ebeae80ae1e4d863a3ca56e69d21255140ef43f4f0d0dc6dc93ff9145911e8def43a85852a5b541497c794329b9f2d77dc3b1cdf70fae5692163f4b1873a5098
7
+ data.tar.gz: 2bd5e27b58558110177d1e50a232189237b148bdccdbb2ce23bedc7040c643be8fad85992c67f73e612c7c373582b028b624d8d24c4a19ebf5d7f1e981799869
@@ -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
@@ -1,5 +1,5 @@
1
1
  module DomoscioAdmin
2
- class InstanceParameter < Resource
2
+ class InstanceParameter < ResourceInstance
3
3
  include DomoscioAdmin::HTTPCalls::Fetch
4
4
  include DomoscioAdmin::HTTPCalls::Update
5
5
  end
@@ -0,0 +1,8 @@
1
+ module DomoscioAdmin
2
+ class InstanceUser < 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
@@ -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
@@ -0,0 +1,9 @@
1
+ module DomoscioAdmin
2
+ class Resource
3
+ class << self
4
+ def class_name
5
+ name.split('::')[-1]
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module DomoscioAdmin
2
+ class ResourceInstance < Resource
3
+ class << self
4
+ def url(id = nil)
5
+ raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource
6
+
7
+ build_url = "/instances/#{DomoscioAdmin.configuration.client_id}/#{underscore(class_name)}s"
8
+ build_url << "/#{CGI.escape(id.to_s)}" 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
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,16 +1,12 @@
1
1
  module DomoscioAdmin
2
- class Resource
2
+ class ResourceUser < Resource
3
3
  class << self
4
- def class_name
5
- name.split('::')[-1]
6
- end
7
-
8
4
  def url(id = nil)
9
5
  if self == Resource
10
6
  raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
11
7
  end
12
8
 
13
- build_url = "/instances/#{DomoscioAdmin.configuration.client_id}/#{class_name.underscore}s"
9
+ build_url = "/users"
14
10
  build_url << "/#{CGI.escape(id.to_s)}" if id
15
11
  return build_url
16
12
  end
@@ -0,0 +1,5 @@
1
+ module DomoscioAdmin
2
+ class User < ResourceUser
3
+ include DomoscioAdmin::HTTPCalls::Fetch
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module DomoscioAdmin
2
- VERSION = "0.1.0"
2
+ VERSION = '0.3.3'.freeze
3
3
  end
@@ -1,24 +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
- require 'domoscio_admin/resource'
14
- require 'domoscio_admin/instance/instance_parameter'
9
+ require 'domoscio_admin/resources/resource'
10
+ require 'domoscio_admin/resources/resource_user'
11
+ require 'domoscio_admin/resources/resource_instance'
12
+ require 'domoscio_admin/resources/instances/instance_parameter'
13
+ require 'domoscio_admin/resources/instances/instance_user'
14
+ require 'domoscio_admin/resources/instances/student_group'
15
+ require 'domoscio_admin/resources/users/user'
15
16
 
16
17
  module DomoscioAdmin
17
18
  class Configuration
18
- 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
19
21
 
20
- def root_url
21
- @root_url ||= ""
22
+ def initialize
23
+ @root_url = ''
24
+ @file_storage ||= true
22
25
  end
23
26
  end
24
27
 
@@ -51,45 +54,48 @@ module DomoscioAdmin
51
54
  uri = api_uri(url)
52
55
 
53
56
  response = DomoscioAdmin.send_request(uri, method, params, headers)
54
- return response if response.kind_of? DomoscioAdmin::ProcessingError
57
+ return response if response.is_a? DomoscioAdmin::ProcessingError
55
58
 
56
59
  begin
57
60
  raise_http_failure(uri, response, params)
58
61
  data = DomoscioAdmin::JSON.load(response.body.nil? ? '' : response.body)
59
- DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) if store_tokens
60
- rescue MultiJson::LoadError => exception
61
- data = ProcessingError.new(uri, 500, exception, response.body, params)
62
- rescue ResponseError => exception
63
- 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
64
72
  end
65
73
 
66
74
  data
67
75
  end
68
76
 
69
- private
70
-
71
77
  # This function catches usual Http errors during calls
72
78
  #
73
79
  def self.send_request(uri, method, params, headers)
74
- begin
75
- response = perform_call(uri, method, params, headers)
76
- response = retry_call_and_store_tokens(uri, method, params, headers) if ['401', '403'].include? response.code
77
- response
78
- rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
79
- ProcessingError.new(uri, 500, exception, response, params)
80
- 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)
81
86
  end
82
87
 
83
88
  # This helper will check the response status and build the correcponding DomoscioAdmin::ResponseError
84
89
  #
85
90
  def self.raise_http_failure(uri, response, params)
86
- unless response.kind_of? Net::HTTPSuccess
87
- if response.blank?
88
- raise ResponseError.new(uri, 500, { error: { status: 500, message: 'DomoscioAdmin not available' } }, {}, params)
89
- else
90
- raise ResponseError.new(uri, response.code.to_i, DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
91
- end
92
- 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
+ )
93
99
  end
94
100
 
95
101
  # Actual HTTP call is performed here
@@ -104,26 +110,29 @@ module DomoscioAdmin
104
110
 
105
111
  # This method is called when AdaptiveEngine returns tokens errors
106
112
  # Action on those errors is to retry and request new tokens, those new token are then stored
107
- def self.retry_call_and_store_tokens(uri, method, params, headers)
113
+ def self.retry_call_and_store_tokens(uri, method, params)
108
114
  headers = request_new_tokens
109
115
  response = perform_call(uri, method, params, headers)
110
- 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
+ })
111
120
  response
112
121
  end
113
122
 
114
123
  def self.user_agent
115
- @uname ||= get_uname
124
+ @uname ||= uname
116
125
  {
117
126
  bindings_version: DomoscioAdmin::VERSION,
118
127
  lang: 'ruby',
119
128
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
120
129
  platform: RUBY_PLATFORM,
121
130
  uname: @uname
122
- }
131
+ }.to_s
123
132
  end
124
133
 
125
- def self.get_uname
126
- `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
127
136
  rescue Errno::ENOMEM
128
137
  'uname lookup failed'
129
138
  end
@@ -148,10 +157,10 @@ module DomoscioAdmin
148
157
  #
149
158
  def self.send_current_tokens(auth_token)
150
159
  {
151
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
152
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
153
- 'AccessToken' => "#{auth_token[:access_token]}",
154
- '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],
155
164
  'Content-Type' => 'application/json'
156
165
  }
157
166
  end
@@ -159,17 +168,10 @@ module DomoscioAdmin
159
168
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
160
169
  def self.request_new_tokens
161
170
  {
162
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
163
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
171
+ 'user_agent' => DomoscioAdmin.user_agent,
172
+ 'ClientId' => DomoscioAdmin.configuration.client_id,
164
173
  'Authorization' => "Token token=#{DomoscioAdmin.configuration.client_passphrase}",
165
174
  'Content-Type' => 'application/json'
166
175
  }
167
176
  end
168
-
169
- DomoscioAdmin.configure do |c|
170
- c.client_id = nil
171
- c.client_passphrase = nil
172
- c.temp_dir = File.expand_path('../tmp', __FILE__)
173
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
174
- end
175
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.1.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pascal Lim
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-08 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,19 +51,22 @@ 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
- - lib/domoscio_admin/instance/instance_parameter.rb
44
55
  - lib/domoscio_admin/json.rb
45
- - lib/domoscio_admin/resource.rb
56
+ - lib/domoscio_admin/resources/instances/instance_parameter.rb
57
+ - lib/domoscio_admin/resources/instances/instance_user.rb
58
+ - lib/domoscio_admin/resources/instances/student_group.rb
59
+ - lib/domoscio_admin/resources/resource.rb
60
+ - lib/domoscio_admin/resources/resource_instance.rb
61
+ - lib/domoscio_admin/resources/resource_user.rb
62
+ - lib/domoscio_admin/resources/users/user.rb
46
63
  - lib/domoscio_admin/version.rb
47
64
  - spec/spec_helper.rb
48
65
  homepage: https://www.domoscio.com
49
66
  licenses:
50
67
  - MIT
51
68
  metadata: {}
52
- post_install_message:
69
+ post_install_message:
53
70
  rdoc_options: []
54
71
  require_paths:
55
72
  - lib
@@ -57,15 +74,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
74
  requirements:
58
75
  - - ">="
59
76
  - !ruby/object:Gem::Version
60
- version: '0'
77
+ version: 2.5.0
61
78
  required_rubygems_version: !ruby/object:Gem::Requirement
62
79
  requirements:
63
80
  - - ">="
64
81
  - !ruby/object:Gem::Version
65
82
  version: '0'
66
83
  requirements: []
67
- rubygems_version: 3.0.8
68
- signing_key:
84
+ rubygems_version: 3.2.22
85
+ signing_key:
69
86
  specification_version: 4
70
87
  summary: Summary of DomoscioAdmin.
71
88
  test_files:
@@ -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