lib/dss_reuters.rb in dss_reuters-0.5.0 vs lib/dss_reuters.rb in dss_reuters-0.6.0

- old
+ new

@@ -4,21 +4,29 @@ require "dss_reuters/version" module DssReuters class Config BASE_URI = "https://hosted.datascopeapi.reuters.com" - DSS_USERNAME = ENV.fetch('DSS_USERNAME') - DSS_PASSWORD = ENV.fetch('DSS_PASSWORD') LOG_LEVEL = ENV['DSS_LOG_LEVEL'] || 'INFO' + DSS_USERNAME = ENV['DSS_USERNAME'] + DSS_PASSWORD = ENV['DSS_PASSWORD'] end class Session include HTTParty base_uri Config::BASE_URI attr_reader :context, :token, :logger + def configured? + !Config::DSS_USERNAME.nil? and !Config::DSS_PASSWORD.nil? + end + + def not_configured_error + @logger.error "dss_reuters gem not configured. you will not be able to fetch data from dss reuters API" + end + def initialize @logger = ::Logger.new(STDOUT) @logger.level = Config::LOG_LEVEL login_path = "/RestApi/v1/Authentication/RequestToken" options = { @@ -31,14 +39,18 @@ "Username" => Config::DSS_USERNAME, "Password" => Config::DSS_PASSWORD } }.to_json } - resp = self.class.post login_path, options - @token = resp["value"] - @context = resp["@odata.context"] - @logger.debug resp + if configured? + resp = self.class.post login_path, options + @token = resp["value"] + @context = resp["@odata.context"] + @logger.debug resp + else + not_configured_error + end end end class User include HTTParty @@ -51,12 +63,16 @@ headers: { "Prefer" => "respond-async", "Authorization" => "Token #{@session.token}" } } - resp = self.class.get path, options - @session.logger.debug resp + if session.configured? + resp = self.class.get path, options + @session.logger.debug resp + else + session.not_configured_error + end end end class OnDemandExtract include HTTParty @@ -99,15 +115,19 @@ }, "Condition" => condition } }.to_json } - resp = self.class.post path, options - if check_status(resp) - @location = resp["location"] + if session.configured? + resp = self.class.post path, options + if check_status(resp) + @location = resp["location"] + end + @session.logger.debug resp + else + session.not_configured_error end - @session.logger.debug resp end end def check_status(resp) if resp["status"] == "InProgress" @@ -123,13 +143,17 @@ headers: { "Prefer" => "respond-async; wait=5", "Authorization" => "Token #{@session.token}" } } - @result = self.class.get @location, options - check_status @result - @session.logger.debug @result - @status + if @session.configured? + @result = self.class.get @location, options + check_status @result + @session.logger.debug @result + @status + else + @session.not_configured_error + end end end end class Api