module TwitterAds::DSL::InstanceMethods
Public Instance Methods
from_response(object, headers = nil)
click to toggle source
Populates a given objects attributes from a parsed JSON API response. This helper handles all necessary type coercions as it assigns attribute values.
@param object [Hash] The parsed JSON response object.
@return [self] A fully hydrated instance of the current class.
@since 0.1.0
# File lib/twitter-ads/resources/dsl.rb, line 23 def from_response(object, headers = nil) if !headers.nil? TwitterAds::Utils.extract_response_headers(headers).each { |key, value| singleton_class.class_eval { attr_accessor key } instance_variable_set("@#{key}", value) } end self.class.properties.each do |name, type| value = nil if type == :time && object[name] && !object[name].empty? value = Time.parse(object[name]) elsif type == :bool && object[name] value = TwitterAds::Utils.to_bool(object[name]) end instance_variable_set("@#{name}", value || object[name]) end self end
to_params()
click to toggle source
Generates a Hash of property values for the current object. This helper handles all necessary type coercions as it generates its output.
@return [Hash] A Hash of the object's properties and cooresponding values.
@since 0.1.0
# File lib/twitter-ads/resources/dsl.rb, line 49 def to_params params = {} self.class.properties.each do |name, type| value = instance_variable_get("@#{name}") || send(name) next if value.nil? # handles unset with empty string if value.respond_to?(:strip) && value.strip == '' params[name] = value.strip next end if type == :time params[name] = value.iso8601 elsif type == :bool params[name] = TwitterAds::Utils.to_bool(value) elsif value.is_a?(Array) next if value.empty? params[name] = value.join(',') else params[name] = value end end params end