Class: AdManagerApi::AdManagerDateTime
- Inherits:
-
Object
- Object
- AdManagerApi::AdManagerDateTime
- Defined in:
- lib/ad_manager_api/ad_manager_api_datetime.rb
Instance Attribute Summary collapse
-
#timezone ⇒ Object
Returns the value of attribute timezone.
Class Method Summary collapse
-
.now(api, timezone) ⇒ Object
Create an AdManagerDateTime for the current time in the specified timezone.
-
.utc(api, *args) ⇒ Object
Create an AdManagerDateTime in the “UTC” timezone.
Instance Method Summary collapse
-
#initialize(api, *args) ⇒ AdManagerDateTime
constructor
Create a new AdManagerDateTime, a utility class that allows for interoperability between the Ad Manager API’s DateTime objects and ruby’s Time class.
-
#method_missing(name, *args, &block) ⇒ Object
When an unrecognized method is applied to AdManagerDateTime, pass it through to the internal ruby Time.
-
#to_h ⇒ Object
Convert AdManagerDateTime into a hash representation which can be consumed by the Ad Manager API.
-
#to_time ⇒ Object
Convert AdManagerDateTime into a native ruby Time object.
Constructor Details
#initialize(api, *args) ⇒ AdManagerDateTime
Create a new AdManagerDateTime, a utility class that allows for interoperability between the Ad Manager 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
- (ad_manager_datetime), an Ad Manager DateTime hash representation
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 99 def initialize(api, *args) @api = api @api.utils_reporter.ad_manager_date_time_used() # Handle special cases when an Ad Manager 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 AdManagerDateTime, 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 AdManagerDateTime 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 AdManagerDateTime, pass it through to the internal ruby Time.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 183 def method_missing(name, *args, &block) # Restrict time zone related functions from being passed to internal ruby # Time attribute, since AdManagerDateTime 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.
84 85 86 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 84 def timezone @timezone end |
Class Method Details
.now(api, timezone) ⇒ Object
Create an AdManagerDateTime for the current time in the specified timezone.
Args:
- timezone: a valid timezone identifier, e.g. "America/New_York"
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
140 141 142 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 140 def self.now(api, timezone) new(api, TZInfo::Timezone.get(timezone).now, timezone) end |
.utc(api, *args) ⇒ Object
Create an AdManagerDateTime in the “UTC” timezone. Calls the AdManagerDateTime contstructor with timezone identifier “UTC”.
Args:
- ([year, [month, [day, [hour, [minute, [second]]]]]])
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
152 153 154 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 152 def self.utc(api, *args) new(api, *args + ['UTC']) end |
Instance Method Details
#to_h ⇒ Object
Convert AdManagerDateTime into a hash representation which can be consumed by the Ad Manager API. E.g., a hash that can be passed as PQL DateTime variables.
Returns:
- ad_manager_datetime_hash: a hash representation of an
AdManagerDateTime
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 163 def to_h { :date => AdManagerApi::AdManagerDate.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 |
#to_time ⇒ Object
Convert AdManagerDateTime into a native ruby Time object.
176 177 178 179 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 176 def to_time return Time.new(@time.year, @time.month, @time.day, @time.hour, @time.min, @time.sec, @timezone.current_period.utc_offset) end |