lib/dss_reuters.rb in dss_reuters-0.3.0 vs lib/dss_reuters.rb in dss_reuters-0.4.0

- old
+ new

@@ -1,5 +1,6 @@ +require "dotenv/load" require "logger" require "httparty" require "dss_reuters/version" module DssReuters @@ -58,46 +59,56 @@ end class OnDemandExtract include HTTParty base_uri Config::BASE_URI - attr_reader :result, :status + attr_reader :result + attr_accessor :status, :location def camelize(str) str.to_s.split('_').collect(&:capitalize).join end - def initialize(session, fields, identifiers, type) + def self.init_with_location(session, location) + ins = self.new(session) + ins.status = :in_progress + ins.location = location + ins + end + + def initialize(session, identifiers=nil, type=nil, fields=nil, condition=nil) @session = session @status = :init path = "/RestApi/v1/Extractions/ExtractWithNotes" - options = { - headers: { - "Prefer" => "respond-async; wait=5", - "Content-Type" => "application/json; odata=minimalmetadata", - "Authorization" => "Token #{@session.token}" - }, - body: { - "ExtractionRequest" => { - "@odata.type" => "#ThomsonReuters.Dss.Api.Extractions.ExtractionRequests.#{camelize(type)}ExtractionRequest", - "ContentFieldNames" => fields, - "IdentifierList" => { - "@odata.type" => "#ThomsonReuters.Dss.Api.Extractions.ExtractionRequests.InstrumentIdentifierList", - "InstrumentIdentifiers" => identifiers, - "ValidationOptions" => nil, - "UseUserPreferencesForValidationOptions" => false - }, - "Condition" => nil - } - }.to_json - } - resp = self.class.post path, options - if check_status(resp) - @location = resp["location"] + + if fields + options = { + headers: { + "Prefer" => "respond-async; wait=5", + "Content-Type" => "application/json; odata=minimalmetadata", + "Authorization" => "Token #{@session.token}" + }, + body: { + "ExtractionRequest" => { + "@odata.type" => "#{camelize(type)}ExtractionRequest", + "ContentFieldNames" => fields, + "IdentifierList" => { + "@odata.type" => "InstrumentIdentifierList", + "InstrumentIdentifiers" => identifiers, + "ValidationOptions" => nil, + "UseUserPreferencesForValidationOptions" => false + }, + "Condition" => condition + } + }.to_json + } + resp = self.class.post path, options + if check_status(resp) + @location = resp["location"] + end + @session.logger.debug resp end - pp resp - @session.logger.debug resp end def check_status(resp) if resp["status"] == "InProgress" @status = :in_progress @@ -115,10 +126,11 @@ } } @result = self.class.get @location, options check_status @result @session.logger.debug @result + @status end end end class Api @@ -128,11 +140,15 @@ def get_user @user = User.new(@session) end - def extract_with_isin(isin_code, fields=nil, type=:composite) + def extract_with_location(location) + OnDemandExtract.init_with_location(@session, location) + end + + def extract_with_isin(isin_code, type=:composite, fields=nil, condition=nil) fields ||= [ "Close Price", "Contributor Code Description", "Currency Code Description", "Dividend Yield", @@ -146,9 +162,9 @@ { "Identifier" => isin_code, "IdentifierType" => "Isin" } ] - OnDemandExtract.new(@session, fields, identifiers, type) + OnDemandExtract.new(@session, identifiers, type, fields, condition) end end end