class Bberg::Requests::RefdataRequestBase

Base class for reference data requests. Child classes implements particular requests, using features of this base class.

Public Class Methods

new() click to toggle source

raises exception, do not instantiate base class - only use child classes.

# File lib/bberg/requests/refdata_request_base.rb, line 13
def initialize
  raise Bberg::BbergException.new("Do not instantiate base class!")      
end

Public Instance Methods

create_request() click to toggle source

Create the reference data request to send to server. To be implemented by specialized child classes. Implementation on base class raises exception.

# File lib/bberg/requests/refdata_request_base.rb, line 39
def create_request
  raise Bberg::BbergException.new("Not implemented on base class!")
end
parse_response(event) click to toggle source

Parse response from server. Ideally this should convert the java response into a ruby friendly format. To be implemented by specialized child classes. Implementation on base class raises exception. @return [Hash] the information in the result parsed to Hash format.

# File lib/bberg/requests/refdata_request_base.rb, line 77
def parse_response(event)
  raise Bberg::BbergException.new("Not implemented in base class!")
end
perform_request() click to toggle source

Perform a synchronous reference data request. Calls (create_request) to create the request object to send. Blocks while waiting for the response. @return [Hash] A parsed response in the form of a Hash.

# File lib/bberg/requests/refdata_request_base.rb, line 21
def perform_request
  @session, @svc, @req_id = create_ref_data_service()
  
  create_request
  
  @session.sendRequest(@request, @req_id)
  
  response = retrieve_response

  @session.stop()
  @session = nil
  
  response
end
retrieve_response() click to toggle source

Retrieve response for this request. Will retrieve events from the request’s session until an event of type REPONSE is found. For each event (partial or not) it will callse (parse_response) and merge the hash returned into a cummulitative result.

Note: if you set the $DEBUG flag the unparsed event will be printed on STDOUT. @return [Hash] A parsed response in the form of a Hash.

# File lib/bberg/requests/refdata_request_base.rb, line 50
def retrieve_response
  done = false
  result = Hash.new
  until done
    event = @session.nextEvent()
    case event.eventType().intValue()
    when Bberg::Native::Event::EventType::Constants::RESPONSE
      print_response_event(event) if $DEBUG
      event_result = parse_response(event)
      result = hash_merge_concat(result, event_result)
      done = true
    when Bberg::Native::Event::EventType::Constants::PARTIAL_RESPONSE
      print_response_event(event) if $DEBUG
      event_result = parse_response(event)
      result = hash_merge_concat(result, event_result)
    else
      print_other_event(event) if $DEBUG
    end
  end
  result
end

Protected Instance Methods

convert_to_rb_date(d) click to toggle source

Convert a Java::ComBloomberglpBlpapi::Datetime to a ruby Date @return [Date] value as Date

# File lib/bberg/requests/refdata_request_base.rb, line 143
def convert_to_rb_date(d)
  Date.new(d.year, d.month, d.dayOfMonth)
end
convert_to_rb_time(dt) click to toggle source

Convert a Java::ComBloomberglpBlpapi::Datetime to a ruby Time @return [Time] value as Time

# File lib/bberg/requests/refdata_request_base.rb, line 136
def convert_to_rb_time(dt)
  hour = dt.hour == 24 ? 0 : dt.hour
  Time.local(dt.year, dt.month, dt.dayOfMonth, hour, dt.minute, dt.second, dt.milliSecond)
end
convert_value_to_bberg(value) click to toggle source

Utility method to convert a ruby values to their bberg format.

So far only time like types are affected.

# File lib/bberg/requests/refdata_request_base.rb, line 126
def convert_value_to_bberg(value)
  if value.is_a? Date or value.is_a? DateTime or value.is_a? Time
    value.strftime("%Y%m%d")
  else
    value
  end
end
create_ref_data_service() click to toggle source

Create a reference data service. This both creates and starts a session, and opens a refdata service. @return [Bberg::Native::Session, Object, Fixnum] session, service and request ID.

# File lib/bberg/requests/refdata_request_base.rb, line 88
def create_ref_data_service
  session = Bberg::Native::Session.new(@session_options)
  raise Bberg::BbergException.new("Could not start session!") unless session.start()
  raise Bberg::BbergException.new("Could not open service!") unless session.openService("//blp/refdata")
  request_id = get_correlation_id()
  ref_data_service = session.getService("//blp/refdata")
  [session, ref_data_service, request_id]
end
get_correlation_id() click to toggle source

Get correlation ID.

NOTE: this needs to be updated so we have increasing unique IDs here. @return [Fixnum] correlation ID.

# File lib/bberg/requests/refdata_request_base.rb, line 101
def get_correlation_id
  # TODO: we need a mutex protected instance variable of increasing ID's to pass in here
  Bberg::Native::CorrelationID.new(1)
end
hash_merge_concat(existing_hash, new_hash) click to toggle source

Utility method to merge and concatenate two Hashes.

This is useful for creating a cummulitative Hash result when reply consists of several events. @param [Hash] existing_hash what we have so far @param [Hash] new_hash partial result to add @return [Hash] merged and concatenated result

# File lib/bberg/requests/refdata_request_base.rb, line 112
def hash_merge_concat(existing_hash, new_hash)
  new_hash.each do |key, value|
    if existing_hash.has_key? key
      existing_hash[key] = existing_hash[key].concat(value)
    else
      existing_hash[key] = value
    end
  end
  existing_hash
end

Private Instance Methods

print_other_event(event) click to toggle source
print_response_event(event) click to toggle source

PRIVATE ############################