Class: AdManagerApi::PQLValues

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_manager_api/pql_statement_utils.rb

Overview

Values class used by StatementBuilder.

Constant Summary collapse

VALUE_TYPES =
{
  Numeric => 'NumberValue',
  String => 'TextValue',
  TrueClass => 'BooleanValue',
  FalseClass => 'BooleanValue',
  Array => 'SetValue',
  AdManagerApi::AdManagerDate => 'DateValue',
  AdManagerApi::AdManagerDateTime => 'DateTimeValue'
}

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ PQLValues

Create a new values object.



55
56
57
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 55

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.



60
61
62
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 60

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.



93
94
95
96
97
98
99
100
101
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 93

def generate_value_object(value)
  typeKeyValue = VALUE_TYPES.find {|key, val| value.is_a? key}
  dateTypes = [AdManagerApi::AdManagerDate, AdManagerApi::AdManagerDateTime]
  if dateTypes.include?(value.class)
    value = value.to_h
  end
  return value if typeKeyValue.nil?
  return {:xsi_type => typeKeyValue.last, :value => value}
end

#valuesObject

Get values as an array, the format the Ad Manager API expects.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ad_manager_api/pql_statement_utils.rb', line 65

def values()
  # values_array is an Ad-Manager-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 'Misconfigured value in StatementBuilder.' unless value.is_a? Hash
    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