domoscio_rails 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -4
  3. data/lib/domoscio_rails/authorization_token.rb +24 -22
  4. data/lib/domoscio_rails/data/content.rb +1 -3
  5. data/lib/domoscio_rails/data/event.rb +1 -3
  6. data/lib/domoscio_rails/data/instance.rb +0 -2
  7. data/lib/domoscio_rails/data/learning_session.rb +1 -3
  8. data/lib/domoscio_rails/data/recommendation.rb +1 -3
  9. data/lib/domoscio_rails/data/student.rb +1 -3
  10. data/lib/domoscio_rails/errors.rb +21 -5
  11. data/lib/domoscio_rails/http_calls.rb +3 -0
  12. data/lib/domoscio_rails/json.rb +1 -1
  13. data/lib/domoscio_rails/knowledge/knowledge_edge.rb +1 -3
  14. data/lib/domoscio_rails/knowledge/knowledge_graph.rb +1 -3
  15. data/lib/domoscio_rails/knowledge/knowledge_node.rb +1 -3
  16. data/lib/domoscio_rails/knowledge/knowledge_node_content.rb +1 -3
  17. data/lib/domoscio_rails/knowledge/knowledge_node_student.rb +1 -3
  18. data/lib/domoscio_rails/objective/objective.rb +1 -3
  19. data/lib/domoscio_rails/objective/objective_knowledge_node.rb +1 -3
  20. data/lib/domoscio_rails/objective/objective_knowledge_node_student.rb +1 -3
  21. data/lib/domoscio_rails/objective/objective_student.rb +0 -2
  22. data/lib/domoscio_rails/resource.rb +18 -14
  23. data/lib/domoscio_rails/tag/tag.rb +1 -3
  24. data/lib/domoscio_rails/tag/tag_edge.rb +1 -3
  25. data/lib/domoscio_rails/tag/tag_set.rb +1 -3
  26. data/lib/domoscio_rails/tag/tagging.rb +1 -3
  27. data/lib/domoscio_rails/utils/gameplay_util.rb +1 -3
  28. data/lib/domoscio_rails/utils/recommendation_util.rb +1 -3
  29. data/lib/domoscio_rails/utils/review_util.rb +1 -3
  30. data/lib/domoscio_rails/version.rb +2 -2
  31. data/lib/domoscio_rails.rb +86 -85
  32. metadata +24 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6072b4a46cd9e2cf0daba6e73b4192ed603d12a9bc6139e3b90bc48fd04131d
4
- data.tar.gz: c7c1e0dc7b0783625e102f62359639be583632594d385eec73f9f8232716345e
3
+ metadata.gz: 3018e6b4f3733f3b33ebb28fab7eccc020ea4909ff08c167fde24eb757890c14
4
+ data.tar.gz: 9216994f5101318046ca16eb8aa2dbaf6a3b1c6d9de533748bf18a61e6001c3d
5
5
  SHA512:
6
- metadata.gz: ba71395ea5360713fd55335771184004d6268662dca37837f7918429ca23a036499d7c207c90b48b8b1efdf38ec666c48011409420766741b30583f10a5edb00
7
- data.tar.gz: 4c8452c0a8358fecfadfa9a45fd2ad5f7cbccfaef9c693b7a563e70b90a30f98cb73b3c535535e65b07f9cfb28eac4760f03bce5ef55068b81decf12bc6d8875
6
+ metadata.gz: '08018205d6a5123cb4b1e768c2960f3fc58c8679d657c114b2ef15af06386cbf2a46c21e32c110c0680042eba72c4a49aa9b32c12555093d44d29a33e5f81848'
7
+ data.tar.gz: fff59acde37fe7c518cfb0da170b902c7878c52a2ac07a4f6f9c2f8e5c82611c51738490b9a4a5c0a1f85b277b6755effe6e8e7f11573322e3526baad6f0c857
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,51 +1,53 @@
1
1
  module DomoscioRails
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 ||= DomoscioRails.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
- class StaticStorage
15
+
16
+ # Manages tokens with configuration
17
+ class ManualSetting
19
18
  def get
