A simple gem to generate social media sharing URLs.
Supported networks: Google+, Facebook, Pinterest, Twitter and mailto:
.
This gem was created to help you create social media sharing URLs. Create a message once and make it sharable on multiple networks with each their own specific parameters.
Use this to create fast sharing links as opposed to performance-heavy third-party sharing widgets.
Read more about Responsible Social Share Links (by @jonsuh).
Setup a new message to share on social networks using the Message
class:
message = SocialUrl::Message.new({
text: 'Hello World',
url: 'http://example.com',
hashtags: %w(nature sunset)
})
Use the message's network methods to get the sharing URLs:
facebook_url
google_url
mailto_url
pinterest_url
twitter_url
message.twitter_url #=> 'https://twitter.com/intent/tweet/?text=Hello%20World&url=http%3A%2F%2Fexample.com&hashtags=nature,sunset'
message.facebook_url #=> 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com'
...
This gem translates the options passed to the SocialUrl::Message
into the correct parameters for each network. Supported options are:
Network | Supported parameters |
---|---|
url (String) |
|
url (String) |
|
mailto | url (String)text (String) |
url (String)text (String)media (String) |
|
url (String)text (String)hashtags (Array of Strings)via (String)related (Array of Strings) |
The SocialUrl
class provides normalization functionality for URL parameters and exposes the available networks:
SocialUrl.networks #=> [:facebook, :google, :twitter, ...]
SocialUrl.normalize_string('Hello World') #=> 'Hello%20World'
SocialUrl.normalize_array(%w(nature sunset)) #=> 'nature,sunset'
SocialUrl.normalize_array(['twitter:Twitter News', 'twitterapi:Twitter API News']) #=> 'twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'
This library is test with the following Rubies: 2.3.8, 2.4.5, 2.5.3.
Add this line to your application's Gemfile:
gem 'social-url'
And then execute:
$ bundle
Or install it yourself as:
$ gem install social-url
I'm happy to accept any pull requests for new networks. Adding a new network is relatively easy. Say you'd want to add mynetwork
support:
- Create
lib/social_url/networks/mynetwork.rb
andtest/lib/social_url/networks/mynetwork_test.rb
- Add tests and implementation (take a look at the current networks)
This library is released under the MIT License.