domoscio_viz 0.2.3 → 0.3.1

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: 050c44f613da493c3ca25fd68c8ba142362bf42f461cb20e39934a88688bc53f
4
- data.tar.gz: 83f2f4df4edcb932d70fd9a52b993369a3e758c2ec4f9d1d43c0da9ee458bcbd
3
+ metadata.gz: a27998a16cab08757bb639a589e3b6fac992c8edc215ddd1bef66244be05c983
4
+ data.tar.gz: 5574e4cde4e47c488626f6598fe1b2299221bb753416b8c388f8ff3cf190581d
5
5
  SHA512:
6
- metadata.gz: 7ad6e1ebc52711ef09bcaf1fbe20da7ae480750829b967702094963f63db1c7679206db7333533d02157235c5056bd8876a5cff76618bac08e584209893d5912
7
- data.tar.gz: e9d0ad596fe0f9c4f8e5411d7a41e962ece08f5c19a7c6ee9b568f24008b76a4d1484914a745aa6d39fad62c4d3e270367c5b62a5da15d64e1f3d042f7c48d14
6
+ metadata.gz: 5f72c732c781f5cc4ab2fd5181547c7e65c07af79b12c98a2554885af42802d08abef81eb86329a4dd374f682d74929c6c6430e56e52515a57cb59931d9c8ce2
7
+ data.tar.gz: 5b3e486ef10f27616094adf973124afcb86c3b202c742500c73e635ce8040d11430708bdd3aee31e81ad6d182dcc3632019181778b861e75d9a9de3f4be40678
data/Rakefile CHANGED
@@ -14,9 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
-
18
-
19
-
20
17
  Bundler::GemHelper.install_tasks
21
18
 
22
19
  require 'rake/testtask'
@@ -28,5 +25,4 @@ Rake::TestTask.new(:test) do |t|
28
25
  t.verbose = false
29
26
  end
30
27
 
31
-
32
28
  task default: :test
@@ -1,59 +1,53 @@
1
1
  module DomoscioViz
2
2
  module AuthorizationToken
3
+ # Token Manager
3
4
  class Manager
4
-
5
5
  class << self
6
6
  def storage
7
- @@storage ||= FileStorage.new
8
- end
9
-
10
- def storage= (storage)
11
- @@storage = storage
7
+ @storage ||= DomoscioViz.configuration.file_storage ? FileStorage.new : ManualSetting.new
12
8
  end
13
9
 
14
- def get_token
15
- token = storage.get
16
- token = DomoscioViz.configuration.client_passphrase if token.nil?
17
- token
10
+ def token
11
+ storage.get
18
12
  end
19
13
  end
20
14
  end
21
15
 
22
- class StaticStorage
16
+ # Manages tokens with configuration
17
+ class ManualSetting
23
18
  def get
24
- @@token ||= nil
19
+ {
20
+ access_token: DomoscioViz.configuration.access_token,
21
+ refresh_token: DomoscioViz.configuration.refresh_token
22
+ }
25
23
  end
26
24
 
27
25
  def store(token)
28
- @@token = token
26
+ DomoscioViz.configuration.access_token = token[:access_token]
27
+ DomoscioViz.configuration.refresh_token = token[:refresh_token]
29
28
  end
30
29
  end
31
30
 
31
+ # Manages tokens with local File
32
32
  class FileStorage
33
33
  require 'yaml'
34
- @temp_dir
35
34
 
36
- def initialize(temp_dir = nil)
37
- @temp_dir = temp_dir || DomoscioViz.configuration.temp_dir
38
- if !@temp_dir
39
- raise "Path to temporary folder is not defined"
40
- end
35
+ def initialize
36
+ raise 'Path to temporary folder is not defined' unless DomoscioViz.configuration.temp_dir
41
37
  end
42
38
 
43
39
  def get
44
- begin
45
- f = File.open(file_path, File::RDONLY)
46
- f.flock(File::LOCK_SH)
47
- txt = f.read
48
- f.close
49
- YAML.load(txt) || nil
50
- rescue Errno::ENOENT
51
- nil
52
- 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
53
47
  end
54
48
 
55
49
  def store(token)
56
- File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
50
+ File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
57
51
  f.flock(File::LOCK_EX)
58
52
  f.truncate(0)
59
53
  f.rewind
@@ -62,8 +56,8 @@ module DomoscioViz
62
56
  end
63
57
 
64
58
  def file_path
65
- File.join(@temp_dir, "DomoscioViz.AuthorizationToken.FileStore.tmp")
59
+ File.join(DomoscioViz.configuration.temp_dir, 'DomoscioViz.AuthorizationToken.FileStore.tmp')
66
60
  end
67
61
  end
68
62
  end
69
- end
63
+ end
@@ -1,6 +1,6 @@
1
1
  module DomoscioViz
