domoscio_viz 0.2.5 → 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: 86781580f4a835cfa5ac44fd6bc32933955f484fecbef42222957ea05239bb2e
4
- data.tar.gz: 58776787e717e2f6d6527c995a8d1b1865d5d4bc78a6067476b1dc049a9a39b5
3
+ metadata.gz: a27998a16cab08757bb639a589e3b6fac992c8edc215ddd1bef66244be05c983
4
+ data.tar.gz: 5574e4cde4e47c488626f6598fe1b2299221bb753416b8c388f8ff3cf190581d
5
5
  SHA512:
6
- metadata.gz: 4dbb5722a87cb87d27c40809144a88235c87f4ecae0923ba3b7d02274397ce15902fea2e58a09c40ca72603684b57c57c6ff1745b2267dec6b56eae1230cb17a
7
- data.tar.gz: 801a9125a3a74fc97c56cb83b01a2d531c5837e928469cdf120ac05d91e056f616725ede57265f73ffd939ffd001bac21e39f0281b6135e1134db766ef4d93e0
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,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.safe_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.5"
3
- end
2
+ VERSION = '0.3.1'.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,52 +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
- params.merge!({'per_page': 2000}) unless params[:per_page]
55
50
  uri = api_uri(url)
56
-
57
51
  response = DomoscioViz.send_request(uri, method, params, headers)
58
- return response if response.kind_of? DomoscioViz::ProcessingError
52
+ return response if response.is_a? DomoscioViz::ProcessingError
59
53
 
60
54
  begin
61
55
  raise_http_failure(uri, response, params)
62
56
  data = DomoscioViz::JSON.load(response.body.nil? ? '' : response.body)
63
- DomoscioViz::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
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
68
67
  end
69
-
70
68
  data
71
69
  end
72
70
 
73
- private
74
-
75
71
  # This function catches usual Http errors during calls
76
72
  #
77
73
  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
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)
85
80
  end
86
81
 
87
82
  # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
88
83
  #
89
84
  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: 'VisualizationEngine not available'}}, {}, params)
93
- else
94
- body = DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true)
95
- raise ResponseError.new(uri, response.code.to_i, body, response.body, params)
96
- end
97
- 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
+ )
98
93
  end
99
94
 
100
95
  # Actual HTTP call is performed here
@@ -109,26 +104,29 @@ module DomoscioViz
109
104
 
110
105
  # This method is called when AdaptiveEngine returns tokens errors
111
106
  # Action on those errors is to retry and request new tokens, those new token are then stored
112
- def self.retry_call_and_store_tokens(uri, method, params, headers)
107
+ def self.retry_call_and_store_tokens(uri, method, params)
113
108
  headers = request_new_tokens
114
109
  response = perform_call(uri, method, params, headers)
115
- 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
+ })
116
114
  response
117
115
  end
118
116
 
119
117
  def self.user_agent
120
- @uname ||= get_uname
118
+ @uname ||= uname
121
119
  {
122
120
  bindings_version: DomoscioViz::VERSION,
123
121
  lang: 'ruby',
124
122
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
125
123
  platform: RUBY_PLATFORM,
126
124
  uname: @uname
127
- }
125
+ }.to_s
128
126
  end
129
127
 
130
- def self.get_uname
131
- `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
132
130
  rescue Errno::ENOMEM
133
131
  'uname lookup failed'
134
132
  end
@@ -137,26 +135,24 @@ module DomoscioViz
137
135
  # will return the processed headers and a token store flag
138
136
  #
139
137
  def self.request_headers
140
- begin
141
- auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
142
- if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
143
- [false, send_current_tokens(auth_token)]
144
- else
145
- [true, request_new_tokens]
146
- end
147
- 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
148
142
  [true, request_new_tokens]
149
143
  end
144
+ rescue SyntaxError, StandardError
145
+ [true, request_new_tokens]
150
146
  end
151
147
 
152
148
  # If stored token successfully loaded we build the header with them
153
149
  #
154
150
  def self.send_current_tokens(auth_token)
155
151
  {
156
- 'user_agent' => "#{DomoscioViz.user_agent}",
157
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
158
- 'AccessToken' => "#{auth_token[:access_token]}",
159
- '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],
160
156
  'Content-Type' => 'application/json'
161
157
  }
162
158
  end
@@ -164,17 +160,10 @@ module DomoscioViz
164
160
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
165
161
  def self.request_new_tokens
166
162
  {
167
- 'user_agent' => "#{DomoscioViz.user_agent}",
168
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
163
+ 'user_agent' => DomoscioViz.user_agent,
164
+ 'ClientId' => DomoscioViz.configuration.client_id,
169
165
  'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
170
166
  'Content-Type' => 'application/json'
171
167
  }
172
168
  end
173
-
174
- DomoscioViz.configure do |c|
175
- c.client_id = nil
176
- c.client_passphrase = nil
177
- c.temp_dir = File.expand_path('../tmp', __FILE__)
178
- FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
179
- end
180
- 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.5
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: 2021-03-22 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.8
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,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