20
- @@token ||= nil
19
+ {
20
+ access_token: DomoscioRails.configuration.access_token,
21
+ refresh_token: DomoscioRails.configuration.refresh_token
22
+ }
21
23
  end
22
24
 
23
25
  def store(token)
24
- @@token = token
26
+ DomoscioRails.configuration.access_token = token[:access_token]
27
+ DomoscioRails.configuration.refresh_token = token[:refresh_token]
25
28
  end
26
29
  end
27
30
 
31
+ # Manages tokens with local File
28
32
  class FileStorage
29
33
  require 'yaml'
30
34
 
31
35
  def initialize
32
- raise "Path to temporary folder is not defined" unless DomoscioRails.configuration.temp_dir
36
+ raise 'Path to temporary folder is not defined' unless DomoscioRails.configuration.temp_dir
33
37
  end
34
38
 
35
39
  def get
36
- begin
37
- f = File.open(file_path, File::RDONLY)
38
- f.flock(File::LOCK_SH)
39
- txt = f.read
40
- f.close
41
- YAML.load(txt) || nil
42
- rescue Errno::ENOENT
43
- nil
44
- 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
45
47
  end
46
48
 
47
49
  def store(token)
48
- File.open(file_path, File::RDWR|File::CREAT, 0644) do |f|
50
+ File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
49
51
  f.flock(File::LOCK_EX)
50
52
  f.truncate(0)
51
53
  f.rewind
@@ -54,8 +56,8 @@ module DomoscioRails
54
56
  end
55
57
 
56
58
  def file_path
57
- File.join(DomoscioRails.configuration.temp_dir, "DomoscioRails.AuthorizationToken.FileStore.tmp")
59
+ File.join(DomoscioRails.configuration.temp_dir, 'DomoscioRails.AuthorizationToken.FileStore.tmp')
58
60
  end
59
61
  end
60
62
  end
61
- end
63
+ end
@@ -1,11 +1,9 @@
1
1
  module DomoscioRails
2
2
  class Content < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Update
7
6
  include DomoscioRails::HTTPCalls::Destroy
8
7
  include DomoscioRails::HTTPCalls::Util
9
-
10
8
  end
11
- end
9
+ end
@@ -1,9 +1,7 @@
1
1
  module DomoscioRails
2
2
  class Event < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
-
8
6
  end
9
- end
7
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class Instance < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::UpdateSelf
7
6
  include DomoscioRails::HTTPCalls::Destroy
8
-
9
7
  end
10
8
  end
@@ -1,11 +1,9 @@
1
1
  module DomoscioRails
2
2
  class LearningSession < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
7
  include DomoscioRails::HTTPCalls::Util
9
-
10
8
  end
11
- end
9
+ end
@@ -1,7 +1,5 @@
1
1
  module DomoscioRails
2
2
  class Recommendation < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Fetch
5
-
6
4
  end
7
- end
5
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class Student < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -6,20 +6,36 @@ module DomoscioRails
6
6
  # Error Message from AdaptiveEngine
7
7
  class ResponseError < Error
8
8
  attr_reader :request_url, :code, :details, :body, :request_params
9
+
9
10
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
10
- @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
11
16
  super(message) if message
12
17
  end