2
- # A Recommandation.
2
+ # Regroup all charts
3
3
  class Chart < Resource
4
4
  include DomoscioViz::HTTPCalls::GetUrl
5
5
  end
6
- end
6
+ end
@@ -1,26 +1,41 @@
1
1
  module DomoscioViz
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
- # ErrorMessage from VizEngine
5
+
6
+ # Error Message from VizEngine
8
7
  class ResponseError < Error
9
8
  attr_reader :request_url, :code, :details, :body, :request_params
10
- def initialize(request_url, code, details, body, request_params)
11
- @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
9
+
10
+ def initialize(request_url, code, details = {}, body = nil, request_params = {})
11
+ @request_url = request_url
12
+ @code = code
13
+ @details = details
14
+ @body = body
15
+ @request_params = request_params
12
16
  super(message) if message
13
17
  end
14
- def message; @details.dig(:error, :message) || @details; end
18
+
19
+ def message
20
+ @details.is_a?(Hash) && @details[:error].is_a?(Hash) ? @details.dig(:error, :message) : @details
21
+ end
15
22
  end
16
23
 
17
- # ProcessingError from Domoscio_viz
24
+ # ProcessingError from DomoscioViz
18
25
  class ProcessingError < Error
19
26
  attr_reader :request_url, :code, :details, :body, :request_params
20
- def initialize(request_url, code, details, body, request_params)
21
- @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
27
+
28
+ def initialize(request_url, code, details = {}, body = nil, request_params = {})
29
+ @request_url = request_url
30
+ @code = code
31
+ @details = details
32
+ @body = body
33
+ @request_params = request_params
22
34
  super(message) if message
23
35
  end
24
- def message; @details.message; end
36
+
37
+ def message
38
+ @details.message
39
+ end
25
40
  end
26
- end
41
+ end
@@ -2,18 +2,14 @@ module DomoscioViz
2
2
  module HTTPCalls
3
3
  module GetUrl
4
4
  module ClassMethods
5
- # In order to catch current SocketsTimeoutError that seem to appear when the VizEngine stays Idle for too long
6
- # Perform the request a second time if it fails the fisrt time
7
5
  def get_url(util_name = nil, params = {})
8
- response = DomoscioViz.request(:post, url(util_name), params)
9
- response = DomoscioViz.request(:post, url(util_name), params) unless response && response.is_a?(Hash) && response['url'] && response['success'] == true
10
- response
6
+ DomoscioViz.request(:post, url(util_name), params)
11
7
  end
12
8
  end
13
9
 
14
10
  def self.included(base)
15
11
  base.extend(ClassMethods)
16
- end
12
+ end
17
13
  end
18
14
  end
19
- end
15
+ end
@@ -20,4 +20,4 @@ module DomoscioViz
20
20
  end
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -2,23 +2,11 @@ module DomoscioViz
2
2
  # @abstract
3
3
  class Resource
4
4
  class << self
5
- def class_name
6
- name.split('::')[-1]
7
- end
5
+ def url(util_name = nil, on_self = nil)
6
+ raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource
8
7
 
9
- def url(util_name = nil, on_self = nil )
10
- if self == Resource
11
- raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
12
- end
13
-
14
- build_url = ""
15
- if !on_self
16
- if util_name
17
- build_url << "/#{util_name}"
18
- end
19
- end
20
- return build_url
8
+ util_name && !on_self ? "/#{util_name}" : ''
21
9
  end
22
10
  end
23
11
  end
24
- end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module DomoscioViz
2
- VERSION = "0.2.3"
3
- end
2
+ VERSION = '0.3.1'.freeze
3
+ end
data/lib/domoscio_viz.rb CHANGED
@@ -1,44 +1,23 @@
1
-
2
1
  require 'net/https'
3
2
  require 'cgi/util'
4
3
  require 'multi_json'
5
- # helpers
4
+
6
5
  require 'domoscio_viz/version'
7
6
  require 'domoscio_viz/json'
8
7
  require 'domoscio_viz/errors'
9
8
  require 'domoscio_viz/authorization_token'
10
- # generators
11
- require 'domoscio_viz/generators/install_generator'
12
- # resources
13
9
  require 'domoscio_viz/http_calls'
14
10
  require 'domoscio_viz/resource'
15
11
  require 'domoscio_viz/chart/chart'
16
12
 
17
13
  module DomoscioViz
18
-
19
14
  class Configuration
20
- attr_accessor :preproduction, :test, :root_url,
21
- :client_id, :client_passphrase,
22
- :temp_dir
23
-
24
- def preproduction
25
- @preproduction || false
26
- end
27
-
28
- def test
29
- @test || false
30
- end
15
+ attr_accessor :client_id, :client_passphrase, :temp_dir,
16
+ :root_url, :file_storage, :access_token, :refresh_token
31
17
 
