lib/domoscio_rails.rb in domoscio_rails-0.2.14 vs lib/domoscio_rails.rb in domoscio_rails-0.2.16.9

- old
+ new

@@ -1,7 +1,7 @@ - -require 'net/http' + +require 'net/https' require 'cgi/util' require 'multi_json' # helpers require 'domoscio_rails/version' @@ -16,17 +16,16 @@ require 'domoscio_rails/adaptative/deterministic/rule_input' require 'domoscio_rails/adaptative/deterministic/rule_output' require 'domoscio_rails/adaptative/deterministic/rule_condition' require 'domoscio_rails/adaptative/predictive/objective' require 'domoscio_rails/adaptative/predictive/objective_student' -require 'domoscio_rails/adaptative/predictive/objective_knowledge_node_student' require 'domoscio_rails/adaptative/predictive/objective_knowledge_node' +require 'domoscio_rails/adaptative/predictive/objective_knowledge_node_student' require 'domoscio_rails/adaptative/recommendation' require 'domoscio_rails/path/learning_path' require 'domoscio_rails/content/content' require 'domoscio_rails/content/knowledge_node_content' -require 'domoscio_rails/admin/user' require 'domoscio_rails/student/student' require 'domoscio_rails/student/student_cluster' require 'domoscio_rails/knowledge/knowledge_graph' require 'domoscio_rails/knowledge/knowledge_edge' require 'domoscio_rails/knowledge/knowledge_node' @@ -36,33 +35,47 @@ require 'domoscio_rails/metadata/tag_edge' require 'domoscio_rails/data/knowledge_node_student' require 'domoscio_rails/data/event' require 'domoscio_rails/utils/review_util' require 'domoscio_rails/utils/gameplay_util' +require 'domoscio_rails/utils/alerts_util' +require 'domoscio_rails/utils/recommendation_util' +require 'domoscio_rails/metadata/delta_object' - module DomoscioRails class Configuration - attr_accessor :preproduction, :root_url, + attr_accessor :preproduction, :test, :root_url, :client_id, :client_passphrase, :temp_dir, :disabled, :version - + def disabled @disabled || false end def preproduction @preproduction || false end - + + def test + @test || false + end + def version @version || 1 end def root_url - @root_url || (@preproduction == true ? ( @version > 1 ? "http://api.domoscio.com" : "http://stats-engine-api.domoscio.com" ) : "http://localhost:3001/") + if @preproduction == true + if @test == true + @root_url || "https://domoscio-adaptive-engine-staging.azurewebsites.net" + else + @root_url || "https://domoscio-adaptive-engine.azurewebsites.net" + end + else + @root_url || "http://localhost:3001" + end end end class << self attr_accessor :configuration @@ -88,26 +101,42 @@ # Raises DomoscioRails::ResponseError if response code != 200. # def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil) return false if @disabled uri = api_uri(url) - uri.query = URI.encode_www_form(filters) unless filters.empty? - - res = Net::HTTP.start(uri.host, uri.port) do |http| # , 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) - before_request_proc.call(req) if before_request_proc - http.request req - end + uri.query = URI.encode_www_form(filters) unless filters.empty? + res = DomoscioRails.send_request(uri, method, params, headers, before_request_proc) + # decode json data begin data = DomoscioRails::JSON.load(res.body.nil? ? '' : res.body) + unless (res.kind_of? Net::HTTPClientError) || (res.kind_of? Net::HTTPServerError) + DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: res['Accesstoken'], refresh_token: res['Refreshtoken']}) + end rescue MultiJson::LoadError data = {} end + if res['Total'] && !filters[:page] + pagetotal = (res['Total'].to_i / res['Per-Page'].to_f).ceil + + for j in 2..pagetotal + params = params.merge({page: j}) + res = DomoscioRails.send_request(uri, method, params, headers, before_request_proc) + + # decode json data + begin + data += DomoscioRails::JSON.load(res.body.nil? ? '' : res.body) + data.flatten! + rescue MultiJson::LoadError + data = {} + end + + end + end + ############### TEMP!!!! ####################################################### #pp method, uri.request_uri, params #, filters, headers #pp res, data #puts @@ -124,10 +153,20 @@ # } data end + + def self.send_request(uri, method, params, headers, before_request_proc) + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| # , 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) + before_request_proc.call(req) if before_request_proc + http.request req + end + end + private def self.user_agent @uname ||= get_uname @@ -146,19 +185,26 @@ 'uname lookup failed' end def self.request_headers auth_token = DomoscioRails::AuthorizationToken::Manager.get_token - headers = { - 'user_agent' => "DomoscioRails V2 RubyBindings/#{DomoscioRails::VERSION}", - 'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",#"#{auth_token['token_type']} #{auth_token['access_token']}", - 'Content-Type' => 'application/json' - } - # begin -# headers.update('x_mangopay_client_user_agent' => DomoscioRails::JSON.dump(user_agent)) -# rescue => e -# headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})") -# end + + if !auth_token.is_a? String + headers = { + 'user_agent' => "DomoscioRails V2 RubyBindings/#{DomoscioRails::VERSION}", + 'AccessToken' => "#{auth_token[:access_token]}", + 'RefreshToken' => "#{auth_token[:refresh_token]}", + 'Content-Type' => 'application/json' + } + else + headers = { + 'user_agent' => "DomoscioRails V2 RubyBindings/#{DomoscioRails::VERSION}", + 'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",#"#{auth_token['token_type']} #{auth_token['access_token']}", + 'Content-Type' => 'application/json' + } + end + headers + end - - + + end