13
- def message; (@details.is_a?(Hash) && @details[:error].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
14
22
  end
15
23
 
16
24
  # ProcessingError from DomoscioRails
17
25
  class ProcessingError < Error
18
26
  attr_reader :request_url, :code, :details, :body, :request_params
27
+
19
28
  def initialize(request_url, code, details = {}, body = nil, request_params = {})
20
- @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
21
34
  super(message) if message
22
35
  end
23
- def message; @details.message; end
36
+
37
+ def message
38
+ @details.message
39
+ end
24
40
  end
25
- end
41
+ end
@@ -8,6 +8,7 @@ module DomoscioRails
8
8
  DomoscioRails.request(:post, url(id), params)
9
9
  end
10
10
  end
11
+
11
12
  def self.included(base)
12
13
  base.extend(ClassMethods)
13
14
  end
@@ -19,6 +20,7 @@ module DomoscioRails
19
20
  DomoscioRails.request(:put, url(id), params)
20
21
  end
21
22
  end
23
+
22
24
  def self.included(base)
23
25
  base.extend(ClassMethods)
24
26
  end
@@ -64,6 +66,7 @@ module DomoscioRails
64
66
  def util(id = nil, util_name = nil, params = {})
65
67
  DomoscioRails.request(:get, url(id, util_name), params)
66
68
  end
69
+
67
70
  def util_post(id = nil, util_name = nil, params = {})
68
71
  DomoscioRails.request(:post, url(id, util_name), params)
69
72
  end
@@ -20,4 +20,4 @@ module DomoscioRails
20
20
  end
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class KnowledgeEdge < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class KnowledgeGraph < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class KnowledgeNode < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class KnowledgeNodeContent < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class KnowledgeNodeStudent < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class Objective < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class ObjectiveKnowledgeNode < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class ObjectiveKnowledgeNodeStudent < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,11 +1,9 @@
1
1
  module DomoscioRails
2
2
  class ObjectiveStudent < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
7
  include DomoscioRails::HTTPCalls::Util
9
-
10
8
  end
11
9
  end
@@ -4,22 +4,26 @@ module DomoscioRails
4
4
  def class_name
5
5
  name.split('::')[-1]
6
6
  end
7
- def url(id = nil, util_name = nil, on_self = nil )
8
- if self == Resource
9
- raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
10
- end
7
+
8
+ def url(id = nil, util_name = nil, on_self = nil)
9
+ raise NotImplementedError, 'Resource is an abstract class. Do not use it directly.' if self == Resource
10
+
11
11
  build_url = "/v#{DomoscioRails.configuration.version}/instances/#{DomoscioRails.configuration.client_id}"
12
- if !on_self
13
- build_url << "/#{class_name.underscore}s"
14
- if util_name
15
- build_url << "/#{util_name}"
16
- end
17
- if id
18
- build_url << "/#{CGI.escape(id.to_s)}"
19
- end
12
+ unless on_self
13
+ build_url << "/#{underscore(class_name)}s"
14
+ build_url << "/#{util_name}" if util_name
15
+ build_url << "/#{CGI.escape(id.to_s)}" if id
20
16
  end
21
- return build_url
17
+ build_url
18
+ end
19
+
20
+ def underscore(string)
21
+ string.gsub(/::/, '/')
22
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
23
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
24
+ .tr('-', '_')
25
+ .downcase
22
26
  end
23
27
  end
24
28
  end
25
- end
29
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class Tag < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class TagEdge < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class TagSet < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,10 +1,8 @@
1
1
  module DomoscioRails
2
2
  class Tagging < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Create
5
4
  include DomoscioRails::HTTPCalls::Fetch
6
5
  include DomoscioRails::HTTPCalls::Destroy
7
6
  include DomoscioRails::HTTPCalls::Update
8
-
9
7
  end
10
- end
8
+ end
@@ -1,7 +1,5 @@
1
1
  module DomoscioRails
2
2
  class GameplayUtil < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Util
5
-
6
4
  end
7
- end
5
+ end
@@ -1,7 +1,5 @@
1
1
  module DomoscioRails
2
2
  class RecommendationUtil < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Util
5
-
6
4
  end
7
- end
5
+ end
@@ -1,7 +1,5 @@
1
1
  module DomoscioRails
2
2
  class ReviewUtil < Resource
3
-
4
3
  include DomoscioRails::HTTPCalls::Util
5
-
6
4
  end
7
- end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module DomoscioRails
2
- VERSION = "0.3.9"
3
- end
2
+ VERSION = '0.4.0'.freeze
3
+ end
@@ -1,4 +1,3 @@
1
-
2
1
  require 'net/https'
3
2
  require 'cgi/util'
4
3
  require 'multi_json'
@@ -10,40 +9,39 @@ require 'domoscio_rails/authorization_token'
10
9
  # resources
11
10
  require 'domoscio_rails/http_calls'
12
11
  require 'domoscio_rails/resource'
13
- require 'domoscio_rails/data/content.rb'
14
- require 'domoscio_rails/data/event.rb'
15
- require 'domoscio_rails/data/instance.rb'
16
- require 'domoscio_rails/data/recommendation.rb'
17
- require 'domoscio_rails/data/learning_session.rb'
18
- require 'domoscio_rails/data/student.rb'
19
- require 'domoscio_rails/knowledge/knowledge_edge.rb'
20
- require 'domoscio_rails/knowledge/knowledge_graph.rb'
21
- require 'domoscio_rails/knowledge/knowledge_node_content.rb'
22
- require 'domoscio_rails/knowledge/knowledge_node_student.rb'
23
- require 'domoscio_rails/knowledge/knowledge_node.rb'
24
- require 'domoscio_rails/objective/objective_knowledge_node_student.rb'
25
- require 'domoscio_rails/objective/objective_knowledge_node.rb'
26
- require 'domoscio_rails/objective/objective_student.rb'
27
- require 'domoscio_rails/objective/objective.rb'
28
- require 'domoscio_rails/tag/tag_edge.rb'
29
- require 'domoscio_rails/tag/tag_set.rb'
30
- require 'domoscio_rails/tag/tag.rb'
31
- require 'domoscio_rails/tag/tagging.rb'
32
- require 'domoscio_rails/utils/gameplay_util.rb'
33
- require 'domoscio_rails/utils/recommendation_util.rb'
34
- require 'domoscio_rails/utils/review_util.rb'
12
+ require 'domoscio_rails/data/content'
13
+ require 'domoscio_rails/data/event'
14
+ require 'domoscio_rails/data/instance'
15
+ require 'domoscio_rails/data/recommendation'
16
+ require 'domoscio_rails/data/learning_session'
17
+ require 'domoscio_rails/data/student'
18
+ require 'domoscio_rails/knowledge/knowledge_edge'
19
+ require 'domoscio_rails/knowledge/knowledge_graph'
20
+ require 'domoscio_rails/knowledge/knowledge_node_content'
21
+ require 'domoscio_rails/knowledge/knowledge_node_student'
22
+ require 'domoscio_rails/knowledge/knowledge_node'
23
+ require 'domoscio_rails/objective/objective_knowledge_node_student'
24
+ require 'domoscio_rails/objective/objective_knowledge_node'
25
+ require 'domoscio_rails/objective/objective_student'
26
+ require 'domoscio_rails/objective/objective'
27
+ require 'domoscio_rails/tag/tag_edge'
28
+ require 'domoscio_rails/tag/tag_set'
29
+ require 'domoscio_rails/tag/tag'
30
+ require 'domoscio_rails/tag/tagging'
31
+ require 'domoscio_rails/utils/gameplay_util'
32
+ require 'domoscio_rails/utils/recommendation_util'
33
+ require 'domoscio_rails/utils/review_util'
35
34
 
36
35
  module DomoscioRails
36
+ # Configurable attributes and default values
37
37
  class Configuration
38
- attr_accessor :root_url, :client_id, :client_passphrase, :temp_dir, :version
39
-
40
- # Refers to AdaptiveEngine Version
41
- def version
42
- @version ||= 2
43
- end
38
+ attr_accessor :client_id, :client_passphrase, :temp_dir, :version,
39
+ :root_url, :file_storage, :access_token, :refresh_token
44
40
 
45
- def root_url
46
- @root_url ||= ""
41
+ def initialize
42
+ @version = 2
43
+ @root_url = ''
44
+ @file_storage ||= true
47
45
  end
48
46
  end
49
47
 
@@ -56,7 +54,7 @@ module DomoscioRails
56
54
  yield configuration
57
55
  end
58
56
 
59
- def self.api_uri(url='')
57
+ def self.api_uri(url = '')
60
58
  URI(configuration.root_url + url)
61
59
  end
62
60
 
@@ -72,75 +70,77 @@ module DomoscioRails
72
70
  # Raises DomoscioRails::ProcessingError on Internal Error
73
71
  #
74
72
  def self.request(method, url, params={})
75
-
76
73
  store_tokens, headers = request_headers
77
- params.merge!({'per_page': 10000}) unless params[:per_page]
74
+ params.merge!({ 'per_page': 1000 }) unless params[:per_page]
78
75
  uri = api_uri(url)
79
-
80
76
  response = DomoscioRails.send_request(uri, method, params, headers)
81
- return response if response.kind_of? DomoscioRails::ProcessingError
77
+ return response if response.is_a? DomoscioRails::ProcessingError
82
78
 
83
79
  begin
84
80
  raise_http_failure(uri, response, params)
85
81
  data = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body)