32
- def root_url
33
- if @preproduction == true
34
- if @test == true
35
- @root_url || "https://domoscio-viz-engine-preprod.azurewebsites.net"
36
- else
37
- @root_url || "https://domoscio-viz-engine-v2.azurewebsites.net"
38
- end
39
- else
40
- @root_url || "http://localhost:3002"
41
- end
18
+ def initialize
19
+ @root_url = ''
20
+ @file_storage ||= true
42
21
  end
43
22
  end
44
23
 
@@ -56,85 +35,135 @@ module DomoscioViz
56
35
  end
57
36
 
58
37
  #
59
- def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
60
- return false if @disabled
38
+ # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
39
+ # - +url+: the part after Configuration#root_url
40
+ # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
41
+ #
42
+ # Performs HTTP requests to Adaptive Engine
43
+ # On token issues, will try once to get a new token then will output a DomoscioViz::ReponseError with details
44
+ #
45
+ # Raises DomoscioViz::ResponseError on Adaptive Error Status
46
+ # Raises DomoscioViz::ProcessingError on Internal Error
47
+ #
48
+ def self.request(method, url, params={})
49
+ store_tokens, headers = request_headers
61
50
  uri = api_uri(url)
62
- uri.query = URI.encode_www_form(filters) unless filters.empty?
63
- res = DomoscioViz.send_request(uri, method, params, headers, before_request_proc)
64
- return res if res.kind_of? DomoscioViz::ProcessingError
65
- # decode json data
66
- begin
67
- data = DomoscioViz::JSON.load(res.body.nil? ? '' : res.body)
68
- raise ResponseError.new(uri, res.code.to_i, data, res.body, params) unless res.kind_of? Net::HTTPSuccess
69
- unless (res.kind_of? Net::HTTPClientError) || (res.kind_of? Net::HTTPServerError)
70
- DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: res['Accesstoken'], refresh_token: res['Refreshtoken']})
51
+ response = DomoscioViz.send_request(uri, method, params, headers)
52
+ return response if response.is_a? DomoscioViz::ProcessingError
53
+
54
+ begin
55
+ raise_http_failure(uri, response, params)
56
+ data = DomoscioViz::JSON.load(response.body.nil? ? '' : response.body)
57
+ if store_tokens
58
+ DomoscioViz::AuthorizationToken::Manager.storage.store({
59
+ access_token: response['Accesstoken'],
60
+ refresh_token: response['Refreshtoken']
61
+ })
71
62
  end
72
- rescue MultiJson::LoadError => exception
73
- return ProcessingError.new(uri, 500, exception, res.body, params)
74
- rescue ResponseError => exception
75
- return exception
63
+ rescue MultiJson::LoadError => e
64
+ data = ProcessingError.new(uri, 500, e, response.body, params)
65
+ rescue ResponseError => e
66
+ data = e
76
67
  end
77
68
  data
78
69
  end
79
70
 
80
- def self.send_request(uri, method, params, headers, before_request_proc)
81
- begin
82
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
83
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
84
- req.body = DomoscioRails::JSON.dump(params)
85
- before_request_proc.call(req) if before_request_proc
86
- http.request req
87
- end
88
- rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
89
- ProcessingError.new(uri, 500, exception, res.body, params)
71
+ # This function catches usual Http errors during calls
72
+ #
73
+ def self.send_request(uri, method, params, headers)
74
+ response = perform_call(uri, method, params, headers)
75
+ response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code
76
+ response
77
+ rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED,
78
+ Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
79
+ ProcessingError.new(uri, 500, e, response, params)
80
+ end
81
+
82
+ # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
83
+ #
84
+ def self.raise_http_failure(uri, response, params)
85
+ return if response.is_a? Net::HTTPSuccess
86
+
87
+ raise ResponseError.new(
88
+ uri,
89
+ response.code.to_i,
90
+ DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true),
91
+ response.body, params
92
+ )
93
+ end
94
+
95
+ # Actual HTTP call is performed here
96
+ #
97
+ def self.perform_call(uri, method, params, headers)
98
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
99
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
100
+ req.body = DomoscioRails::JSON.dump(params)
101
+ http.request req
90
102
  end
91
103
  end
92
104
 
93
- private
105
+ # This method is called when AdaptiveEngine returns tokens errors
106
+ # 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)
108
+ headers = request_new_tokens
109
+ response = perform_call(uri, method, params, headers)
110
+ DomoscioViz::AuthorizationToken::Manager.storage.store({
111
+ access_token: response['Accesstoken'],
112
+ refresh_token: response['Refreshtoken']
113
+ })
114
+ response
115
+ end
94
116
 
95
117
  def self.user_agent
