Module: DomoscioRails
- Defined in:
- lib/domoscio_rails.rb,
lib/domoscio_rails/json.rb,
lib/domoscio_rails/errors.rb,
lib/domoscio_rails/tag/tag.rb,
lib/domoscio_rails/version.rb,
lib/domoscio_rails/resource.rb,
lib/domoscio_rails/data/role.rb,
lib/domoscio_rails/data/user.rb,
lib/domoscio_rails/utils/lxp.rb,
lib/domoscio_rails/data/event.rb,
lib/domoscio_rails/data/scorm.rb,
lib/domoscio_rails/http_calls.rb,
lib/domoscio_rails/data/device.rb,
lib/domoscio_rails/tag/tag_set.rb,
lib/domoscio_rails/tag/tagging.rb,
lib/domoscio_rails/data/account.rb,
lib/domoscio_rails/data/content.rb,
lib/domoscio_rails/data/student.rb,
lib/domoscio_rails/tag/tag_edge.rb,
lib/domoscio_rails/utils/export.rb,
lib/domoscio_rails/data/question.rb,
lib/domoscio_rails/utils/kpi_log.rb,
lib/domoscio_rails/utils/analytic.rb,
lib/domoscio_rails/data/supervisor.rb,
lib/domoscio_rails/knowledge/scale.rb,
lib/domoscio_rails/utils/stats_util.rb,
lib/domoscio_rails/utils/review_util.rb,
lib/domoscio_rails/data/student_group.rb,
lib/domoscio_rails/authorization_token.rb,
lib/domoscio_rails/data/recommendation.rb,
lib/domoscio_rails/objective/objective.rb,
lib/domoscio_rails/utils/gameplay_util.rb,
lib/domoscio_rails/utils/export_request.rb,
lib/domoscio_rails/data/learning_session.rb,
lib/domoscio_rails/objective/subscription.rb,
lib/domoscio_rails/data/instance_parameter.rb,
lib/domoscio_rails/data/student_group_user.rb,
lib/domoscio_rails/knowledge/knowledge_edge.rb,
lib/domoscio_rails/knowledge/knowledge_node.rb,
lib/domoscio_rails/knowledge/knowledge_graph.rb,
lib/domoscio_rails/utils/recommendation_util.rb,
lib/domoscio_rails/objective/learning_program.rb,
lib/domoscio_rails/objective/objective_student.rb,
lib/domoscio_rails/knowledge/knowledge_graph_node.rb,
lib/domoscio_rails/knowledge/knowledge_graph_scale.rb,
lib/domoscio_rails/knowledge/knowledge_node_content.rb,
lib/domoscio_rails/knowledge/knowledge_node_student.rb,
lib/domoscio_rails/objective/learning_program_student.rb,
lib/domoscio_rails/objective/objective_knowledge_node.rb,
lib/domoscio_rails/data/learning_session_knowledge_node.rb,
lib/domoscio_rails/data/learning_session_recommendation.rb,
lib/domoscio_rails/objective/objective_knowledge_node_student.rb
Defined Under Namespace
Modules: AuthorizationToken, HTTPCalls, JSON, Resource Classes: Account, Analytic, Configuration, Content, Device, Error, Event, Export, ExportRequest, GameplayUtil, InstanceParameter, KnowledgeEdge, KnowledgeGraph, KnowledgeGraphNode, KnowledgeGraphScale, KnowledgeNode, KnowledgeNodeContent, KnowledgeNodeStudent, KpiLog, LearningProgram, LearningProgramStudent, LearningSession, LearningSessionKnowledgeNode, LearningSessionRecommendation, Lxp, Objective, ObjectiveKnowledgeNode, ObjectiveKnowledgeNodeStudent, ObjectiveStudent, ProcessingError, Question, Recommendation, RecommendationUtil, ResponseError, ReviewUtil, Role, Scale, Scorm, StatsUtil, Student, StudentGroup, StudentGroupUser, Subscription, Supervisor, Tag, TagEdge, TagSet, Tagging, User
Constant Summary collapse
- VERSION =
'0.4.33'.freeze
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
- .api_uri(url = '') ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.perform_call(uri, method, params, headers) ⇒ Object
Actual HTTP call is performed here.
-
.raise_http_failure(uri, response, params) ⇒ Object
This helper will check the response status and build the correcponding DomoscioRails::ResponseError.
-
.request(method, url, params = {}) ⇒ Object
-
method
: HTTP method; lowercase symbol, e.g.
-
-
.request_headers ⇒ Object
Process the token loading and analyze will return the processed headers and a token store flag.
-
.request_new_tokens ⇒ Object
If we cant find tokens of they are corrupted / expired, then we set headers to request new ones.
-
.retry_call_and_store_tokens(uri, method, params) ⇒ Object
This method is called when AdaptiveEngine returns tokens errors Action on those errors is to retry and request new tokens, those new token are then stored.
-
.send_current_tokens(auth_token) ⇒ Object
If stored token successfully loaded we build the header with them.
-
.send_request(uri, method, params, headers) ⇒ Object
This function catches usual Http errors during callsheaders.
- .uname ⇒ Object
- .user_agent ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
72 73 74 |
# File 'lib/domoscio_rails.rb', line 72 def configuration @configuration end |
Class Method Details
.api_uri(url = '') ⇒ Object
80 81 82 |
# File 'lib/domoscio_rails.rb', line 80 def self.api_uri(url = '') URI(configuration.root_url + url) end |
.configure {|configuration| ... } ⇒ Object
75 76 77 78 |
# File 'lib/domoscio_rails.rb', line 75 def self.configure self.configuration ||= Configuration.new yield configuration end |
.perform_call(uri, method, params, headers) ⇒ Object
Actual HTTP call is performed here
179 180 181 182 183 184 185 |
# File 'lib/domoscio_rails.rb', line 179 def self.perform_call(uri, method, params, headers) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| req = Net::HTTP.const_get(method.capitalize).new(uri.request_uri, headers) req.body = DomoscioRails::JSON.dump(params) http.request req end end |
.raise_http_failure(uri, response, params) ⇒ Object
This helper will check the response status and build the correcponding DomoscioRails::ResponseError
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/domoscio_rails.rb', line 166 def self.raise_http_failure(uri, response, params) return if response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPNoContent) raise ResponseError.new( uri, response.code.to_i, DomoscioRails::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true), response.body, params ) end |
.request(method, url, params = {}) ⇒ Object
-
method
: HTTP method; lowercase symbol, e.g. :get, :post etc. -
url
: the part after Configuration#root_url -
params
: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
Performs HTTP requests to Adaptive Engine On token issues, will try once to get a new token then will output a DomoscioRails::ReponseError with details
Raises DomoscioRails::ResponseError on Adaptive Error Status Raises DomoscioRails::ProcessingError on Internal Error
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/domoscio_rails.rb', line 95 def self.request(method, url, params = {}) params ||= {} store_tokens, headers = request_headers params.merge!({ 'per_page': 1000 }) unless params[:per_page] uri = api_uri(url) response = DomoscioRails.send_request(uri, method, params, headers) return response if response.is_a? DomoscioRails::ProcessingError begin raise_http_failure(uri, response, params) if response['Content-Type'] == 'application/zip' || response['Content-Type'] == 'application/xlsx' data = response elsif response.is_a?(Net::HTTPNoContent) data = [] else data = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body) end if store_tokens DomoscioRails::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) end rescue MultiJson::LoadError => e data = ProcessingError.new(uri, 500, e, response.body, params) rescue ResponseError => e data = e end if response['Total'] || response['Total-Pages'] != '1' pagetotal = if response['Total'] (response['Total'].to_i / response['Per-Page'].to_f).ceil else response['Total-Pages'].to_i end (2..pagetotal).each do |j| response = DomoscioRails.send_request(uri, method, params.merge({ page: j }), headers) return response if response.is_a? DomoscioRails::ProcessingError begin raise_http_failure(uri, response, params) body = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body) data += body data.flatten! rescue MultiJson::LoadError => e return ProcessingError.new(uri, 500, e, response.body, params) rescue ResponseError => e return e end end end data end |
.request_headers ⇒ Object
Process the token loading and analyze will return the processed headers and a token store flag
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/domoscio_rails.rb', line 219 def self.request_headers auth_token = DomoscioRails::AuthorizationToken::Manager.token if auth_token && auth_token[:access_token] && auth_token[:refresh_token] [false, send_current_tokens(auth_token)] else [true, request_new_tokens] end rescue SyntaxError, StandardError [true, request_new_tokens] end |
.request_new_tokens ⇒ Object
If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
242 243 244 245 246 247 248 |
# File 'lib/domoscio_rails.rb', line 242 def self.request_new_tokens { 'user_agent' => DomoscioRails.user_agent.to_s, 'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}", 'Content-Type' => 'application/json' } end |
.retry_call_and_store_tokens(uri, method, params) ⇒ Object
This method is called when AdaptiveEngine returns tokens errors Action on those errors is to retry and request new tokens, those new token are then stored
189 190 191 192 193 194 195 196 197 |
# File 'lib/domoscio_rails.rb', line 189 def self.retry_call_and_store_tokens(uri, method, params) headers = request_new_tokens response = perform_call(uri, method, params, headers) DomoscioRails::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) response end |
.send_current_tokens(auth_token) ⇒ Object
If stored token successfully loaded we build the header with them
232 233 234 235 236 237 238 239 |
# File 'lib/domoscio_rails.rb', line 232 def self.send_current_tokens(auth_token) { 'user_agent' => DomoscioRails.user_agent.to_s, 'AccessToken' => auth_token[:access_token].to_s, 'RefreshToken' => auth_token[:refresh_token].to_s, 'Content-Type' => 'application/json' } end |
.send_request(uri, method, params, headers) ⇒ Object
This function catches usual Http errors during callsheaders
155 156 157 158 159 160 161 162 |
# File 'lib/domoscio_rails.rb', line 155 def self.send_request(uri, method, params, headers) response = perform_call(uri, method, params, headers) response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code response rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e ProcessingError.new(uri, 500, e, response) end |
.uname ⇒ Object
210 211 212 213 214 |
# File 'lib/domoscio_rails.rb', line 210 def self.uname `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i rescue Errno::ENOMEM 'uname lookup failed' end |
.user_agent ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/domoscio_rails.rb', line 199 def self.user_agent @uname ||= uname { bindings_version: DomoscioRails::VERSION, lang: 'ruby', lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})", platform: RUBY_PLATFORM, uname: @uname }.to_s end |