Class: FacebookAds::AdObject

Inherits:
Object
  • Object
show all
Includes:
Fields, GraphEdges, GraphNodes
Defined in:
lib/facebook_ads/ad_object.rb

Direct Known Subclasses

Ad, AdAccount, AdAccountAdRulesHistory, AdAccountDeliveryEstimate, AdAccountRoas, AdAccountTargetingUnified, AdAccountUser, AdActivity, AdAssetFeedSpec, AdAssetFeedSpecAssetLabel, AdAssetFeedSpecBody, AdAssetFeedSpecCaption, AdAssetFeedSpecDescription, AdAssetFeedSpecGroupRule, AdAssetFeedSpecImage, AdAssetFeedSpecLinkURL, AdAssetFeedSpecTargetRule, AdAssetFeedSpecTitle, AdAssetFeedSpecVideo, AdAsyncRequest, AdAsyncRequestSet, AdAsyncRequestSetNotificationResult, AdCampaignDeliveryEstimate, AdCreative, AdCreativeCollectionThumbnailInfo, AdCreativeLinkData, AdCreativeLinkDataAppLinkSpec, AdCreativeLinkDataCallToAction, AdCreativeLinkDataCallToActionValue, AdCreativeLinkDataChildAttachment, AdCreativeLinkDataImageOverlaySpec, AdCreativeObjectStorySpec, AdCreativeOfferData, AdCreativePhotoData, AdCreativePlaceData, AdCreativePostClickConfiguration, AdCreativeTextData, AdCreativeVideoData, AdImage, AdKeywordStats, AdLabel, AdPlacePageSet, AdPreview, AdPromotedObject, AdRecommendation, AdRecommendationData, AdReportRun, AdRule, AdRuleEvaluationSpec, AdRuleExecutionOptions, AdRuleExecutionSpec, AdRuleFilters, AdRuleHistory, AdRuleHistoryResult, AdRuleHistoryResultAction, AdRuleSchedule, AdRuleScheduleSpec, AdRuleTrigger, AdSet, AdStudy, AdStudyCell, AdStudyObjective, AdVideo, AdgroupPlacementSpecificReviewFeedback, AdgroupRelevanceScore, AdgroupReviewFeedback, AdsActionStats, AdsDataPartner, AdsImageCrops, AdsInsights, AdsPixel, AdsPixelStats, AdsPixelStatsResult, AgencyClientDeclaration, AndroidAppLink, AppLinks, BroadTargetingCategories, Business, BusinessAdAccountRequest, BusinessPageRequest, Campaign, CheckBatchRequestStatus, ConversionActionQuery, CustomAudience, CustomAudienceAdAccount, CustomAudienceDataSource, CustomAudiencePermission, CustomAudiencePrefillState, CustomAudienceSession, CustomAudienceStatus, CustomAudiencesTOS, CustomConversion, CustomConversionStatsResult, DayPart, DeliveryCheck, DeliveryCheckExtraInfo, DeliveryEstimate, Domain, Event, EventSourceGroup, ExternalEventSource, FlexibleTargeting, FundingSourceDetails, FundingSourceDetailsCoupon, Hotel, HotelRoom, IDName, IosAppLink, Lead, LeadGenQualifier, LeadGenQuestion, LeadGenQuestionOption, LeadgenForm, LegacyBusinessAdAccountRequest, LookalikeSpec, MinimumBudget, OfflineConversionDataSet, OffsitePixel, OutcomePredictionPoint, PartnerCategory, ProductCatalog, ProductCatalogHotelRoomsBatch, ProductCatalogImageSettings, ProductCatalogImageSettingsOperation, ProductCatalogPricingVariablesBatch, ProductFeed, ProductFeedSchedule, ProductFeedUpload, ProductFeedUploadError, ProductFeedUploadErrorSample, ProductGroup, ProductItem, ProductItemCommerceInsights, ProductSet, ProductVariant, ProfilePictureSource, RateCard, ReachEstimate, ReachFrequencyPrediction, ReachFrequencySpec, RevSharePolicy, RoasCohortsData, RoasCohortsPerCohortIntervalUnit, Targeting, TargetingDynamicRule, TargetingGeoLocation, TargetingGeoLocationCity, TargetingGeoLocationCustomLocation, TargetingGeoLocationElectoralDistrict, TargetingGeoLocationMarket, TargetingGeoLocationPlace, TargetingGeoLocationPoliticalDistrict, TargetingGeoLocationRegion, TargetingGeoLocationZip, TargetingProductAudienceSpec, TargetingProductAudienceSubSpec, TargetingSentenceLine, Transaction, TransactionCurrencyAmount, User, UserLeadGenDisclaimerResponse, UserLeadGenFieldData, VideoThumbnail, WebAppLink, WindowsAppLink, WindowsPhoneAppLink

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GraphEdges

included

Methods included from GraphNodes

included

Methods included from Fields

included

Constructor Details

#initialize(attributes, *args) ⇒ AdObject

Returns a new instance of AdObject.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/facebook_ads/ad_object.rb', line 35

