Class: DfpApi::PQLValues
- Inherits:
-
Object
- Object
- DfpApi::PQLValues
- Defined in:
- lib/dfp_api/pql_statement_utils.rb
Overview
Values class used by StatementBuilder.
Constant Summary collapse
- VALUE_TYPES =
{ Numeric => 'NumberValue', Integer => 'NumberValue', Float => 'NumberValue', String => 'TextValue', TrueClass => 'BooleanValue', FalseClass => 'BooleanValue', Array => 'SetValue', DfpApi::DfpDate => 'DateValue', DfpApi::DfpDateTime => 'DateTimeValue' }
Instance Method Summary collapse
-
#add_value(key, value) ⇒ Object
Add a value to the current values object on the provided key.
-
#generate_value_object(value) ⇒ Object
Create an individual value object by inferring the xsi_type.
-
#initialize(values = {}) ⇒ PQLValues
constructor
Create a new values object.
-
#values ⇒ Object
Get values as an array, the format the DFP API expects.
Constructor Details
#initialize(values = {}) ⇒ PQLValues
Create a new values object.
60 61 62 |
# File 'lib/dfp_api/pql_statement_utils.rb', line 60 def initialize(values = {}) @values = values || {} end |
Instance Method Details
#add_value(key, value) ⇒ Object
Add a value to the current values object on the provided key.
65 66 67 |
# File 'lib/dfp_api/pql_statement_utils.rb', line 65 def add_value(key, value) @values[key.to_sym] = generate_value_object(value) end |
#generate_value_object(value) ⇒ Object
Create an individual value object by inferring the xsi_type. If the value type isn't recognized, return the original value parameter.
97 98 99 100 101 102 103 104 |
# File 'lib/dfp_api/pql_statement_utils.rb', line 97 def generate_value_object(value) type = VALUE_TYPES[value.class] if [DfpApi::DfpDate, DfpApi::DfpDateTime].include?(value.class) value = value.to_h end return value if type.nil? return {:xsi_type => type, :value => value} end |
#values ⇒ Object
Get values as an array, the format the DFP API expects.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dfp_api/pql_statement_utils.rb', line 70 def values() # values_array is a DFP compliant list of values of the following form: # [:key => ..., :value => {:xsi_type => ..., :value => ...}] values_array = @values.map do |key, value| raise 'Missing value in StatementBuilder.' if value.nil? raise 'Value cannot be nil on StatementBuilder.' if value[:value].nil? raise 'Missing value type for %s.' % key if value[:xsi_type].nil? unless VALUE_TYPES.values.include?(value[:xsi_type]) raise 'Unknown value type for %s.' % key end if value[:xsi_type] == 'SetValue' if value[:value].map {|v| v.class}.to_set.size > 1 raise 'Cannot pass more than one type in set variable, %s.' % key end end if value[:xsi_type] == 'DateTimeValue' unless value[:value][:time_zone_id] raise 'Missing timezone on DateTimeValue variable, %s.' %key end end {:key => key.to_s(), :value => value} end return values_array end |