Class: DfpApi::DfpDateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/dfp_api/dfp_api_datetime.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, *args) ⇒ DfpDateTime

Create a new DfpDateTime, a utility class that allows for interoperability between the DFP API's DateTime objects and ruby's Time class. The last argument must be a valid timezone identifier, e.g. "America/New_York".

Args:

- args:
- ([year, [month, [day, [hour, [minute, [second,]]]]]] timezone)
- (time, timezone), a native Time object and a timezone identifier
- (dfp_datetime), a DFP DateTime hash representation

Returns:

- dfp_datetime: an instance of DfpDateTime


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dfp_api/dfp_api_datetime.rb', line 92

def initialize(api, *args)
  @api = api
  @api.utils_reporter.dfp_date_time_used()

  # Handle special cases when a DFP DateTime hash or ruby Time instance are
  # passed as the first argument to the constructor.
  case args.first
  when Hash
    hash = args.first
    datetime_args = [hash[:date][:year], hash[:date][:month],
        hash[:date][:day], hash[:hour], hash[:minute], hash[:second],
        hash[:time_zone_id]]
  when DfpDateTime, Time, DateTime
    time = args.first
    datetime_args = [args.last]
    [:sec, :min, :hour, :day, :month, :year].each do |duration|
      datetime_args.unshift(time.send(duration))
    end
  else
    datetime_args = args
  end
  # Check the validity of the timezone parameter, which is required.
  if not TZInfo::Timezone.all_identifiers.include?(datetime_args.last)
    raise "Last argument to DfpDateTime constructor must be valid timezone"
  end
  # Set timezone attribute and pass its utc offset into the Time
  # constructor.
  @timezone = TZInfo::Timezone.get(datetime_args.pop)
  @time = Time.new(*datetime_args,
      utc_offset=@timezone.current_period.utc_offset)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

When an unrecognized method is applied to DfpDateTime, pass it through to the internal ruby Time.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dfp_api/dfp_api_datetime.rb', line 166

def method_missing(name, *args, &block)
  # Restrict time zone related functions from being passed to internal ruby
  # Time attribute, since DfpDateTime handles timezones its own way
  restricted_functions = [:dst?, :getgm, :getlocal, :getutc, :gmt,
      :gmtime, :gmtoff, :isdst, :localtime, :utc]
  if restricted_functions.include? name
    raise NoMethodError, 'undefined method %s for %s' % [name, self]
  end
  result = @time.send(name, *args, &block)
  if result.is_a? Time
    return self.class.new(@api, result, @timezone.identifier)
  else
    return result
  end
end

Instance Attribute Details

#timezone ⇒ Object

Returns the value of attribute timezone.



78
79
80
# File 'lib/dfp_api/dfp_api_datetime.rb', line 78

def timezone
  @timezone
end

Class Method Details

.now(api, timezone) ⇒ Object

Create a DfpDateTime for the current time in the specified timezone.

Args:

- timezone: a valid timezone identifier, e.g. "America/New_York"

Returns:

- dfp_datetime: an instance of DfpDateTime


131
132
133
# File 'lib/dfp_api/dfp_api_datetime.rb', line 131

def self.now(api, timezone)
  new(api, TZInfo::Timezone.get(timezone).now, timezone)
end

.utc(api, *args) ⇒ Object

Create a DfpDateTime in the "UTC" timezone. Calls the DfpDateTime contstructor with timezone identifier "UTC".

Args:

- ([year, [month, [day, [hour, [minute, [second]]]]]])

Returns:

- dfp_datetime: an instance of DfpDateTime


143
144
145
# File 'lib/dfp_api/dfp_api_datetime.rb', line 143

def self.utc(api, *args)
  new(api, *args + ['UTC'])
end

Instance Method Details

#to_h ⇒ Object

Convert DfpDateTime into a hash representation which can be consumed by the DFP API. E.g., a hash that can be passed as PQL DateTime variables.

Returns:

- dfp_datetime_hash: a hash representation of a DfpDateTime


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/dfp_api/dfp_api_datetime.rb', line 152

def to_h
  {
    :date => DfpApi::DfpDate.new(
        @api, @time.year, @time.month, @time.day
    ).to_h,
    :hour => @time.hour,
    :minute => @time.min,
    :second => @time.sec,
    :time_zone_id => @timezone.identifier
  }
end