domoscio_viz 0.2.5a → 0.3.2

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: c21fbe0307ea47d6608df642b9a72d6a684d707002aad710369a1deeb6e8b150
4
- data.tar.gz: 2e8c0a44e2c64fb0bec2d7b3e38ed8feccd52183e34c17f0100db24d8a23253c
3
+ metadata.gz: 1e8a8a848fe4002d6eeac3f2580a37be7be066696a70c241e5e1ad0f57bd2d3d
4
+ data.tar.gz: 35dcfbf99c45d85371a4c3a5902f04806faed87727cc7c3a60319dfc43f82d6e
5
5
  SHA512:
6
- metadata.gz: 31c4a07d614f7228da16b172b166df19bb46559658e3d4b2e1facd74952ff5f610ac536eb411f1b8723104c6628bdf17f99af2966c4dc7ba63f8734e2a14cb75
7
- data.tar.gz: ea7ee09cad87f5c02af87783729b71e01c9bcb46ac45666902b89b360a1cf1806ccafccf5651a84e1864f5a40dd9f3ab8d34c072ea754d9e1018af4d25919ccd
6
+ metadata.gz: 6f1fe6e67f545784ccf5802c4d86e4952cc7f7e8d9a5094058ab2cfecd05f68bc5c67a08cdab6fb68e2353beec975405b04ba3ae20a782127a308e7b98aa5131
7
+ data.tar.gz: 7abba7516235a69835f540e970beec180216aad1bde9e71380c54f69cf3f190e60104147f06c570e7e87287c637376e5501358eb3c3d74d4ed11b8149cee6491
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,55 +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
10
+ def token
15
11
  storage.get
16
12
  end
17
13
  end
18
14
  end
19
15
 
20
- class StaticStorage
16
+ # Manages tokens with configuration
17
+ class ManualSetting
21
18
  def get
22
- @@token ||= nil
19
+ {
20
+ access_token: DomoscioViz.configuration.access_token,
21
+ refresh_token: DomoscioViz.configuration.refresh_token
22
+ }
23
23
  end
24
24
 
25
25
  def store(token)
26
- @@token = token
26
+ DomoscioViz.configuration.access_token = token[:access_token]
27
+ DomoscioViz.configuration.refresh_token = token[:refresh_token]
27
28
  end
28
29
  end
29
30
 
31
+ # Manages tokens with local File
30
32
  class FileStorage
31
33
  require 'yaml'
32
- @temp_dir
33
34
 
34
- def initialize(temp_dir = nil)
35
- @temp_dir = temp_dir || DomoscioViz.configuration.temp_dir
36
- raise "Path to temporary folder is not defined" unless @temp_dir
35
+ def initialize
36
+ raise 'Path to temporary folder is not defined' unless DomoscioViz.configuration.temp_dir
37
37
  end
38
38
 
39
39
  def get
40
- begin
41
- f = File.open(file_path, File::RDONLY)
42
- f.flock(File::LOCK_SH)
43
- txt = f.read
44
- f.close
45
- YAML.load(txt) || nil
46
- rescue Errno::ENOENT
47
- nil
48
- end
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
49
47
  end
50
48
 
51
49
  def store(token)
52
- File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
50
+ File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
53
51
  f.flock(File::LOCK_EX)
54
52
  f.truncate(0)
55
53
  f.rewind
@@ -58,8 +56,8 @@ module DomoscioViz
58
56
  end
59
57
 
60
58
  def file_path
61
- File.join(@temp_dir, "DomoscioViz.AuthorizationToken.FileStore.tmp")
59
+ File.join(DomoscioViz.configuration.temp_dir, 'DomoscioViz.AuthorizationToken.FileStore.tmp')
62
60
  end
63
61
  end
64
62
  end
65
- 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
- # ResponseError from VizEngine
5
+
6
+ # Error Message from VizEngine
8
7
  class ResponseError < Error
9
8
  attr_reader :request_url, :code, :details, :body, :request_params
9
+
10
10
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
11
- @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
12
16
  super(message) if message
13
17
  end
14
- def message; @details.is_a?(Hash) ? @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
27
+
20
28
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
21
- @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
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
@@ -6,9 +6,10 @@ module DomoscioViz
6
6
  DomoscioViz.request(:post, url(util_name), params)
7
7
  end
8
8
  end
9
+
9
10
  def self.included(base)
10
11
  base.extend(ClassMethods)
11
- end
12
+ end
12
13
  end
13
14
  end
14
- 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.5a"
3
- end
2
+ VERSION = '0.3.2'.freeze
3
+ end
data/lib/domoscio_viz.rb CHANGED
@@ -1,26 +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 :root_url, :client_id, :client_passphrase, :temp_dir
15
+ attr_accessor :client_id, :client_passphrase, :temp_dir,
16
+ :root_url, :file_storage, :access_token, :refresh_token
21
17
 
22
- def root_url
23
- @root_url ||= ""
18
+ def initialize
19
+ @root_url = ''
20
+ @file_storage ||= true
24
21
  end
25
22
  end
26
23
 
@@ -49,50 +46,50 @@ module DomoscioViz
49
46
  # Raises DomoscioViz::ProcessingError on Internal Error
50
47
  #
51
48
  def self.request(method, url, params={})
52
-
53
49
  store_tokens, headers = request_headers
54
50
  uri = api_uri(url)
55
-
56
51
  response = DomoscioViz.send_request(uri, method, params, headers)
57
- return response if response.kind_of? DomoscioViz::ProcessingError
52
+ return response if response.is_a? DomoscioViz::ProcessingError
58
53
 