96
- @uname ||= get_uname
118
+ @uname ||= uname
97
119
  {
98
120
  bindings_version: DomoscioViz::VERSION,
99
121
  lang: 'ruby',
100
122
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
101
123
  platform: RUBY_PLATFORM,
102
124
  uname: @uname
103
- }
125
+ }.to_s
104
126
  end
105
127
 
106
- def self.get_uname
107
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
128
+ def self.uname
129
+ `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i
108
130
  rescue Errno::ENOMEM
109
131
  'uname lookup failed'
110
132
  end
111
133
 
134
+ # Process the token loading and analyze
135
+ # will return the processed headers and a token store flag
136
+ #
112
137
  def self.request_headers
113
138
  auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
114
- if !auth_token.is_a? String
115
- headers = {
116
- 'user_agent' => "#{DomoscioViz.user_agent}",
117
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
118
- 'AccessToken' => "#{auth_token[:access_token]}",
119
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
120
- 'Content-Type' => 'application/json'
121
- }
139
+ if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
140
+ [false, send_current_tokens(auth_token)]
122
141
  else
123
- headers = {
124
- 'user_agent' => "#{DomoscioViz.user_agent}",
125
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
126
- 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
127
- 'Content-Type' => 'application/json'
128
- }
142
+ [true, request_new_tokens]
129
143
  end
130
- headers
144
+ rescue SyntaxError, StandardError
145
+ [true, request_new_tokens]
146
+ end
147
+
148
+ # If stored token successfully loaded we build the header with them
149
+ #
150
+ def self.send_current_tokens(auth_token)
151
+ {
152
+ 'user_agent' => DomoscioViz.user_agent,
153
+ 'ClientId' => DomoscioViz.configuration.client_id,
154
+ 'AccessToken' => auth_token[:access_token],
155
+ 'RefreshToken' => auth_token[:refresh_token],
156
+ 'Content-Type' => 'application/json'
157
+ }
131
158
  end
132
159
 
133
- DomoscioViz.configure do |c|
134
- c.preproduction = false
135
- c.client_id = nil
136
- c.client_passphrase = nil
137
- c.temp_dir = File.expand_path('../tmp', __FILE__)
138
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
160
+ # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
161
+ def self.request_new_tokens
162
+ {
163
+ 'user_agent' => DomoscioViz.user_agent,
164
+ 'ClientId' => DomoscioViz.configuration.client_id,
165
+ 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
166
+ 'Content-Type' => 'application/json'
167
+ }
139
168
  end
140
- end
169
+ end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domoscio_viz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benoit Praly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-03 00:00:00.000000000 Z
11
+ date: 2022-01-27 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 Viz Engine.
28
42
  email:
29
43
  - benoit.praly@domoscio.com
@@ -38,8 +52,6 @@ files:
38
52
  - lib/domoscio_viz/authorization_token.rb
39
53
  - lib/domoscio_viz/chart/chart.rb
40
54
  - lib/domoscio_viz/errors.rb
41
- - lib/domoscio_viz/generators/install_generator.rb
42
- - lib/domoscio_viz/generators/templates/install.rb
43
55
  - lib/domoscio_viz/http_calls.rb
44
56
  - lib/domoscio_viz/json.rb
45
57
  - lib/domoscio_viz/resource.rb
@@ -56,14 +68,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
68
  requirements:
57
69
  - - ">="
58
70
  - !ruby/object:Gem::Version
59
- version: '0'
71
+ version: 2.5.0
60
72
  required_rubygems_version: !ruby/object:Gem::Requirement
61
73
  requirements:
62
74
  - - ">="
63
75
  - !ruby/object:Gem::Version
64
76
  version: '0'
65
77
  requirements: []
66
- rubygems_version: 3.0.3
78
+ rubygems_version: 3.1.2
67
79
  signing_key:
68
80
  specification_version: 4
69
81
  summary: Summary of DomoscioViz.
@@ -1,10 +0,0 @@
1
- require 'rails/generators'
2
- module DomoscioViz
3
- class InstallGenerator < ::Rails::Generators::Base
4
- source_root File.expand_path('../templates', __FILE__)
5
- desc "Generate config file for DomoscioViz configuration"
6
- def install
7
- copy_file "install.rb", "config/initializers/domoscio_viz.rb"
8
- end
9
- end
10
- end
@@ -1,11 +0,0 @@
1
- DomoscioViz.configure do |c|
2
- if Rails.env == "production"
3
- c.preproduction = true
4
- else
5
- c.preproduction = false
6
- end
7
- c.client_id = ENV['DOMOSCIO_ID']
8
- c.client_passphrase = ENV['DOMOSCIO_PASSWORD']
9
- c.temp_dir = File.expand_path('../tmp', __FILE__)
10
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
11
- end