Skip to content

Commit

Permalink
Improve Faker::Twitter compatibility (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham authored and stympy committed Feb 21, 2017
1 parent 0f3c187 commit b8450d5
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 46 deletions.
8 changes: 5 additions & 3 deletions doc/twitter.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Faker::Twitter

Generate realistic Twitter user and status objects like you would get back from the API.
Generate realistic Twitter user and status objects similar to what you would get back from the API.

```json
{
Expand All @@ -14,8 +14,10 @@ Generate realistic Twitter user and status objects like you would get back from

```ruby
Faker::Twitter.user #=> {:id=>8821452687517076614, :name=>"Lincoln Paucek", :screen_name=>"cody"...
Faker::Twitter.user(include_status: false)
Faker::Twitter.user(include_status: false) # Just get a user object with no embed status
Faker::Twitter.user(include_email: true) # Simulate an authenticated user with the email permission

Faker::Twitter.status #=> {:id=>8821452687517076614, :text=>"Ea et laboriosam vel non."...
Faker::Twitter.status(include_user: false)
Faker::Twitter.status(include_user: false) # Just get a status object with no embed user
Faker::Twitter.status(include_photo: true) # Includes entities for an attached image
```
153 changes: 110 additions & 43 deletions lib/faker/twitter.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,87 @@
module Faker
class Twitter < Base
class << self
def user(include_status: true)
def user(include_status: true, include_email: false)
user_id = id
background_image_url = Faker::LoremPixel.image('600x400') # TODO: Make the dimensions change
profile_image_url = Faker::Avatar.image(user_id, '48x48')
user = {
id: user_id,
id_str: user_id.to_s,
name: Faker::Name.name,
screen_name: Faker::Internet.user_name,
location: "#{Faker::Address.city}, #{Faker::Address.state_abbr}, #{Faker::Address.country_code}",
contributors_enabled: Faker::Boolean.boolean(0.1),
created_at: created_at,
default_profile_image: Faker::Boolean.boolean(0.1),
default_profile: Faker::Boolean.boolean(0.1),
description: Faker::Lorem.sentence,
url: Faker::Internet.url('example.com'),
entities: nil, # TODO
protected: Faker::Boolean.boolean(0.1),
entities: user_entities,
favourites_count: Faker::Number.between(1, 100_000),
follow_request_sent: false,
followers_count: Faker::Number.between(1, 10_000_000),
following: false,
friends_count: Faker::Number.between(1, 100_000),
listed_count: Faker::Number.between(1, 1000),
created_at: created_at,
favourites_count: Faker::Number.between(1, 100_000),
utc_offset: utc_offset,
time_zone: Faker::Address.time_zone,
geo_enabled: Faker::Boolean.boolean(0.1),
verified: Faker::Boolean.boolean(0.1),
statuses_count: Faker::Number.between(1, 100_000),
lang: Faker::Address.country_code,
contributors_enabled: Faker::Boolean.boolean(0.1),
is_translator: Faker::Boolean.boolean(0.1),
is_translation_enabled: Faker::Boolean.boolean(0.1),
is_translator: Faker::Boolean.boolean(0.1),
lang: Faker::Address.country_code,
listed_count: Faker::Number.between(1, 1000),
location: "#{Faker::Address.city}, #{Faker::Address.state_abbr}, #{Faker::Address.country_code}",
name: Faker::Name.name,
notifications: false,
profile_background_color: Faker::Color.hex_color,
profile_background_image_url: background_image_url.sub('https://', 'http://'),
profile_background_image_url_https: background_image_url,
profile_background_image_url: background_image_url.sub('https://', 'http://'),
profile_background_tile: Faker::Boolean.boolean(0.1),
profile_image_url: profile_image_url.sub('https://', 'http://'),
profile_image_url_https: profile_image_url,
profile_banner_url: Faker::LoremPixel.image('1500x500'),
profile_image_url_https: profile_image_url,
profile_image_url: profile_image_url.sub('https://', 'http://'),
profile_link_color: Faker::Color.hex_color,
profile_sidebar_border_color: Faker::Color.hex_color,
profile_sidebar_fill_color: Faker::Color.hex_color,
profile_text_color: Faker::Color.hex_color,
profile_use_background_image: Faker::Boolean.boolean(0.4),
default_profile: Faker::Boolean.boolean(0.1),
default_profile_image: Faker::Boolean.boolean(0.1),
following: false,
follow_request_sent: false,
notifications: false
protected: Faker::Boolean.boolean(0.1),
screen_name: Faker::Internet.user_name(nil, ['_']),
statuses_count: Faker::Number.between(1, 100_000),
time_zone: Faker::Address.time_zone,
url: Faker::Internet.url('example.com'),
utc_offset: utc_offset,
verified: Faker::Boolean.boolean(0.1)
}
user[:status] = Faker::Twitter.status(include_user: false) if include_status
user[:email] = Faker::Internet.safe_email if include_email
user
end

def status(include_user: true)
def status(include_user: true, include_photo: false)
status_id = id
status = {
created_at: created_at,
id: status_id,
id_str: status_id.to_s,
text: Faker::Lorem.sentence,
truncated: false,
entities: nil, # TODO
source: "<a href=\"#{Faker::Internet.url('example.com')}\" rel=\"nofollow\">#{Faker::Company.name}</a>",
contributors: nil,
coordinates: nil,
created_at: created_at,
entities: status_entities(include_photo: include_photo),
favorite_count: Faker::Number.between(1, 10_000),
favorited: false,
geo: nil,
in_reply_to_screen_name: nil,
in_reply_to_status_id: nil,
nil: nil,
in_reply_to_user_id: nil,
in_reply_to_user_id_str: nil,
in_reply_to_screen_name: nil,
geo: nil,
coordinates: nil,
place: nil,
contributors: nil,
retweeted_status: nil,
in_reply_to_user_id: nil,
is_quote_status: false,
lang: Faker::Address.country_code,
nil: nil,
place: nil,
possibly_sensitive: Faker::Boolean.boolean(0.1),
retweet_count: Faker::Number.between(1, 10_000),
favorite_count: Faker::Number.between(1, 10_000),
favorited: false,
retweeted_status: nil,
retweeted: false,
possibly_sensitive: Faker::Boolean.boolean(0.1),
lang: Faker::Address.country_code
source: "<a href=\"#{Faker::Internet.url('example.com')}\" rel=\"nofollow\">#{Faker::Company.name}</a>",
text: Faker::Lorem.sentence,
truncated: false
}
status[:user] = Faker::Twitter.user(include_status: false) if include_user
status[:text] = "#{status[:text]} #{status[:entities][:media].first[:url]}" if include_photo
status
end

Expand All @@ -96,6 +98,71 @@ def created_at
def utc_offset
Faker::Number.between(-43_200, 50_400)
end

def user_entities
{
url: {
urls: []
},
description: {
urls: []
}
}
end

def status_entities(include_photo: false)
entities = {
hashtags: [],
symbols: [],
user_mentions: [],
urls: []
}
entities[:media] = [photo_entity] if include_photo
entities
end

def photo_entity
# TODO: Dynamic image sizes
# TODO: Return accurate indices
media_url = Faker::LoremPixel.image('1064x600')
media_id = id
{
id: media_id,
id_str: media_id.to_s,
indices: [
103,
126
],
media_url: media_url.sub('https://', 'http://'),
media_url_https: media_url,
url: Faker::Internet.url('example.com'),
display_url: 'example.com',
expanded_url: Faker::Internet.url('example.com'),
type: 'photo',
sizes: {
medium: {
w: 1064,
h: 600,
resize: 'fit'
},
large: {
w: 1064,
h: 600,
resize: 'fit'
},
small: {
w: 680,
h: 383,
resize: 'fit'
},
thumb: {
w: 150,
h: 150,
resize: 'crop'
}
}
}
end
end
end
end
17 changes: 17 additions & 0 deletions test/test_twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def test_user
assert_nil user[:status][:user]
end

def test_user_with_email
user = @tester.user(include_email: true)
assert user.is_a?(Hash)
assert user.keys.count == 42
assert user[:email].is_a?(String)
end

def test_user_without_status
user = @tester.user(include_status: false)
assert user.is_a?(Hash)
Expand All @@ -24,6 +31,7 @@ def test_status
status = @tester.status
assert status.is_a?(Hash)
assert status.keys.count == 25
assert status[:entities].is_a?(Hash)
assert status[:user].is_a?(Hash)
assert_nil status[:user][:status]
end
Expand All @@ -34,4 +42,13 @@ def test_status_without_user
assert status.keys.count == 24
assert_nil status[:user]
end

def test_status_with_photo
status = @tester.status(include_photo: true)
assert status.is_a?(Hash)
assert status.keys.count == 25
assert status[:entities].is_a?(Hash)
assert status[:entities][:media].count == 1
assert status[:entities][:media].first.keys.count == 10
end
end

0 comments on commit b8450d5

Please sign in to comment.