Class: DfpApi::DfpDate

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, *args) ⇒ DfpDate

Create a new DfpDate, a utility class that allows for interoperability between the DFP API's Date objects and ruby's Date class.

Args:

- args:
- ([year, [month, [day]]]), integer values, uses Date defaults
- (date), a native ruby Date object
- (dfp_date), a DFP Date hash representation

Returns:

- dfp_date: an instance of DfpDate


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dfp_api/dfp_api_datetime.rb', line 37

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

  case args.first
  when Hash
    hash = args.first
    date_args = [hash[:year], hash[:month], hash[:day]]
  when Date
    date = args.first
    date_args = [date.year, date.month, date.day]
  else
    date_args = args
  end
  @date = Date.new(*date_args)
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 DfpDate, pass it through to the internal ruby Date.



61
62
63
64
65
# File 'lib/dfp_api/dfp_api_datetime.rb', line 61

def method_missing(name, *args, &block)
  result = @date.send(name, *args, &block)
  return self.class.new(@api, result) if result.is_a? Date
  return result
end

Class Method Details

.today(api, *args) ⇒ Object

Creates a DfpDate object denoting the present day.



55
56
57
# File 'lib/dfp_api/dfp_api_datetime.rb', line 55

def self.today(api, *args)
  self.new(api, Date.today(*args))
end

Instance Method Details

#to_h ⇒ Object

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

Returns:

- dfp_datetime: a DFP Date hash representation


72
73
74
# File 'lib/dfp_api/dfp_api_datetime.rb', line 72

def to_h()
  return {:year => @date.year, :month => @date.month, :day => @date.day}
end