59
54
  begin
60
55
  raise_http_failure(uri, response, params)
61
56
  data = DomoscioViz::JSON.load(response.body.nil? ? '' : response.body)
62
- DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']}) if store_tokens
63
- rescue MultiJson::LoadError => exception
64
- data = ProcessingError.new(uri, 500, exception, response.body, params)
65
- rescue ResponseError => exception
66
- data = exception
57
+ if store_tokens
58
+ DomoscioViz::AuthorizationToken::Manager.storage.store({
59
+ access_token: response['Accesstoken'],
60
+ refresh_token: response['Refreshtoken']
61
+ })
62
+ end
63
+ rescue MultiJson::LoadError => e
64
+ data = ProcessingError.new(uri, 500, e, response.body, params)
65
+ rescue ResponseError => e
66
+ data = e
67
67
  end
68
-
69
68
  data
70
69
  end
71
70
 
72
- private
73
-
74
71
  # This function catches usual Http errors during calls
75
72
  #
76
73
  def self.send_request(uri, method, params, headers)
77
- begin
78
- response = perform_call(uri, method, params, headers)
79
- response = retry_call_and_store_tokens(uri, method, params, headers) if ['401','403'].include? response.code
80
- response
81
- rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
82
- ProcessingError.new(uri, 500, exception, response, params)
83
- end
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)
84
80
  end
85
81
 
86
82
  # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
87
83
  #
88
84
  def self.raise_http_failure(uri, response, params)
89
- unless response.kind_of? Net::HTTPSuccess
90
- if response.blank?
91
- raise ResponseError.new(uri, 500, {error: {status: 500, message: 'VisualizationEngine not available'}}, {}, params)
92
- else
93
- raise ResponseError.new(uri, response.code.to_i, DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
94
- end
95
- end
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
+ )
96
93
  end
97
94
 
98
95
  # Actual HTTP call is performed here
@@ -107,26 +104,29 @@ module DomoscioViz
107
104
 
108
105
  # This method is called when AdaptiveEngine returns tokens errors
109
106
  # Action on those errors is to retry and request new tokens, those new token are then stored
110
- def self.retry_call_and_store_tokens(uri, method, params, headers)
107
+ def self.retry_call_and_store_tokens(uri, method, params)
111
108
  headers = request_new_tokens
112
109
  response = perform_call(uri, method, params, headers)
113
- DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']})
110
+ DomoscioViz::AuthorizationToken::Manager.storage.store({
111
+ access_token: response['Accesstoken'],
112
+ refresh_token: response['Refreshtoken']
113
+ })
114
114
  response
115
115
  end
116
116
 
117
117
  def self.user_agent
118
- @uname ||= get_uname
118
+ @uname ||= uname
119
119
  {
120
120
  bindings_version: DomoscioViz::VERSION,
121
121
  lang: 'ruby',
122
122
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
123
123
  platform: RUBY_PLATFORM,
124
124
  uname: @uname
125
- }
125
+ }.to_s
126
126
  end
127
127
 
128
- def self.get_uname
129
- `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
130
130
  rescue Errno::ENOMEM
131
131
  'uname lookup failed'
132
132
  end
@@ -135,26 +135,24 @@ module DomoscioViz
135
135
  # will return the processed headers and a token store flag
136
136
  #
137
137
  def self.request_headers
138
- begin
139
- auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
140
- if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
141
- [false, send_current_tokens(auth_token)]
142
- else
143
- [true, request_new_tokens]
144
- end
145
- rescue SyntaxError, StandardError
138
+ auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
139
+ if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
140
+ [false, send_current_tokens(auth_token)]
141
+ else
146
142
  [true, request_new_tokens]
147
143
  end
144
+ rescue SyntaxError, StandardError
145
+ [true, request_new_tokens]
148
146
  end
149
147
 
150
148
  # If stored token successfully loaded we build the header with them
151
149
  #
152
150
  def self.send_current_tokens(auth_token)
153
151
  {
154
- 'user_agent' => "#{DomoscioViz.user_agent}",
155
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
156
- 'AccessToken' => "#{auth_token[:access_token]}",
157
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
152
+ 'user_agent' => DomoscioViz.user_agent,
153
+ 'ClientId' => DomoscioViz.configuration.client_id,
154
+ 'AccessToken' => auth_token[:access_token],
155
+ 'RefreshToken' => auth_token[:refresh_token],
158
156
  'Content-Type' => 'application/json'
159
157
  }
160
158
  end
@@ -162,17 +160,10 @@ module DomoscioViz
162
160
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
163
161
  def self.request_new_tokens
164
162
  {
165
- 'user_agent' => "#{DomoscioViz.user_agent}",
166
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
163
+ 'user_agent' => DomoscioViz.user_agent,
164
+ 'ClientId' => DomoscioViz.configuration.client_id,
167
165
  'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
168
166
  'Content-Type' => 'application/json'
169
167
  }
170
168
  end
171
-
172
- DomoscioViz.configure do |c|
173
- c.client_id = nil
174
- c.client_passphrase = nil
175
- c.temp_dir = File.expand_path('../tmp', __FILE__)
176
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
177
- end
178
- 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.5a
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benoit Praly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-14 00:00:00.000000000 Z
11
+ date: 2022-08-22 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
- version: 1.3.1
76
+ version: '0'
65
77
  requirements: []
66
- rubygems_version: 3.0.8
78
+ rubygems_version: 3.2.22
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,6 +0,0 @@
1
- DomoscioViz.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