domoscio_admin 0.2.0 → 0.3.1

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: 803479587b960b56624c80a2556cf6d2cf0a30fbbba879874e8126bf2a7f3097
4
+ data.tar.gz: a4af20195420db5d4fbd74188258b4797323363a7ea833b624e03db7c109c4f9
5
5
  SHA512:
6
- metadata.gz: 3dc3c6d2116930d2b29df39fe51eebd225f74c3837b4707fb37ec6bd5c8b224bfd550df41f50ffabcab50899948509f6e32c3c31e13752d2620a1fc6aaa3ed8b
7
- data.tar.gz: 33cf012a480e5fb6e9b48b5ace7deff1095c95c3be5ada2e0df6ddd141c386112e0005156f9297d3fd48635d15a1eebe36ef961857d332205d50e7d8aec03f18
6
+ metadata.gz: a8dd27e6cbe94b22326e72a4f74c0df2ecddf56139a4cf4bbf03f3aaf0266941b438bf1e4b26194510fa0d8a65e3513889f06363b34e15d8fb7663876ab9523d
7
+ data.tar.gz: 9ced7635b86302dab1def46dd0c8c95e0475a10d505af9264dea3038e6b6912df670a7ae882aa1a0350bbd503dabf48800a59f441f552436232d9f5916f7962f
@@ -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
@@ -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"
7
+ build_url = "/instances/#{DomoscioAdmin.configuration.client_id}/#{underscore(class_name)}s"
10
8
  build_url << "/#{CGI.escape(id.to_s)}" if id
11
- return build_url
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
@@ -1,3 +1,3 @@
1
1
  module DomoscioAdmin
2
- VERSION = "0.2.0"
2
+ VERSION = '0.3.1'.freeze
3
3
  end
@@ -1,14 +1,10 @@
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'
@@ -19,10 +15,12 @@ require 'domoscio_admin/resources/users/user'
19
15
 
20
16
  module DomoscioAdmin
21
17
  class Configuration
22
- attr_accessor :root_url, :client_id, :client_passphrase, :temp_dir
18
+ attr_accessor :client_id, :client_passphrase, :temp_dir, :root_url,
19
+ :file_storage, :access_token, :refresh_token
23
20
 
24
- def root_url
25
- @root_url ||= ""
21
+ def initialize
22
+ @root_url = ''
23
+ @file_storage ||= true
26
24
  end
27
25
  end
28
26
 
@@ -55,45 +53,48 @@ module DomoscioAdmin
55
53
  uri = api_uri(url)
56
54
 
57
55
  response = DomoscioAdmin.send_request(uri, method, params, headers)
58
- return response if response.kind_of? DomoscioAdmin::ProcessingError
56
+ return response if response.is_a? DomoscioAdmin::ProcessingError
59
57
 
60
58
  begin
61
59
  raise_http_failure(uri, response, params)
62
60
  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
61
+ if store_tokens
62
+ DomoscioAdmin::AuthorizationToken::Manager.storage.store({
63
+ access_token: response['Accesstoken'],
64
+ refresh_token: response['Refreshtoken']
65
+ })
66
+ end
67
+ rescue MultiJson::LoadError => e
68
+ data = ProcessingError.new(uri, 500, e, response.body, params)
69
+ rescue ResponseError => e
70
+ data = e
68
71
  end
69
72
 
70
73
  data
71
74
  end
72
75
 
73
- private
74
-
75
76
  # This function catches usual Http errors during calls
76
77
  #
77
78
  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
79
+ response = perform_call(uri, method, params, headers)
80
+ response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code
81
+ response
82
+ rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET,
83
+ EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
84
+ ProcessingError.new(uri, 500, e, response, params)
85
85
  end
86
86
 
87
87
  # This helper will check the response status and build the correcponding DomoscioAdmin::ResponseError
88
88
  #
89
89
  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
90
+ return if response.is_a? Net::HTTPSuccess
91
+
92
+ raise ResponseError.new(
93
+ uri,
94
+ response.code.to_i,
95
+ DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true),
96
+ response.body, params
97
+ )
97
98
  end
98
99
 
99
100
  # Actual HTTP call is performed here
@@ -108,26 +109,29 @@ module DomoscioAdmin
108
109
 
109
110
  # This method is called when AdaptiveEngine returns tokens errors
110
111
  # 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)
112
+ def self.retry_call_and_store_tokens(uri, method, params)
112
113
  headers = request_new_tokens
113
114
  response = perform_call(uri, method, params, headers)
114
- DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] })
115
+ DomoscioAdmin::AuthorizationToken::Manager.storage.store({
116
+ access_token: response['Accesstoken'],
117
+ refresh_token: response['Refreshtoken']
118
+ })
115
119
  response
116
120
  end
117
121
 
118
122
  def self.user_agent
119
- @uname ||= get_uname
123
+ @uname ||= uname
120
124
  {
121
125
  bindings_version: DomoscioAdmin::VERSION,
122
126
  lang: 'ruby',
123
127
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
124
128
  platform: RUBY_PLATFORM,
125
129
  uname: @uname
126
- }
130
+ }.to_s
127
131
  end
128
132
 
129
- def self.get_uname
130
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
133
+ def self.uname
134
+ `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i
131
135
  rescue Errno::ENOMEM
132
136
  'uname lookup failed'
133
137
  end
@@ -152,10 +156,10 @@ module DomoscioAdmin
152
156
  #
153
157
  def self.send_current_tokens(auth_token)
154
158
  {
155
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
156
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
157
- 'AccessToken' => "#{auth_token[:access_token]}",
158
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
159
+ 'user_agent' => DomoscioAdmin.user_agent,
160
+ 'ClientId' => DomoscioAdmin.configuration.client_id,
161
+ 'AccessToken' => auth_token[:access_token],
162
+ 'RefreshToken' => auth_token[:refresh_token],
159
163
  'Content-Type' => 'application/json'
160
164
  }
161
165
  end
@@ -163,17 +167,10 @@ module DomoscioAdmin
163
167
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
164
168
  def self.request_new_tokens
165
169
  {
166
- 'user_agent' => "#{DomoscioAdmin.user_agent}",
167
- 'ClientId' => "#{DomoscioAdmin.configuration.client_id}",
170
+ 'user_agent' => DomoscioAdmin.user_agent,
171
+ 'ClientId' => DomoscioAdmin.configuration.client_id,
168
172
  'Authorization' => "Token token=#{DomoscioAdmin.configuration.client_passphrase}",
169
173
  'Content-Type' => 'application/json'
170
174
  }
171
175
  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
176
  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.1
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-01-25 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,8 +51,6 @@ 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
@@ -61,14 +73,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
73
  requirements:
62
74
  - - ">="
63
75
  - !ruby/object:Gem::Version
64
- version: '0'
76
+ version: 2.5.0
65
77
  required_rubygems_version: !ruby/object:Gem::Requirement
66
78
  requirements:
67
79
  - - ">="
68
80
  - !ruby/object:Gem::Version
69
81
  version: '0'
70
82
  requirements: []
71
- rubygems_version: 3.0.8
83
+ rubygems_version: 3.1.2
72
84
  signing_key:
73
85
  specification_version: 4
74
86
  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