Skip to content

Commit cf19b87

Browse files
Address review comments
1 parent 6dc25bd commit cf19b87

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

doc/default/x.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
`Faker::Twitter` has been deprecated. While it's still maintained, its docs can be found at [Faker::Twitter](#fakertwitter).
1+
`Faker::Twitter` has been deprecated in favor of `Faker::X`. While it's still maintained, its docs can be found at [Faker::Twitter](#fakertwitter).
22

33
# Faker::X
44

5-
Generates approximate X (previous Twitter) user and tweet objects, based on X's API v2 responses.
5+
Generates approximate X (previously Twitter) user and tweet objects, based on X's API v2 responses.
66

77
The generators are not a comprehensive match of the API. However, they are enough to create a demo app using X’s user or tweet data, for example.
88

lib/faker/default/twitter.rb

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def user
246246
},
247247
username: screen_name,
248248
pinned_tweet_id: Faker::Number.between(from: 1, to: 9_223_372_036_854_775_807).to_s,
249-
entities: entities(url: url),
249+
entities: user_entities,
250250
description: Faker::Lorem.sentence,
251251
name: Faker::Name.name,
252252
verified: Faker::Boolean.boolean(true_ratio: 0.1),
@@ -277,39 +277,22 @@ def user
277277
# @faker.version 1.7.3
278278
def tweet(include_media: false, include_user: false)
279279
tweet = {}
280-
conversation_id = id.to_s
281-
282-
data = [
283-
{
284-
id: conversation_id,
285-
text: Faker::Lorem.sentence,
286-
lang: Faker::Address.country_code,
287-
conversation_id: conversation_id,
288-
created_at: created_at,
289-
public_metrics: {
290-
retweet_count: Faker::Number.between(to: 1, from: 100),
291-
reply_count: Faker::Number.between(to: 1, from: 20),
292-
like_count: Faker::Number.between(to: 1, from: 25),
293-
quote_count: Faker::Number.between(to: 1, from: 10)
294-
},
295-
possibly_sensitive: Faker::Boolean.boolean(true_ratio: 0.1),
296-
entities: entities(url: url),
297-
in_reply_to_user_id: Faker::Boolean.boolean(true_ratio: 0.1)
298-
}
299-
]
300-
301-
tweet[:data] = data
280+
tweet_object = tweet_item
302281
includes = {}
303282

304283
if include_media
305284
media_key = Faker::Number.between(from: 1, to: 9_223_372_036_854_775_807).to_s
306285

307286
includes[:media] = media(media_key)
308-
tweet[:data] << { attachments: { media_keys: [media_key] } }
287+
tweet_object[:attachments] = { media_keys: [media_key] }
309288
end
310289

311290
includes[:users] = Faker::X.user[:includes][:users] if include_user
312291

292+
tweet[:data] = [
293+
tweet_object
294+
]
295+
313296
unless includes.empty?
314297
tweet[:includes] = includes
315298
end
@@ -337,52 +320,80 @@ def id
337320
end
338321

339322
def created_at
340-
Faker::Date.between(from: '2006-03-21', to: ::Date.today).strftime('%Y-%m-%dT%H:%M:%S%:z')
323+
Faker::Date.between(from: '2006-03-21', to: ::Date.today).to_time.utc.iso8601(3)
341324
end
342325

343326
def url
344327
"https://t.co/#{Faker::Lorem.characters(number: 10)}"
345328
end
346329

347-
def entities(url:)
348-
display_url = Faker::Internet.url(host: 'example.com')
349-
330+
def user_entities
350331
{
351332
url: {
352-
urls: [
353-
{
354-
url: url,
355-
expanded_url: display_url,
356-
display_url: display_url.sub('http://', '')
357-
}
358-
]
333+
urls: tweet_entities[:urls]
359334
},
360335
description: {
361-
hashtags: [
362-
{
363-
tag: Faker::Lorem.word.capitalize
364-
}
365-
]
336+
hashtags: tweet_entities[:hashtags]
366337
}
367338
}
368339
end
369340

341+
def tweet_entities
342+
display_url = Faker::Internet.url(host: 'example.com')
343+
344+
{
345+
urls: [
346+
{
347+
start: 0,
348+
end: 0,
349+
url: url,
350+
expanded_url: display_url,
351+
display_url: display_url.sub('http://', '')
352+
}
353+
],
354+
hashtags: [
355+
{
356+
start: 0,
357+
end: 0,
358+
tag: Faker::Lorem.word.capitalize
359+
}
360+
]
361+
}
362+
end
363+
370364
def media(media_key)
371365
expanded_url = Faker::LoremFlickr.image(size: '1064x600')
372366

373367
[{
374-
type: 'photo',
375-
indices: [103, 126],
376368
height: Faker::Number.between(to: 1, from: 1000),
377369
media_key: media_key,
370+
type: 'photo',
378371
preview_image_url: expanded_url,
379372
width: Faker::Number.between(to: 1, from: 2000),
380-
url: "https://t.co/#{Faker::Lorem.characters(number: 10)}",
381-
expanded_url: expanded_url,
382-
display_url: expanded_url.sub('https://', ''),
383373
alt_text: Faker::Lorem.sentence
384374
}]
385375
end
376+
377+
def tweet_item
378+
conversation_id = id.to_s
379+
380+
{
381+
id: conversation_id,
382+
text: Faker::Lorem.sentence,
383+
lang: sample(%w[en fr es pt it ja]),
384+
conversation_id: conversation_id,
385+
created_at: created_at,
386+
author_id: Faker::Number.between(from: 1, to: 9_223_372_036_854_775_807).to_s,
387+
public_metrics: {
388+
retweet_count: Faker::Number.between(to: 1, from: 100),
389+
reply_count: Faker::Number.between(to: 1, from: 20),
390+
like_count: Faker::Number.between(to: 1, from: 25),
391+
quote_count: Faker::Number.between(to: 1, from: 10)
392+
},
393+
possibly_sensitive: Faker::Boolean.boolean(true_ratio: 0.1),
394+
entities: tweet_entities
395+
}
396+
end
386397
end
387398
end
388399
end

0 commit comments

Comments
 (0)