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.
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
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 123 124 125 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 94 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.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 172 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.
79 80 81 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 79 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
135 136 137 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 135 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
147 148 149 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 147 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
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ad_manager_api/ad_manager_api_datetime.rb', line 158 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 |