Skip to content

Commit 9b52e23

Browse files
Address review comments
1 parent 6dc25bd commit 9b52e23

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-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: 58 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,38 +277,21 @@ 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

311-
includes[:users] = Faker::X.user[:includes][:users] if include_user
290+
includes[:users] = user[:includes][:users] if include_user
291+
292+
tweet[:data] = [
293+
tweet_object
294+
]
312295

313296
unless includes.empty?
314297
tweet[:includes] = includes
@@ -337,52 +320,82 @@ 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')
330+
def user_entities
331+
entities = tweet_entities
349332

350333
{
351334
url: {
352-
urls: [
353-
{
354-
url: url,
355-
expanded_url: display_url,
356-
display_url: display_url.sub('http://', '')
357-
}
358-
]
335+
urls: entities[:urls]
359336
},
360337
description: {
361-
hashtags: [
362-
{
363-
tag: Faker::Lorem.word.capitalize
364-
}
365-
]
338+
hashtags: entities[:hashtags]
366339
}
367340
}
368341
end
369342

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

373369
[{
374-
type: 'photo',
375-
indices: [103, 126],
376370
height: Faker::Number.between(to: 1, from: 1000),
377371
media_key: media_key,
372+
type: 'photo',
378373
preview_image_url: expanded_url,
379374
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://', ''),
383375
alt_text: Faker::Lorem.sentence
384376
}]
385377
end
378+
379+
def tweet_item
380+
conversation_id = id.to_s
381+
382+
{
383+
id: conversation_id,
384+
text: Faker::Lorem.sentence,
385+
lang: sample(%w[en fr es pt it ja]),
386+
conversation_id: conversation_id,
387+
created_at: created_at,
388+
author_id: Faker::Number.between(from: 1, to: 9_223_372_036_854_775_807).to_s,
389+
public_metrics: {
390+
retweet_count: Faker::Number.between(to: 1, from: 100),
391+
reply_count: Faker::Number.between(to: 1, from: 20),
392+
like_count: Faker::Number.between(to: 1, from: 25),
393+
quote_count: Faker::Number.between(to: 1, from: 10)
394+
},
395+
possibly_sensitive: Faker::Boolean.boolean(true_ratio: 0.1),
396+
entities: tweet_entities
397+
}
398+
end
386399
end
387400
end
388401
end

0 commit comments

Comments
 (0)