Skip to content

Commit

Permalink
as_user_id is now required parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
smaeda-ks committed Sep 15, 2019
1 parent 9683ca8 commit 61bfa97
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion examples/draft_tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# load up the account instance, campaign and line item
account = client.accounts(ADS_ACCOUNT)

# get user_id for as_user_id parameter
user_id = TwitterRestApi::UserIdLookup.load(account, screen_name: 'your_twitter_handle_name').id

# fetch draft tweets from a given account
tweets = TwitterAds::Creative::DraftTweet.all(account)
tweets.each { |tweet|
Expand All @@ -30,6 +33,7 @@
# create a new draft tweet
draft_tweet = TwitterAds::Creative::DraftTweet.new(account)
draft_tweet.text = 'draft tweet - new'
draft_tweet.as_user_id = user_id
draft_tweet.save
p draft_tweet.id_str
p draft_tweet.text
Expand All @@ -52,7 +56,7 @@
# draft_tweet.preview(draft_tweet_id: '1142048306194862080')

# create a nullcasted tweet using draft tweet metadata
tweet = TwitterAds::Tweet.create(account, text: draft_tweet.text)
tweet = TwitterAds::Tweet.create(account, text: draft_tweet.text, as_user_id: user_id)
p tweet

# delete draft tweet
Expand Down
8 changes: 6 additions & 2 deletions examples/promoted_tweet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
campaign = account.campaigns.first
line_item = account.line_items(nil, campaign_ids: campaign.id).first

# get user_id for as_user_id parameter
user_id = TwitterRestApi::UserIdLookup.load(account, screen_name: 'your_twitter_handle_name').id

# create request for a simple nullcasted tweet
tweet1 = TwitterAds::Tweet.create(account, text: 'There can be only one...')
tweet1 = TwitterAds::Tweet.create(account, text: 'There can be only one...', as_user_id: user_id)

# promote the tweet using our line item
promoted_tweet = TwitterAds::Creative::PromotedTweet.new(account)
Expand All @@ -36,7 +39,8 @@
tweet2 = TwitterAds::Tweet.create(
account,
text: 'Fine. There can be two.',
card_uri: website_card.card_uri
card_uri: website_card.card_uri,
as_user_id: user_id
)

# promote the tweet using our line item
Expand Down
2 changes: 2 additions & 0 deletions lib/twitter-ads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
require 'twitter-ads/http/request'
require 'twitter-ads/http/response'

require 'twitter-ads/restapi.rb'

require 'twitter-ads/audiences/tailored_audience'

require 'twitter-ads/campaign/app_list'
Expand Down
29 changes: 29 additions & 0 deletions lib/twitter-ads/restapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true
# Copyright (C) 2019 Twitter, Inc.

module TwitterRestApi
class UserIdLookup
include TwitterAds::DSL
include TwitterAds::Resource

attr_reader :account

property :id, read_only: true
property :id_str, read_only: true
property :screen_name, read_only: true

DOMAIN = 'https://api.twitter.com'
RESOURCE = '/1.1/users/show.json'

def self.load(account, opts = {})
response = TwitterAds::Request.new(
account.client,
:get,
RESOURCE,
params: opts,
domain: DOMAIN
).perform
new.from_response(response.body, response.headers)
end
end
end

0 comments on commit 61bfa97

Please sign in to comment.