86
- DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']}) if store_tokens
87
- rescue MultiJson::LoadError => exception
88
- data = ProcessingError.new(uri, 500, exception, response.body, params)
89
- rescue ResponseError => exception
90
- data = exception
82
+ if store_tokens
83
+ DomoscioRails::AuthorizationToken::Manager.storage.store({
84
+ access_token: response['Accesstoken'],
85
+ refresh_token: response['Refreshtoken']
86
+ })
87
+ end
88
+ rescue MultiJson::LoadError => e
89
+ data = ProcessingError.new(uri, 500, e, response.body, params)
90
+ rescue ResponseError => e
91
+ data = e
91
92
  end
92
93
 
93
94
  if response['Total']
94
95
  pagetotal = (response['Total'].to_i / response['Per-Page'].to_f).ceil
95
- for j in 2..pagetotal
96
- response = DomoscioRails.send_request(uri, method, params.merge({page: j}), headers)
97
- return response if response.kind_of? DomoscioRails::ProcessingError
96
+ (2..pagetotal).each do |j|
97
+ response = DomoscioRails.send_request(uri, method, params.merge({ page: j }), headers)
98
+ return response if response.is_a? DomoscioRails::ProcessingError
99
+
98
100
  begin
99
101
  raise_http_failure(uri, response, params)
