Class: Bberg::Requests::RefdataRequestBase

Inherits:
Object
  • Object
show all
Defined in:
lib/bberg/requests/refdata_request_base.rb

Overview

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

Direct Known Subclasses

HistoricalDataRequest, ReferenceDataRequest

Instance Method Summary collapse

Constructor Details

#initializeRefdataRequestBase

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



13
14
15
# File 'lib/bberg/requests/refdata_request_base.rb', line 13

def initialize
  raise Bberg::BbergException.new("Do not instantiate base class!")      
end

Instance Method Details

#convert_to_rb_date(d) ⇒ Date (protected)

Convert a Java::ComBloomberglpBlpapi::Datetime to a ruby Date



143
144
145
# 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) ⇒ Time (protected)

Convert a Java::ComBloomberglpBlpapi::Datetime to a ruby Time



136
137
138
139
# 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) ⇒ Object (protected)

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

So far only time like types are affected.



126
127
128
129
130
131
132
# 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_serviceBberg::Native::Session, ... (protected)

Create a reference data service. This both creates and starts a session, and opens a refdata service.



88
89
90
91
92
93
94
95
# 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

#create_requestObject

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



39
40
41
# File 'lib/bberg/requests/refdata_request_base.rb', line 39

def create_request
  raise Bberg::BbergException.new("Not implemented on base class!")
end

#get_correlation_idFixnum (protected)

Get correlation ID.

NOTE: this needs to be updated so we have increasing unique IDs here.



101
102
103
104
# 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) ⇒ Hash (protected)

Utility method to merge and concatenate two Hashes.

This is useful for creating a cummulitative Hash result when reply consists of several events.



112
113
114
115
116
117
118
119
120
121
# 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

#parse_response(event) ⇒ Hash

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.



77
78
79
# 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_requestHash

Perform a synchronous reference data request. Calls (#create_request) to create the request object to send. Blocks while waiting for the response.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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_responseHash

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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