social-url 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 382bc516fb895000ad278ae48561ff9d814a2118
4
- data.tar.gz: 92dde830fec2519dd536d5719f710f30dcd921c3
3
+ metadata.gz: b53dbc1e3360fcc3106bb39f5fb0982f8b7f92d3
4
+ data.tar.gz: 1911e4958a6545999181821fc3f70e3dbf1e212c
5
5
  SHA512:
6
- metadata.gz: 82be3aa78314a4b6d72fd686b410e355c402b9774e17e439049cd2d7534b8e3617fd6255a4637b3d33d7523eba81d8c162a8f4e2ad7eb0cac249e2425163bdcd
7
- data.tar.gz: a1499e19fddcaef083ea5dd7599abe42d33acc337ee6ac5e7dc00ca564f44962d1d3b7c07f509e6a39b11a9b72670de179d471aae7294f530e2ddd7e852ce059
6
+ metadata.gz: 6374b509874ea4ec03d63d383de4ca34393ce22a1e67236105705aa453a4ef9f11210d4f4c1c455bd2147cc904b642d3f9bc5ccdc36e82d9eb1fc2c08b013523
7
+ data.tar.gz: 8007c1381a449eb81424b48a1f491e85e601ce102e9207cd5e84280aa2c3eadef1590b07cd365ddfc68b299ed1b1180e1638bc8afd26dde23a16dd31f31d083c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- social-url (0.3.0)
4
+ social-url (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -61,7 +61,6 @@ message.facebook_url #=> 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2
61
61
  This gem is a work-in-progress. Planned features:
62
62
 
63
63
  - Raise on missing required network parameters
64
- - Merge keys for networks (e.g. url, u)
65
64
  - Ruby on Rails ActionView helpers
66
65
 
67
66
  ## Contributing
data/lib/social_url.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'erb'
2
+
3
+ require 'social_url/errors'
2
4
  require 'social_url/version'
5
+
3
6
  require 'social_url/message'
4
7
  require 'social_url/facebook'
5
8
  require 'social_url/google'
@@ -11,6 +14,12 @@ module SocialUrl
11
14
 
12
15
  class << self
13
16
  NETWORKS = [:facebook, :google, :pinterest, :twitter]
17
+ KEYS = {
18
+ u: :url,
19
+ url: :u,
20
+ description: :text,
21
+ text: :description
22
+ }
14
23
 
15
24
  def networks
16
25
  NETWORKS
@@ -26,6 +35,16 @@ module SocialUrl
26
35
  opts[key] = normalize_array(value) if value.is_a?(Array)
27
36
  end
28
37
 
38
+ normalize_keys(opts)
39
+ end
40
+
41
+ def normalize_keys(options)
42
+ opts = options.dup
43
+
44
+ options.each do |key, value|
45
+ opts[KEYS[key]] = opts[key] if KEYS[key]
46
+ end
47
+
29
48
  opts
30
49
  end
31
50
 
@@ -38,7 +57,11 @@ module SocialUrl
38
57
  ERB::Util.url_encode(value)
39
58
  end.join(',')
40
59
  end
41
- end
42
60
 
43
- class UnsupportedNetworkError < StandardError; end
61
+ def filtered_params(options, params)
62
+ params.collect do |param|
63
+ "#{param}=#{options[param]}"
64
+ end.join('&')
65
+ end
66
+ end
44
67
  end
@@ -1,13 +1,13 @@
1
1
  module SocialUrl
2
2
  class Facebook
3
+ PARAMS = [:u]
4
+
3
5
  def initialize(options)
4
- @options = options
6
+ @params = SocialUrl.filtered_params(options, PARAMS)
5
7
  end
6
8
 
7
9
  def url
8
- params = "u=#{@options[:url]}"
9
-
10
- "https://www.facebook.com/sharer/sharer.php?#{params}"
10
+ "https://www.facebook.com/sharer/sharer.php?#{@params}"
11
11
  end
12
12
  end
13
13
  end
@@ -1,13 +1,13 @@
1
1
  module SocialUrl
2
2
  class Google
3
+ PARAMS = [:url]
4
+
3
5
  def initialize(options)
4
- @options = options
6
+ @params = SocialUrl.filtered_params(options, PARAMS)
5
7
  end
6
8
 
7
9
  def url
8
- params = "url=#{@options[:url]}"
9
-
10
- "https://plus.google.com/share?#{params}"
10
+ "https://plus.google.com/share?#{@params}"
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,13 @@
1
+ module SocialUrl
2
+ class Pinterest
3
+ PARAMS = [:url, :media, :description]
4
+
5
+ def initialize(options)
6
+ @params = SocialUrl.filtered_params(options, PARAMS)
7
+ end
8
+
9
+ def url
10
+ "https://www.pinterest.com/pin/create/button/?#{@params}"
11
+ end
12
+ end
13
+ end
@@ -1,15 +1,13 @@
1
1
  module SocialUrl
2
2
  class Twitter
3
+ PARAMS = [:text, :url, :hashtags, :via, :related]
4
+
3
5
  def initialize(options)
4
- @options = options
6
+ @params = SocialUrl.filtered_params(options, PARAMS)
5
7
  end
6
8
 
7
9
  def url
8
- params = @options.collect do |key, value|
9
- "#{key}=#{value}"
10
- end.join('&')
11
-
12
- "https://twitter.com/intent/tweet/?#{params}"
10
+ "https://twitter.com/intent/tweet/?#{@params}"
13
11
  end
14
12
  end
15
13
  end
@@ -1,3 +1,3 @@
1
1
  module SocialUrl
2
- VERSION = '0.3.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -2,39 +2,43 @@ require 'test_helper'
2
2
 
3
3
  module SocialUrl
4
4
  class MessageTest < Minitest::Test
5
- def test_exposes_network_methods
6
- message = Message.new({})
5
+ def setup
6
+ @message = Message.new({})
7
+ @complete_message = Message.new({
8
+ text: 'Hello World',
9
+ url: 'http://example.com',
10
+ hashtags: %w(nature sunset),
11
+ via: 'twitterdev',
12
+ related: ['twitter:Twitter News', 'twitterapi:Twitter API News']
13
+ })
14
+ end
15
+
16
+ def teardown
17
+ @message.destroy
18
+ @complete_message.destroy
19
+ end
7
20
 
21
+ def test_exposes_network_methods
8
22
  SocialUrl.networks.each do |network|
9
23
  method = "#{network}_url".to_sym
10
- assert_respond_to message, method
24
+ assert_respond_to @message, method
11
25
  end
12
26
  end
13
27
 
14
28
  def test_throws_on_invalid_network
15
- message = Message.new({})
16
-
17
29
  assert_raises UnsupportedNetworkError do
18
- message.derp_url
30
+ @message.derp_url
19
31
  end
20
32
  end
21
33
 
22
34
  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
35
  url = ['https://twitter.com/intent/tweet/?text=Hello%20World',
32
36
  '&url=http%3A%2F%2Fexample.com',
33
37
  '&hashtags=nature,sunset',
34
38
  '&via=twitterdev',
35
39
  '&related=twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'].join
36
40
 
37
- assert_equal url, message.twitter_url
41
+ assert_equal url, @complete_message.twitter_url
38
42
  end
39
43
  end
40
44
  end
@@ -1,6 +1,26 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SocialUrlTest < Minitest::Test
4
+ def setup
5
+ @options = {
6
+ text: 'Hello World',
7
+ url: 'http://example.com/',
8
+ hashtags: %w(nature sunset),
9
+ via: 'twitterdev',
10
+ related: ['twitter:Twitter News', 'twitterapi:Twitter API News']
11
+ }
12
+
13
+ @normalized_options = {
14
+ text: 'Hello%20World',
15
+ description: 'Hello%20World',
16
+ u: 'http%3A%2F%2Fexample.com%2F',
17
+ url: 'http%3A%2F%2Fexample.com%2F',
18
+ hashtags: 'nature,sunset',
19
+ via: 'twitterdev',
20
+ related: 'twitter%3ATwitter%20News,twitterapi%3ATwitter%20API%20News'
21
+ }
22
+ end
23
+
4
24
  def test_exposes_available_networks
5
25
  assert_kind_of Array, SocialUrl.networks
6
26
  assert_includes SocialUrl.networks, :twitter
@@ -23,22 +43,13 @@ class SocialUrlTest < Minitest::Test
23
43
  end
24
44
 
25
45
  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
- }
46
+ assert_equal @normalized_options, SocialUrl.normalize(@options)
47
+ end
33
48
 
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
- }
49
+ def test_filtered_params
50
+ filtered_params = 'text=Hello%20World&url=http%3A%2F%2Fexample.com%2F'
51
+ params = [:text, :url]
41
52
 
42
- assert_equal normalized_options, SocialUrl.normalize(options)
53
+ assert_equal filtered_params, SocialUrl.filtered_params(@normalized_options, params)
43
54
  end
44
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: social-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Venneman
@@ -100,6 +100,7 @@ files:
100
100
  - lib/social_url/facebook.rb
101
101
  - lib/social_url/google.rb
102
102
  - lib/social_url/message.rb
103
+ - lib/social_url/pinterest.rb
103
104
  - lib/social_url/twitter.rb
104
105
  - lib/social_url/version.rb
105
106
  - social-url.gemspec