100
102
  body = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body)
101
103
  data += body
102
104
  data.flatten!
103
- rescue MultiJson::LoadError => exception
104
- return ProcessingError.new(uri, 500, exception, response.body, params)
105
- rescue ResponseError => exception
106
- return exception
105
+ rescue MultiJson::LoadError => e
106
+ return ProcessingError.new(uri, 500, e, response.body, params)
107
+ rescue ResponseError => e
108
+ return e
107
109
  end
108
110
  end
109
111
  end
110
112
  data
111
113
  end
112
114
 
113
- private
114
-
115
- # This function catches usual Http errors during calls
115
+ # This function catches usual Http errors during callsheaders
116
116
  #
117
117
  def self.send_request(uri, method, params, headers)
118
- begin
119
- response = perform_call(uri, method, params, headers)
120
- response = retry_call_and_store_tokens(uri, method, params, headers) if ['401','403'].include? response.code
121
- response
122
- rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
123
- ProcessingError.new(uri, 500, exception, response)
124
- end
118
+ response = perform_call(uri, method, params, headers)
119
+ response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code
120
+ response
121
+ rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET,
122
+ EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
123
+ ProcessingError.new(uri, 500, e, response)
125
124
  end
126
125
 
127
126
  # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
128
127
  #
129
128
  def self.raise_http_failure(uri, response, params)
130
- unless response.kind_of? Net::HTTPSuccess
131
- if response.blank?
132
- raise ResponseError.new(uri, 500, {error: {status: 500, message: 'AdaptiveEngine not available'}}, {}, params)
133
- else
134
- raise ResponseError.new(uri, response.code.to_i, DomoscioRails::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
135
- end
136
- end
129
+ return if response.is_a? Net::HTTPSuccess
130
+
131
+ raise ResponseError.new(
132
+ uri,
133
+ response.code.to_i,
134
+ DomoscioRails::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true),
135
+ response.body, params
136
+ )
137
137
  end
138
138
 
139
139
  # Actual HTTP call is performed here
140
140
  #
141
141
  def self.perform_call(uri, method, params, headers)
142
142
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
143
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
143
+ req = Net::HTTP.const_get(method.capitalize).new(uri.request_uri, headers)
144
144
  req.body = DomoscioRails::JSON.dump(params)
