Skip to main content

Social Media Parser gem

Blog post   •   Sep 10, 2014 08:28 GMT

The new Network feature on Mynewsdesk will introduce a lot of new ways you can manage a contact. For instance, adding social media profiles and links to each contact.

To keep the user input as easy as possible, we wanted people to basically just paste urls and then it would be parsed into social media profile objects it possible (we use some of them to fetch content from their API). Pasting links works well for social media profiles that aren't username heavy, like Facebook, but not for Twitter or Instagram, which is very username heavy, so the logical way to add a new profile for them would be to add a provider and username only.

We built this logic about a month prior to today, but needed it for two apps in our infrastructure, so we've been converting it and extending it to a gem, which we've published to rubygems today.

Here's how it works.

parser = SocialMediaParser.parse "https://www.facebook.com/teamcoco"
=> #<SocialMediaParser::SocialMedia::Facebook:0x007fe014ef0f78 @url="https://www.facebook.com/teamcoco"&gt;

parser.username
=> "teamcoco"

parser.provider
=> "facebook"

parser.url
=> "https://www.facebook.com/teamcoco"

If you instead want to construct a profile using provider and username options, you can use this

parser = SocialMediaParser.parse provider: "twitter", username: "fallontonight"
=> #<SocialMediaParser::SocialMedia::Twitter:0x007fe40ab08330 @provider="twitter", @username="fallontonight">

parser.url
=> "https://www.twitter.com/fallontonight"

When there's not enough input provided for SocialMediaParser to extract a valid profile, it will just return a Link object

parser = SocialMediaParser.parse url: "http://www.mynewsdesk.com"
=> #<SocialMediaParser::Link:0x007fe40ab9b770 @url="http://www.mynewsdesk.com"&gt;

parser.url
=> "http://www.mynewsdesk.com"

parser.username
=> nil

That's it, really. We hope to find this useful to someone!

Comments (2)

    Hi!

    Thanks for such a knowledgeable post.

    <a href="http://airul.id/cerita-lucu">Airul</a>

    - Market Research Vietnam - May 11, 2016 03:22 GMT

    Hi!

    Thanks for such a knowledgeable post.

    <a href="https://www.roofnic.com/android-restart-sendiri/">Android Restart Sendiri</a>

    - Inspiratif - Jan 30, 2017 17:03 GMT

Add comment

Comment