social-url 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d01b5270b58b9764104d86befbc364c0e54004d
4
+ data.tar.gz: 5125800db737d0cc20621730b854b8f49a8257bd
5
+ SHA512:
6
+ metadata.gz: a52bfabb0050d6aa28bf1ba9e6737338bb1c66f0ebdc65a7b2acb1aeda2bc805cb99d70b5da25025ffe4d0efe7d8c97036cac5c2dd1ff6e195947596b83b063d
7
+ data.tar.gz: 3034c2fb435822f2d8f8263270f433af7a6cc58f83a6a67cc8e10bd7f4089f07c84f40db510b79fe91aee700b151cbdd583e004e4796067ed260b9b0c21570f8
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ coverage
2
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ social-url (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ codeclimate-test-reporter (0.4.7)
10
+ simplecov (>= 0.7.1, < 1.0.0)
11
+ coderay (1.1.0)
12
+ docile (1.1.5)
13
+ ffi (1.9.10)
14
+ formatador (0.2.5)
15
+ guard (2.12.8)
16
+ formatador (>= 0.2.4)
17
+ listen (>= 2.7, <= 4.0)
18
+ lumberjack (~> 1.0)
19
+ nenv (~> 0.1)
20
+ notiffany (~> 0.0)
21
+ pry (>= 0.9.12)
22
+ shellany (~> 0.0)
23
+ thor (>= 0.18.1)
24
+ guard-compat (1.2.1)
25
+ guard-minitest (2.4.4)
26
+ guard-compat (~> 1.2)
27
+ minitest (>= 3.0)
28
+ json (1.8.3)
29
+ listen (3.0.2)
30
+ rb-fsevent (>= 0.9.3)
31
+ rb-inotify (>= 0.9)
32
+ lumberjack (1.0.9)
33
+ method_source (0.8.2)
34
+ minitest (5.7.0)
35
+ nenv (0.2.0)
36
+ notiffany (0.0.6)
37
+ nenv (~> 0.1)
38
+ shellany (~> 0.0)
39
+ pry (0.10.1)
40
+ coderay (~> 1.1.0)
41
+ method_source (~> 0.8.1)
42
+ slop (~> 3.4)
43
+ rake (10.4.2)
44
+ rb-fsevent (0.9.5)
45
+ rb-inotify (0.9.5)
46
+ ffi (>= 0.5.0)
47
+ shellany (0.0.1)
48
+ simplecov (0.10.0)
49
+ docile (~> 1.1.0)
50
+ json (~> 1.8)
51
+ simplecov-html (~> 0.10.0)
52
+ simplecov-html (0.10.0)
53
+ slop (3.6.0)
54
+ thor (0.19.1)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ codeclimate-test-reporter (~> 0.4)
61
+ guard (~> 2.12)
62
+ guard-minitest (~> 2.4)
63
+ rake (~> 10.4)
64
+ simplecov (~> 0.9)
65
+ social-url!
66
+
67
+ BUNDLED WITH
68
+ 1.10.5
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ clearing :on
2
+
3
+ guard :minitest do
4
+ watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
5
+ watch(%r{^test/.+_test\.rb$})
6
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
7
+ end
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # social_url
2
+
3
+ A simple gem to generate social media sharing links.
4
+ Supports multiple platforms with a plugin architecture.
5
+
6
+ ## Introduction
7
+
8
+ This gem was created to help quickly set up sharing links for web pages. This should help you create simple and fast sharing links to social media networks as opposed to performance-heavy third-party sharing widgets.
9
+
10
+ Read more about (https://jonsuh.com/blog/social-share-links/)[Responsible Social Share Links].
11
+
12
+ ## Installation
13
+
14
+ Add it to your Gemfile with:
15
+
16
+ ```ruby
17
+ gem 'social_url'
18
+ ```
19
+
20
+ Run the `bundle install` in your terminal command to install it.
21
+
22
+ ## Usage
23
+
24
+ The gem exposes two classes `SocialUrl` and `SocialUrl::Message`. The `SocialUrl` class provides normalization functionality for URL parameters and exposes the available networks:
25
+
26
+ ```ruby
27
+ SocialUrl.networks #=> [:twitter]
28
+
29
+ SocialUrl.normalize_string('Hello World') #=> 'Hello%20World'
30
+ SocialUrl.normalize_array(%w(nature sunset)) #=> 'nature,sunset'
31
+ SocialUrl.normalize_array(['twitter:Twitter News', 'twitterapi:Twitter API News']) #=> 'twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'
32
+ ```
33
+
34
+ Setup a new message to share on social networks using the `Message` class:
35
+
36
+ ```ruby
37
+ message = SocialUrl::Message.new({
38
+ text: 'Hello World',
39
+ url: 'http://example.com',
40
+ hashtags: %w(nature sunset)
41
+ })
42
+
43
+ message.twitter_url #=> 'https://twitter.com/intent/tweet/?text=Hello%20World&url=http%3A%2F%2Fexample.com&hashtags=nature,sunset'
44
+ ```
45
+
46
+ ### Available networks
47
+
48
+ - Facebook
49
+ - Twitter
50
+
51
+ ## TODO
52
+
53
+ This gem is a work-in-progress. Planned features:
54
+
55
+ - Ruby on Rails ActionView helpers
56
+
57
+ ## Contributing
58
+
59
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
60
+
61
+ - Report bugs
62
+ - Fix bugs and submit pull requests
63
+ - Write, clarify, or fix documentation
64
+ - Suggest or add new features
65
+ - Write missing tests
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << 'lib'
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.verbose = false
8
+ end
9
+
10
+ task default: :test
data/lib/social_url.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'erb'
2
+ require 'social_url/version'
3
+ require 'social_url/message'
4
+ require 'social_url/facebook'
5
+ require 'social_url/twitter'
6
+
7
+ module SocialUrl
8
+ include ERB::Util
9
+
10
+ class << self
11
+ NETWORKS = [:twitter]
12
+
13
+ def networks
14
+ NETWORKS
15
+ end
16
+
17
+ def normalize(options)
18
+ opts = {}
19
+
20
+ options.each do |key, value|
21
+ next unless value
22
+
23
+ opts[key] = normalize_string(value) if value.is_a?(String)
24
+ opts[key] = normalize_array(value) if value.is_a?(Array)
25
+ end
26
+
27
+ opts
28
+ end
29
+
30
+ def normalize_string(string)
31
+ ERB::Util.url_encode(string)
32
+ end
33
+
34
+ def normalize_array(array)
35
+ array.collect do |value|
36
+ ERB::Util.url_encode(value)
37
+ end.join(',')
38
+ end
39
+ end
40
+
41
+ class UnsupportedNetworkError < StandardError; end
42
+ end
@@ -0,0 +1,13 @@
1
+ module SocialUrl
2
+ class Facebook
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def url
8
+ params = "u=#{@options[:url]}"
9
+
10
+ "https://www.facebook.com/sharer/sharer.php?#{params}"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module SocialUrl
2
+ class Message
3
+ def initialize(options)
4
+ @options = SocialUrl.normalize(options)
5
+
6
+ init_networks
7
+ end
8
+
9
+ def method_missing(method)
10
+ network = /(.+)_url/.match(method)
11
+ return unless network
12
+
13
+ networks = SocialUrl.networks.join(',')
14
+ raise UnsupportedNetworkError,
15
+ "Unsupported network '#{network[1]}'. Choose from: #{networks}."
16
+ end
17
+
18
+ private
19
+
20
+ def init_networks
21
+ SocialUrl.networks.each do |network|
22
+ self.class.send(:define_method, "#{network}_url") do
23
+ klass = network.to_s.capitalize
24
+ SocialUrl.const_get(klass).new(@options).url
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module SocialUrl
2
+ class Twitter
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def url
8
+ params = @options.collect do |key, value|
9
+ "#{key}=#{value}"
10
+ end.join('&')
11
+
12
+ "https://twitter.com/intent/tweet/?#{params}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SocialUrl
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,24 @@
1
+ require './lib/social_url/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'social-url'
5
+ s.version = SocialUrl::VERSION
6
+ s.authors = ["Richard Venneman"]
7
+ s.email = 'richardvenneman@me.com'
8
+ s.homepage = 'https://github.com/richardvenneman/social-url'
9
+ s.summary = 'Generates social media sharing links'
10
+ s.description = 'A simple gem to generate social sharing links from your
11
+ messages. Supports multiple social platforms trough a plugin
12
+ architecture.'
13
+
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = Dir['test/**/*']
18
+
19
+ s.add_development_dependency 'rake', '~> 10.4'
20
+ s.add_development_dependency 'guard', '~> 2.12'
21
+ s.add_development_dependency 'guard-minitest', '~> 2.4'
22
+ s.add_development_dependency 'simplecov', '~> 0.9'
23
+ s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ module SocialUrl
4
+ class FacebookTest < Minitest::Test
5
+ def setup
6
+ @options = { url: 'http://example.com' }
7
+ end
8
+
9
+ def test_url
10
+ url = 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fexample.com'
11
+
12
+ opts = SocialUrl.normalize(@options)
13
+ assert_equal url, Facebook.new(opts).url
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module SocialUrl
4
+ class MessageTest < Minitest::Test
5
+ def test_exposes_network_methods
6
+ message = Message.new({})
7
+
8
+ SocialUrl.networks.each do |network|
9
+ method = "#{network}_url".to_sym
10
+ assert_respond_to message, method
11
+ end
12
+ end
13
+
14
+ def test_throws_on_invalid_network
15
+ message = Message.new({})
16
+
17
+ assert_raises UnsupportedNetworkError do
18
+ message.derp_url
19
+ end
20
+ end
21
+
22
+ def test_network_url
23
+ message = Message.new({
24
+ text: 'Hello World',
25
+ url: 'http://example.com',
26
+ hashtags: %w(nature sunset),
27
+ via: 'twitterdev',
28
+ related: ['twitter:Twitter News', 'twitterapi:Twitter API News']
29
+ })
30
+
31
+ url = ['https://twitter.com/intent/tweet/?text=Hello%20World',
32
+ '&url=http%3A%2F%2Fexample.com',
33
+ '&hashtags=nature,sunset',
34
+ '&via=twitterdev',
35
+ '&related=twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'].join
36
+
37
+ assert_equal url, message.twitter_url
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ module SocialUrl
4
+ class TwitterTest < Minitest::Test
5
+ def setup
6
+ @options = {
7
+ text: 'Hello World',
8
+ url: 'http://example.com',
9
+ hashtags: %w(nature sunset),
10
+ via: 'twitterdev',
11
+ related: ['twitter:Twitter News', 'twitterapi:Twitter API News']
12
+ }
13
+ end
14
+
15
+ def test_url
16
+ url = ['https://twitter.com/intent/tweet/?text=Hello%20World',
17
+ '&url=http%3A%2F%2Fexample.com',
18
+ '&hashtags=nature,sunset',
19
+ '&via=twitterdev',
20
+ '&related=twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'].join
21
+
22
+ opts = SocialUrl.normalize(@options)
23
+ assert_equal url, Twitter.new(opts).url
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class SocialUrlTest < Minitest::Test
4
+ def test_exposes_available_networks
5
+ assert_kind_of Array, SocialUrl.networks
6
+ assert_includes SocialUrl.networks, :twitter
7
+ end
8
+
9
+ def test_string_normalization
10
+ text = 'Hello%20World'
11
+
12
+ assert_equal text, SocialUrl.normalize_string('Hello World')
13
+ end
14
+
15
+ def test_array_normalization
16
+ array = %w(nature sunset)
17
+ text = 'nature,sunset'
18
+ complex_array = ['twitter:Twitter News', 'twitterapi:Twitter API News']
19
+ complex_text = 'twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'
20
+
21
+ assert_equal text, SocialUrl.normalize_array(array)
22
+ assert_equal complex_text, SocialUrl.normalize_array(complex_array)
23
+ end
24
+
25
+ def test_normalization
26
+ options = {
27
+ text: 'Hello World',
28
+ url: 'http://example.com/',
29
+ hashtags: %w(nature sunset),
30
+ via: 'twitterdev',
31
+ related: ['twitter:Twitter News', 'twitterapi:Twitter API News']
32
+ }
33
+
34
+ normalized_options = {
35
+ text: 'Hello%20World',
36
+ url: 'http%3A%2F%2Fexample.com%2F',
37
+ hashtags: 'nature,sunset',
38
+ via: 'twitterdev',
39
+ related: 'twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'
40
+ }
41
+
42
+ assert_equal normalized_options, SocialUrl.normalize(options)
43
+ end
44
+ end
@@ -0,0 +1,17 @@
1
+ # Coverage
2
+ if ENV['CODECLIMATE_REPO_TOKEN']
3
+ require 'codeclimate-test-reporter'
4
+ CodeClimate::TestReporter.start
5
+ elsif ENV['COVERAGE']
6
+ require 'simplecov'
7
+ SimpleCov.start do
8
+ add_group 'Libraries', 'lib'
9
+ end
10
+ end
11
+
12
+ # Source files
13
+ require './lib/social_url'
14
+
15
+ # Support
16
+ require 'minitest/autorun'
17
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Richard Venneman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
83
+ description: |-
84
+ A simple gem to generate social sharing links from your
85
+ messages. Supports multiple social platforms trough a plugin
86
+ architecture.
87
+ email: richardvenneman@me.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - Guardfile
96
+ - README.md
97
+ - Rakefile
98
+ - lib/social_url.rb
99
+ - lib/social_url/facebook.rb
100
+ - lib/social_url/message.rb
101
+ - lib/social_url/twitter.rb
102
+ - lib/social_url/version.rb
103
+ - social-url.gemspec
104
+ - test/lib/social_url/facebook_test.rb
105
+ - test/lib/social_url/message_test.rb
106
+ - test/lib/social_url/twitter_test.rb
107
+ - test/lib/social_url_test.rb
108
+ - test/test_helper.rb
109
+ homepage: https://github.com/richardvenneman/social-url
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.4.8
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Generates social media sharing links
133
+ test_files:
134
+ - test/lib/social_url/facebook_test.rb
135
+ - test/lib/social_url/message_test.rb
136
+ - test/lib/social_url/twitter_test.rb
137
+ - test/lib/social_url_test.rb
138
+ - test/test_helper.rb