145
145
  http.request req
146
146
  end
@@ -148,26 +148,29 @@ module DomoscioRails
148
148
 
149
149
  # This method is called when AdaptiveEngine returns tokens errors
150
150
  # Action on those errors is to retry and request new tokens, those new token are then stored
151
- def self.retry_call_and_store_tokens(uri, method, params, headers)
151
+ def self.retry_call_and_store_tokens(uri, method, params)
152
152
  headers = request_new_tokens
153
153
  response = perform_call(uri, method, params, headers)
154
- DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']})
154
+ DomoscioRails::AuthorizationToken::Manager.storage.store({
155
+ access_token: response['Accesstoken'],
156
+ refresh_token: response['Refreshtoken']
157
+ })
155
158
  response
156
159
  end
157
160
 
158
161
  def self.user_agent
159
- @uname ||= get_uname
162
+ @uname ||= uname
160
163
  {
161
164
  bindings_version: DomoscioRails::VERSION,
162
165
  lang: 'ruby',
163
166
  lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})",
164
167
  platform: RUBY_PLATFORM,
165
168
  uname: @uname
166
- }
169
+ }.to_s
167
170
  end
168
171
 
169
- def self.get_uname
170
- `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i
172
+ def self.uname
173
+ `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i
171
174
  rescue Errno::ENOMEM
172
175
  'uname lookup failed'
173
176
  end
@@ -176,25 +179,23 @@ module DomoscioRails
176
179
  # will return the processed headers and a token store flag
177
180
  #
178
181
  def self.request_headers
179
- begin
180
- auth_token = DomoscioRails::AuthorizationToken::Manager.get_token
181
- if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
182
- [false, send_current_tokens(auth_token)]
183
- else
184
- [true, request_new_tokens]
185
- end
186
- rescue SyntaxError, StandardError
182
+ auth_token = DomoscioRails::AuthorizationToken::Manager.token
183
+ if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
184
+ [false, send_current_tokens(auth_token)]
185
+ else
187
186
  [true, request_new_tokens]
188
187
  end
188
+ rescue SyntaxError, StandardError
189
+ [true, request_new_tokens]
189
190
  end
190
191
 
191
192
  # If stored token successfully loaded we build the header with them
192
193
  #
193
194
  def self.send_current_tokens(auth_token)
194
195
  {
195
- 'user_agent' => "#{DomoscioRails.user_agent}",
196
- 'AccessToken' => "#{auth_token[:access_token]}",
197
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
196
+ 'user_agent' => DomoscioRails.user_agent.to_s,
197
+ 'AccessToken' => auth_token[:access_token].to_s,
198
+ 'RefreshToken' => auth_token[:refresh_token].to_s,
198
199
  'Content-Type' => 'application/json'
199
200
  }
200
201
  end
@@ -202,9 +203,9 @@ module DomoscioRails
202
203
  # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
203
204
  def self.request_new_tokens
204
205
  {
205
- 'user_agent' => "#{DomoscioRails.user_agent}",
206
+ 'user_agent' => DomoscioRails.user_agent.to_s,
206
207
  'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",
207
208
  'Content-Type' => 'application/json'
208
209
  }
209
210
  end
210
- end
211
+ end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domoscio_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.4.0
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-12-14 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 API.
28
42
  email:
29
43
  - benoit.praly@domoscio.com
@@ -76,15 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
90
  requirements:
77
91
  - - ">="
78
92
  - !ruby/object:Gem::Version
79
- version: '0'
93
+ version: 2.5.0
80
94
  required_rubygems_version: !ruby/object:Gem::Requirement
81
95
  requirements:
82
96
  - - ">="
83
97
  - !ruby/object:Gem::Version
84
98
  version: '0'
85
99
  requirements: []
86
- rubygems_version: 3.2.22
100
+ rubygems_version: 3.1.2
87
101
  signing_key:
88
102
  specification_version: 4
89
- summary: Summary of DomoscioRails.
103
+ summary: Summary of DomoscioRailspec.
90
104
  test_files: []