def initialize(attributes, *args)
  if attributes.empty?
    raise InvalidParameterError, 'Invalid attributes. Must include at least one attribute'
  end

  update_attributes(attributes)
  # assume object with only id in the attributes as not loaded

  # is next arg a list of fields?
  fields = (args[0].is_a?(Array) || args[0].is_a?(String)) ? args.shift : []
  fields = fields.split(',') if fields.is_a?(String)
  session = args.shift

  self.internal_fields = fields + attributes.keys
  self.session = session
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/facebook_ads/ad_object.rb', line 27

def attributes
  @attributes
end

#deserializerObject

Returns the value of attribute deserializer.



28
29
30
# File 'lib/facebook_ads/ad_object.rb', line 28

def deserializer
  @deserializer
end

#internal_fieldsObject

Returns the value of attribute internal_fields.



27
28
29
# File 'lib/facebook_ads/ad_object.rb', line 27

def internal_fields
  @internal_fields
end

#last_api_responseObject (readonly)

Returns the value of attribute last_api_response.



27
28
29
# File 'lib/facebook_ads/ad_object.rb', line 27

def last_api_response
  @last_api_response
end

#last_destroyedObject

Returns the value of attribute last_destroyed.



29
30
31
# File 'lib/facebook_ads/ad_object.rb', line 29

def last_destroyed
  @last_destroyed
end

#last_savedObject

Returns the value of attribute last_saved.



29
30
31
# File 'lib/facebook_ads/ad_object.rb', line 29

def last_saved
  @last_saved
end

Class Method Details

.get(id, *args) {|obj| ... } ⇒ Object

Yields:

  • (obj)


52
53
54
55
56
# File 'lib/facebook_ads/ad_object.rb', line 52

def self.get(id, *args)
  obj = new({id: id}, *args)
  yield obj if block_given?
  obj
end

Instance Method Details

#[](attr_name) ⇒ Object



72
73
74
# File 'lib/facebook_ads/ad_object.rb', line 72

def [](attr_name)
  @attributes[attr_name.to_sym]
end

#as_json(*opts) ⇒ Object



80
81
82
# File 'lib/facebook_ads/ad_object.rb', line 80

def as_json(*opts)
  to_hash
end

#changesObject



155
156
157
# File 'lib/facebook_ads/ad_object.rb', line 155

def changes
  @changes ||= {}
end

#destroy(params = {}) ⇒ Object



149
150
151
152
153
# File 'lib/facebook_ads/ad_object.rb', line 149

def destroy(params = {})
  delete(params) do |attrs|
    self.last_destroyed = Time.now if attrs['success']
  end
end

#fields_as_stringObject



92
93
94
# File 'lib/facebook_ads/ad_object.rb', line 92

def fields_as_string
  @internal_fields.to_a.join(',')
end

#graph_paramsObject

TODO refactor this to somewhere



118
119
120
# File 'lib/facebook_ads/ad_object.rb', line 118

def graph_params
  {fields: fields_as_string}
end

#idObject



58
59
60
# File 'lib/facebook_ads/ad_object.rb', line 58

def id
  self[:id]
end

#inspectObject



88
89
90
# File 'lib/facebook_ads/ad_object.rb', line 88

def inspect
  "#<#{self.class.name} #{to_hash.inspect}>"
end

#load!Object



106
107
108
109
110
# File 'lib/facebook_ads/ad_object.rb', line 106

def load!
  get(graph_params) do |attrs|
    update_attributes(attrs)
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/facebook_ads/ad_object.rb', line 102

def loaded?
  (@internal_fields - attributes.keys).empty?
end

#reload!Object



112
113
114
115
# File 'lib/facebook_ads/ad_object.rb', line 112

def reload!
  # delete all attribute except id
  @attributes = @attributes.keep_if { |k,_| k == :id }
end

#save(params = {}) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/facebook_ads/ad_object.rb', line 141

def save(params = {})
  post(graph_params.merge(changes).merge(params)) do |attrs|
    update_attributes(attrs)
    @changes = {}
    self.last_saved = Time.now
  end
end

#sessionObject



159
160
161
# File 'lib/facebook_ads/ad_object.rb', line 159

def session
  @session || Session.default_session
end

#session=(session) ⇒ Object



163
164
165
# File 'lib/facebook_ads/ad_object.rb', line 163

def session=(session)
  @session = session.is_a?(Hash) ? Session.new(session) : session
end

#to_hashObject



76
77
78
# File 'lib/facebook_ads/ad_object.rb', line 76

def to_hash
  @attributes.merge(changes)
end

#to_json(*a) ⇒ Object



84
85
86
# File 'lib/facebook_ads/ad_object.rb', line 84

def to_json(*a)
  as_json.to_json(*a)
end

#update_attributes(attrs) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/facebook_ads/ad_object.rb', line 62

def update_attributes(attrs)
  @attributes ||= {}
  @attributes.merge!(
    self.class.deserializer ?
      self.class.deserializer.deserialize(symbolize_keys_shallow(attrs)) :
      symbolize_keys_shallow(attrs)
  )
  self
end