Class: Bberg::Requests::ReferenceDataRequest

Inherits:
RefdataRequestBase show all
Defined in:
lib/bberg/requests/reference_data_request.rb

Overview

A class for preforming reference data requets.

Constant Summary collapse

DEFAULT_OPTIONS =
Hash[
:fields => ["PX_SETTLE"]

Instance Method Summary collapse

Methods inherited from RefdataRequestBase

#convert_to_rb_date, #convert_to_rb_time, #convert_value_to_bberg, #create_ref_data_service, #get_correlation_id, #hash_merge_concat, #perform_request, #retrieve_response

Constructor Details

#initialize(session_options, identifiers, options_arg = {}) ⇒ ReferenceDataRequest

Create new instance.

Parameters:

  • session_options (Bberg::Native::SessionOptions)

    to specify how to connect session.

  • identifiers (#each|String)

    a list of identifiers for this request

  • options_arg (Hash) (defaults to: {})

    specification of what fields or other parameters to use for the request.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bberg/requests/reference_data_request.rb', line 21

def initialize(session_options, identifiers, options_arg = {})
  @session_options = session_options
  
  @identifiers = unless identifiers.respond_to? 'each'
    [identifiers]
  else
    identifiers
  end
  
  @options = DEFAULT_OPTIONS.merge(options_arg)
end

Instance Method Details

#create_requestObject

Create a reference data request.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bberg/requests/reference_data_request.rb', line 34

def create_request
  request = @svc.createRequest("ReferenceDataRequest")

  @identifiers.each {|identifier| request.append("securities", identifier) }
  
  @options.each do |key, value|
    next if key == :fields or key == :overrides
    request.set(key.to_s, convert_value_to_bberg(value))
  end
  
  @options[:fields].each {|f| request.append("fields", f) }
  
  overrides = request.getElement("overrides")
  @options[:overrides].each do |field_id, value|
    new_override = overrides.appendElement()
    new_override.setElement("fieldId", field_id.to_s)
    new_override.setElement("value", convert_value_to_bberg(value))
  end
  @request = request
end

#parse_response(event) ⇒ Hash

Parse event for ReferenceDataResponse.

Returns:

  • (Hash)

    event parsed into a Hash format.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bberg/requests/reference_data_request.rb', line 57

def parse_response(event)
  iter = event.messageIterator()
  result = Hash.new
  
  while iter.hasNext()
    
    message = iter.next()
    raise Bberg::BbergException.new("Got a response with incorrect correlation id!") if message.correlationID != @req_id
    msg_type = message.messageType().toString()
    raise Bberg::BbergException.new("Expected message of type ReferenceDataResponse but got #{msg_type}") if msg_type != "ReferenceDataResponse"
    
    security_data_array = message.getElement("securityData")
    (0..(security_data_array.numValues - 1)).each do |sec_num|
      security_data = security_data_array.getValueAsElement(sec_num)
      security_name = security_data.getElementAsString("security")
      field_data = security_data.getElement("fieldData")

      result[security_name] ||= Hash.new
      
      (0..(field_data.numElements - 1)).each do |field_num|
        field_element = field_data.getElement(field_num)
        values = if field_element.isArray
          process_array_type(field_element)
        else
          get_element_values(field_data, field_num)
        end
        result[security_name][field_element.name.toString] = values
      end
    end
  end
  result
end