social_media_parser 0.1.0

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: 846a6fdd025433f2116c000b3a70c564b5edc479
4
+ data.tar.gz: 6fb70ea26a18a132fe49e68da6d25b1dde9f2213
5
+ SHA512:
6
+ metadata.gz: bf4bc7df352688cbdd8a5a4af09cb77e167fe0de54801699824e152d056034dc6bb394e07119603a615058b0426cd7c49d7f80a86ebd0c348ece4225bc402e99
7
+ data.tar.gz: b55815fb373e7b9f70cd4f50fa6848a0850f639b253c64fbbbb1ebc0c750c86f6200d322add345c93b0b639bc341287de123293c0d7f6271620eaaa9a817562c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in social_media_parser.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Markus Nordin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # SocialMediaParser
2
+
3
+ Parse social media attributes from url or construct url from attributes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'social_media_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install social_media_parser
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ parser = SocialMediaParser.parse "https://www.facebook.com/teamcoco"
25
+ => #<SocialMediaParser::SocialMedia::Facebook:0x007fe014ef0f78 @url="https://www.facebook.com/teamcoco">
26
+
27
+ parser.username
28
+ => "teamcoco"
29
+
30
+ parser.provider
31
+ => "facebook"
32
+
33
+ parser.url
34
+ => "https://www.facebook.com/teamcoco"
35
+ ```
36
+
37
+ `SocialMediaParser#parse` accepts either a url string or a hash, that accepts
38
+
39
+ ```
40
+ {username: "teamcoco", provider: "facebook", url: "https://www.facebook.com/teamcoco", url_or_username: "teamcoco"}
41
+ ```
42
+
43
+ The `url_or_username` option can be used when you're not sure of the input, like the screenshot below for instance. This gem is built to take user input directly.
44
+
45
+ ![screen shot 2014-09-06 at 21 49 51](https://cloud.githubusercontent.com/assets/28260/4176355/4ea9524a-35ff-11e4-86e2-27407beef42c.png)
46
+
47
+
48
+ If the input provided isn't enough for SocialMediaParser to figure out which provider it is, it returns a `SocialMediaParser::Link` object instead.
49
+
50
+ ```
51
+ link = SocialMediaParser.parse "www.ruby-lang.org/en/"
52
+ => #<SocialMediaParser::Link:0x007fe014fd8350 @url="https://www.ruby-lang.org/en/">
53
+
54
+ link.url
55
+ => "http://www.ruby-lang.org/en/"
56
+ ```
57
+
58
+ The `url` method will always return a clean url, prepending http schema if needed and validating the top domain, using public_suffix.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,49 @@
1
+ require 'public_suffix'
2
+
3
+ module SocialMediaParser
4
+ class Link
5
+ def initialize(attrs)
6
+ attrs.each do |k,v|
7
+ instance_variable_set("@#{k}", v) unless v.nil?
8
+ end
9
+ end
10
+
11
+ def username
12
+ nil
13
+ end
14
+
15
+ def provider
16
+ nil
17
+ end
18
+
19
+ def url
20
+ url_from_attributes
21
+ end
22
+
23
+ def attributes
24
+ {
25
+ url: url,
26
+ provider: provider,
27
+ username: username,
28
+ }
29
+ end
30
+
31
+ private
32
+
33
+ def url_from_attributes
34
+ valid_url_format(@url) or valid_url_format(@url_or_username)
35
+ end
36
+
37
+ def valid_url_format(url)
38
+ uri = URI.parse(url)
39
+ return uri.to_s if %w(http https).include?(uri.scheme)
40
+ return "http://#{url}" if PublicSuffix.valid?(URI.parse("http://#{url}").host)
41
+ rescue URI::BadURIError, URI::InvalidURIError
42
+ nil
43
+ end
44
+
45
+ def invalid_url_format?(url)
46
+ !valid_url_format url
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Facebook < Provider
6
+ URL_REGEX = /(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/[\w\-]*)?(?:[?\d\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-\.]*)?/i
7
+
8
+ def provider
9
+ 'facebook'
10
+ end
11
+
12
+ private
13
+
14
+ def parse_username_from_url
15
+ URL_REGEX.match(url_from_attributes).to_a[1]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Github < Provider
6
+
7
+ def provider
8
+ 'github'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Google < Provider
6
+ URL_REGEX = /(?:(?:http|https):\/\/)plus.google.com\/?(?:u\/\d{1,}\/|)(?:\+|)([\w\-\.\%]{1,})/i
7
+
8
+ def provider
9
+ 'google'
10
+ end
11
+
12
+ def url
13
+ return url_from_attributes if url_from_attributes
14
+ if username
15
+ if Float(username)
16
+ "https://plus.google.com/#{username}"
17
+ end
18
+ end
19
+ rescue ArgumentError
20
+ "https://plus.google.com/+#{username}"
21
+ end
22
+
23
+ private
24
+
25
+ def parse_username_from_url
26
+ URL_REGEX.match(url_from_attributes).to_a[1]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Instagram < Provider
6
+
7
+ def provider
8
+ 'instagram'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Pinterest < Provider
6
+
7
+ def provider
8
+ 'pinterest'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,48 @@
1
+ require 'social_media_parser/link'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Provider < ::SocialMediaParser::Link
6
+ PROVIDERS = ['facebook', 'github', 'google', 'instagram', 'pinterest', 'twitter', 'youtube']
7
+
8
+ def self.parse(attributes)
9
+ PROVIDERS.map do |provider|
10
+ Object.const_get("SocialMediaParser").const_get("SocialMedia").const_get(provider.capitalize).new(attributes)
11
+ end.select(&:valid?).first or
12
+ ::SocialMediaParser::Link.new(attributes)
13
+ end
14
+
15
+ def username
16
+ return @username if @username
17
+ if @url_or_username and invalid_url_format? @url_or_username
18
+ @url_or_username
19
+ elsif url_from_attributes
20
+ parse_username_from_url
21
+ end
22
+ end
23
+
24
+ def url
25
+ return url_from_attributes if url_from_attributes
26
+ "https://www.#{provider}.com/#{username}"
27
+ end
28
+
29
+ def valid?
30
+ (@provider and @provider.downcase == provider) or
31
+ (username and URI.parse(url_from_attributes).host.match("#{provider}.com"))
32
+ rescue URI::BadURIError, URI::InvalidURIError
33
+ false
34
+ end
35
+
36
+ private
37
+
38
+ # Common social media url format, like https://www.twitter.com/teamcoco
39
+ # Overwrite this in subclasses when social media url formatting
40
+ # doesn't look like this
41
+ def parse_username_from_url
42
+ URI.parse(url_from_attributes).path.split("/")[1]
43
+ rescue URI::BadURIError, URI::InvalidURIError
44
+ nil
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Twitter < Provider
6
+ URL_REGEX = /(?:(?:http|https):\/\/)?(?:www.)?twitter.com\/(?:(?:\w)*#!\/)?(\w*)/i
7
+
8
+ def provider
9
+ 'twitter'
10
+ end
11
+
12
+ private
13
+
14
+ def parse_username_from_url
15
+ URL_REGEX.match(url_from_attributes).to_a[1]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'social_media_parser/social_media/provider'
2
+
3
+ module SocialMediaParser
4
+ module SocialMedia
5
+ class Youtube < Provider
6
+ URL_REGEX = /(?:(?:http|https):\/\/)?(?:www.)?youtube\.com\/(?:user\/)([\w\-\.]{1,})/i
7
+
8
+ def provider
9
+ 'youtube'
10
+ end
11
+
12
+ def url
13
+ "https://www.youtube.com/user/#{username}"
14
+ end
15
+
16
+ private
17
+
18
+ def parse_username_from_url
19
+ URL_REGEX.match(url_from_attributes).to_a[1]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module SocialMediaParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'ostruct'
2
+ Dir[File.join(File.dirname(__FILE__), 'social_media_parser', 'social_media', '*.rb')].each {|file| require file }
3
+
4
+ module SocialMediaParser
5
+ def self.parse(attrs)
6
+ if attrs.is_a? String
7
+ return parse(url: attrs)
8
+ end
9
+ SocialMedia::Provider.parse(attrs)
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'social_media_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "social_media_parser"
8
+ spec.version = SocialMediaParser::VERSION
9
+ spec.authors = ["Markus Nordin", "Jonas Brusman", "Alexander Rothe"]
10
+ spec.email = ["dev@mynewsdesk.com"]
11
+ spec.summary = %q{Parse social media attributes from url or construct url from attributes}
12
+ spec.homepage = "http://devcorner.mynewsdesk.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "public_suffix", "~> 1.4.5"
21
+
22
+ spec.add_development_dependency "rspec", "~> 3.0.0"
23
+ spec.add_development_dependency "pry"
24
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser){ described_class.parse profile_attributes }
5
+
6
+ context "with just an url" do
7
+ context "with a valid url" do
8
+ let(:profile_attributes){ {url: "http://www.url.com"} }
9
+
10
+ it "returns the parsed attributes" do
11
+ expect(parser.url).to eq "http://www.url.com"
12
+ expect(parser.provider).to be_nil
13
+ expect(parser.username).to be_nil
14
+ end
15
+ end
16
+ end
17
+
18
+ context "with an unknown provider" do
19
+ let(:result){ described_class.new(profile_attributes).attributes }
20
+ let(:profile_attributes){ {url: "http://unknown.com/john_snow", provider: "unknown", username: "john_snow"} }
21
+
22
+ it "returns nil" do
23
+ expect(parser.url).to eq "http://unknown.com/john_snow"
24
+ expect(parser.provider).to eq nil
25
+ expect(parser.username).to eq nil
26
+ end
27
+ end
28
+
29
+ context "#url" do
30
+ context "with a valid url" do
31
+ let(:profile_attributes){ {url: "http://www.url.com"} }
32
+
33
+ it "returns the url" do
34
+ expect(parser.url).to eq "http://www.url.com"
35
+ end
36
+ end
37
+
38
+ context "with an url missing scheme" do
39
+ let(:profile_attributes){ {url: "www.url.com"} }
40
+
41
+ it "returns the url prefixed with http" do
42
+ expect(parser.url).to eq "http://www.url.com"
43
+ end
44
+ end
45
+
46
+ context "with an url missing scheme without www" do
47
+ let(:profile_attributes){ {url: "url.com"} }
48
+
49
+ it "returns the url prefixed with http" do
50
+ expect(parser.url).to eq "http://url.com"
51
+ end
52
+ end
53
+
54
+ context "with an url containing path" do
55
+ let(:profile_attributes){ {url: "url.com/epic/lol"} }
56
+
57
+ it "returns the url with http" do
58
+ expect(parser.url).to eq "http://url.com/epic/lol"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser) { described_class.parse(profile_attributes) }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://www.facebook.com/teamcoco"} }
8
+
9
+ it "returns a Facebook object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Facebook
11
+ end
12
+ end
13
+
14
+ context "with facebook url and provider" do
15
+ let(:profile_attributes){ {url: "https://facebook.com/awesome_random_dude", provider: "facebook"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://facebook.com/awesome_random_dude"
19
+ expect(parser.provider).to eq "facebook"
20
+ expect(parser.username).to eq "awesome_random_dude"
21
+ end
22
+ end
23
+
24
+ context "with facebook profile_id url and provider" do
25
+ let(:profile_attributes){ {url: "https://www.facebook.com/profile.php?id=644727125&fref=nf", provider: "facebook"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.facebook.com/profile.php?id=644727125&fref=nf"
29
+ expect(parser.provider).to eq "facebook"
30
+ expect(parser.username).to eq "644727125"
31
+ end
32
+ end
33
+
34
+ context "with facebook username as url_or_username and provider" do
35
+ let(:profile_attributes){ {url_or_username: "john.snow", provider: "facebook"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://www.facebook.com/john.snow"
39
+ expect(parser.provider).to eq "facebook"
40
+ expect(parser.username).to eq "john.snow"
41
+ end
42
+ end
43
+
44
+ context "with facebook http url as url_or_username and case insensitive provider" do
45
+ let(:profile_attributes){ {url_or_username: "http://www.facebook.com/john.snow", provider: "Facebook"} }
46
+
47
+ it "returns the parsed attributes" do
48
+ expect(parser.url).to eq "http://www.facebook.com/john.snow"
49
+ expect(parser.provider).to eq "facebook"
50
+ expect(parser.username).to eq "john.snow"
51
+ end
52
+ end
53
+
54
+ context "url variations" do
55
+ it "parses username from url with trailing slash" do
56
+ parser = described_class.parse "https://www.facebook.com/teamcoco/"
57
+ expect(parser.username).to eq "teamcoco"
58
+ end
59
+
60
+ it "parses username from url without www" do
61
+ parser = described_class.parse "https://facebook.com/teamcoco"
62
+ expect(parser.username).to eq "teamcoco"
63
+ end
64
+
65
+ it "parses username from url without http" do
66
+ parser = described_class.parse "www.facebook.com/teamcoco"
67
+ expect(parser.username).to eq "teamcoco"
68
+ end
69
+
70
+ it "parses username from url without http and www" do
71
+ parser = described_class.parse "facebook.com/teamcoco"
72
+ expect(parser.username).to eq "teamcoco"
73
+ end
74
+
75
+ it "parses username from photo stream page url" do
76
+ parser = described_class.parse "https://www.facebook.com/teamcoco/photos_stream"
77
+ expect(parser.username).to eq "teamcoco"
78
+ end
79
+
80
+ it "parses username from photo url" do
81
+ parser = described_class.parse "https://www.facebook.com/teamcoco/photos/pb.108905269168364.-2207520000.1409669893./757027907689427/?type=3&theater"
82
+ expect(parser.username).to eq "teamcoco"
83
+ end
84
+
85
+ it "parses username from facebook pages with username url" do
86
+ parser = described_class.parse "https://www.facebook.com/natten4ever"
87
+ expect(parser.username).to eq "natten4ever"
88
+ end
89
+
90
+ # Usernames derived from Facebook pages urls without username uses id as unique handler
91
+ # Since page name isn't usable in the Graph API, we chose to return the unique id instead
92
+ context "old facebook pages urls" do
93
+ it "parses Facebook ID as username" do
94
+ parser = described_class.parse "https://www.facebook.com/pages/Stiftelsen-Expo/208751565805849"
95
+ expect(parser.username).to eq "208751565805849"
96
+ end
97
+
98
+ it "parses Facebook ID as username from the about page" do
99
+ parser = described_class.parse "https://www.facebook.com/pages/Stiftelsen-Expo/208751565805849?sk=info"
100
+ expect(parser.username).to eq "208751565805849"
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser) { described_class.parse(profile_attributes) }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://github.com/mynewsdesk"} }
8
+
9
+ it "returns a Github object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Github
11
+ end
12
+ end
13
+
14
+ context "with github url and provider" do
15
+ let(:profile_attributes){ {url: "https://github.com/mynewsdesk", provider: "github"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://github.com/mynewsdesk"
19
+ expect(parser.provider).to eq "github"
20
+ expect(parser.username).to eq "mynewsdesk"
21
+ end
22
+ end
23
+
24
+ context "with github as provider and username as url_or_username" do
25
+ let(:profile_attributes){ {url_or_username: "mynewsdesk", provider: "github"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.github.com/mynewsdesk"
29
+ expect(parser.provider).to eq "github"
30
+ expect(parser.username).to eq "mynewsdesk"
31
+ end
32
+ end
33
+
34
+ context "with github as provider and url as url_or_username" do
35
+ let(:profile_attributes){ {url_or_username: "https://github.com/mynewsdesk", provider: "github"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://github.com/mynewsdesk"
39
+ expect(parser.provider).to eq "github"
40
+ expect(parser.username).to eq "mynewsdesk"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url with trailing slash" do
46
+ parser = described_class.parse "https://github.com/teamcoco/"
47
+ expect(parser.username).to eq "teamcoco"
48
+ end
49
+
50
+ it "parses username from url without www" do
51
+ parser = described_class.parse "https://github.com/mynewsdesk"
52
+ expect(parser.username).to eq "mynewsdesk"
53
+ end
54
+
55
+ it "parses username from url without http" do
56
+ parser = described_class.parse "www.github.com/mynewsdesk"
57
+ expect(parser.username).to eq "mynewsdesk"
58
+ end
59
+
60
+ it "parses username from url without http and www" do
61
+ parser = described_class.parse "github.com/mynewsdesk"
62
+ expect(parser.username).to eq "mynewsdesk"
63
+ end
64
+
65
+ it "parses username from repo url" do
66
+ parser = described_class.parse "https://github.com/mynewsdesk/social_media_parser"
67
+ expect(parser.username).to eq "mynewsdesk"
68
+ end
69
+
70
+ it "parses username from repo open issues url" do
71
+ parser = described_class.parse "https://github.com/mynewsdesk/social_media_parser/issues?q=is%3Aopen+is%3Aissue"
72
+ expect(parser.username).to eq "mynewsdesk"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser) { described_class.parse(profile_attributes) }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://plus.google.com/+TeamCoco"} }
8
+
9
+ it "returns a Google object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Google
11
+ end
12
+ end
13
+
14
+ context "with google plus url as url_or_username and provider" do
15
+ let(:profile_attributes){ {url_or_username: "https://plus.google.com/+TeamCoco/posts", provider: "google"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://plus.google.com/+TeamCoco/posts"
19
+ expect(parser.provider).to eq "google"
20
+ expect(parser.username).to eq "TeamCoco"
21
+ end
22
+ end
23
+
24
+ context "with google provider and custom username" do
25
+ let(:profile_attributes){ {username: "TeamCoco", provider: "google"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://plus.google.com/+TeamCoco"
29
+ expect(parser.provider).to eq "google"
30
+ expect(parser.username).to eq "TeamCoco"
31
+ end
32
+ end
33
+
34
+ context "with google provider and numeric id as username" do
35
+ let(:profile_attributes){ {username: "105063820137409755625", provider: "google"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://plus.google.com/105063820137409755625"
39
+ expect(parser.provider).to eq "google"
40
+ expect(parser.username).to eq "105063820137409755625"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url with trailing slash" do
46
+ parser = described_class.parse "plus.google.com/+TeamCoco/"
47
+ expect(parser.username).to eq "TeamCoco"
48
+ end
49
+
50
+ it "parses username from url without http" do
51
+ parser = described_class.parse "plus.google.com/+TeamCoco"
52
+ expect(parser.username).to eq "TeamCoco"
53
+ end
54
+
55
+ it "parses username from post url and multi user login" do
56
+ parser = described_class.parse "https://plus.google.com/u/1/+TeamCoco/posts/B1n3wkASqno"
57
+ expect(parser.username).to eq "TeamCoco"
58
+ end
59
+
60
+ it "parses username from photo url" do
61
+ parser = described_class.parse "https://plus.google.com/+TeamCoco/photos/photo/6049107252378797394?pid=6049107252378797394&oid=105163107119743094340"
62
+ expect(parser.username).to eq "TeamCoco"
63
+ end
64
+
65
+ it "parses username from multi user login and with special characters" do
66
+ parser = described_class.parse "https://plus.google.com/u/0/+KristoferBj%C3%B6rkman/posts"
67
+ expect(parser.username).to eq "KristoferBj%C3%B6rkman"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser){ described_class.parse profile_attributes }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://instagram.com/jimmykimmellive"} }
8
+
9
+ it "returns a Instagram object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Instagram
11
+ end
12
+ end
13
+
14
+ context "with instagram url and provider" do
15
+ let(:profile_attributes){ {url: "https://instagram.com/jimmykimmellive", provider: "instagram"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://instagram.com/jimmykimmellive"
19
+ expect(parser.provider).to eq "instagram"
20
+ expect(parser.username).to eq "jimmykimmellive"
21
+ end
22
+ end
23
+
24
+ context "with instagram as provider and username as url_or_username" do
25
+ let(:profile_attributes){ {url_or_username: "jimmykimmellive", provider: "instagram"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.instagram.com/jimmykimmellive"
29
+ expect(parser.provider).to eq "instagram"
30
+ expect(parser.username).to eq "jimmykimmellive"
31
+ end
32
+ end
33
+
34
+ context "with instagram as provider and username as url_or_username" do
35
+ let(:profile_attributes){ {url_or_username: "https://instagram.com/jimmykimmellive", provider: "instagram"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://instagram.com/jimmykimmellive"
39
+ expect(parser.provider).to eq "instagram"
40
+ expect(parser.username).to eq "jimmykimmellive"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url with trailing slash" do
46
+ parser = described_class.parse "http://instagram.com/jimmyfallon/"
47
+ expect(parser.username).to eq "jimmyfallon"
48
+ end
49
+
50
+ it "parses username from url with www" do
51
+ parser = described_class.parse "http://www.instagram.com/jimmyfallon"
52
+ expect(parser.username).to eq "jimmyfallon"
53
+ end
54
+
55
+ it "parses username from url without http and www" do
56
+ parser = described_class.parse "instagram.com/jimmyfallon"
57
+ expect(parser.username).to eq "jimmyfallon"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser){ described_class.parse profile_attributes }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://pinterest.com/fallontonight"} }
8
+
9
+ it "returns a Pinterest object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Pinterest
11
+ end
12
+ end
13
+
14
+ context "with pinterest url and provider" do
15
+ let(:profile_attributes){ {url: "https://pinterest.com/fallontonight", provider: "pinterest"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://pinterest.com/fallontonight"
19
+ expect(parser.provider).to eq "pinterest"
20
+ expect(parser.username).to eq "fallontonight"
21
+ end
22
+ end
23
+
24
+ context "with pinterest as provider and username as url_or_username" do
25
+ let(:profile_attributes){ {url_or_username: "fallontonight", provider: "pinterest"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.pinterest.com/fallontonight"
29
+ expect(parser.provider).to eq "pinterest"
30
+ expect(parser.username).to eq "fallontonight"
31
+ end
32
+ end
33
+
34
+ context "with pinterest as provider and username as url_or_username" do
35
+ let(:profile_attributes){ {url_or_username: "https://www.pinterest.com/fallontonight", provider: "pinterest"} }
36
+
37
+ it "returns nil" do
38
+ expect(parser.url).to eq "https://www.pinterest.com/fallontonight"
39
+ expect(parser.provider).to eq "pinterest"
40
+ expect(parser.username).to eq "fallontonight"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url without trailing slash" do
46
+ parser = described_class.parse "http://www.pinterest.com/marthastewart"
47
+ expect(parser.username).to eq "marthastewart"
48
+ end
49
+
50
+ it "parses username from url without www" do
51
+ parser = described_class.parse "http://pinterest.com/marthastewart/"
52
+ expect(parser.username).to eq "marthastewart"
53
+ end
54
+
55
+ it "parses username from pinterest pinboard url" do
56
+ parser = described_class.parse "http://www.pinterest.com/marthastewart/around-my-farm/"
57
+ expect(parser.username).to eq "marthastewart"
58
+ end
59
+
60
+ it "parses username from pinterest followers url" do
61
+ parser = described_class.parse "http://www.pinterest.com/marthastewart/followers/"
62
+ expect(parser.username).to eq "marthastewart"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser){ described_class.parse profile_attributes }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://www.twitter.com/TheDailyShow"} }
8
+
9
+ it "returns a Twitter object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Twitter
11
+ end
12
+ end
13
+
14
+ context "with twitter as provider and username as url_or_username" do
15
+ let(:profile_attributes){ {url_or_username: "TheDailyShow", provider: "twitter"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://www.twitter.com/TheDailyShow"
19
+ expect(parser.provider).to eq "twitter"
20
+ expect(parser.username).to eq "TheDailyShow"
21
+ end
22
+ end
23
+
24
+ context "with twitter as provider and username as url_or_username" do
25
+ let(:profile_attributes){ {url_or_username: "https://www.twitter.com/TheDailyShow", provider: "twitter"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.twitter.com/TheDailyShow"
29
+ expect(parser.provider).to eq "twitter"
30
+ expect(parser.username).to eq "TheDailyShow"
31
+ end
32
+ end
33
+
34
+ context "with twitter url and provider" do
35
+ let(:profile_attributes){ {url: "https://twitter.com/TheDailyShow", provider: "twitter"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://twitter.com/TheDailyShow"
39
+ expect(parser.provider).to eq "twitter"
40
+ expect(parser.username).to eq "TheDailyShow"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url without trailing slash" do
46
+ parser = described_class.parse "https://twitter.com/TEDTalks"
47
+ expect(parser.username).to eq "TEDTalks"
48
+ end
49
+
50
+ it "parses username from url with www" do
51
+ parser = described_class.parse "https://www.twitter.com/TEDTalks/"
52
+ expect(parser.username).to eq "TEDTalks"
53
+ end
54
+
55
+ it "parses username from url without http" do
56
+ parser = described_class.parse "twitter.com/TEDTalks/"
57
+ expect(parser.username).to eq "TEDTalks"
58
+ end
59
+
60
+ it "parses username from tweet url" do
61
+ parser = described_class.parse "https://twitter.com/TEDTalks/status/506827261379874816"
62
+ expect(parser.username).to eq "TEDTalks"
63
+ end
64
+
65
+ it "parses username from twitter media url" do
66
+ parser = described_class.parse "https://twitter.com/TEDTalks/media"
67
+ expect(parser.username).to eq "TEDTalks"
68
+ end
69
+
70
+ context "old twitter urls" do
71
+ it "parses username from twitter profile page url" do
72
+ parser = described_class.parse "http://twitter.com/#!/johnnycullen"
73
+ expect(parser.username).to eq "johnnycullen"
74
+ end
75
+
76
+ it "parses username from tweet url" do
77
+ parser = described_class.parse "https://twitter.com/#!/JohnnyCullen/status/507124787546968064"
78
+ expect(parser.username).to eq "JohnnyCullen"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ let(:parser){ described_class.parse profile_attributes }
5
+
6
+ context "correct class" do
7
+ let(:profile_attributes) { {url: "https://www.youtube.com/user/TeamCoco"} }
8
+
9
+ it "returns a Youtube object" do
10
+ expect(parser).to be_a SocialMediaParser::SocialMedia::Youtube
11
+ end
12
+ end
13
+
14
+ context "with url and provider" do
15
+ let(:profile_attributes){ {url: "https://www.youtube.com/user/teamcoco", provider: "youtube"} }
16
+
17
+ it "returns the parsed attributes" do
18
+ expect(parser.url).to eq "https://www.youtube.com/user/teamcoco"
19
+ expect(parser.provider).to eq "youtube"
20
+ expect(parser.username).to eq "teamcoco"
21
+ end
22
+ end
23
+
24
+ context "with username and provider" do
25
+ let(:profile_attributes){ {username: "TeamCoco", provider: "youtube"} }
26
+
27
+ it "returns the parsed attributes" do
28
+ expect(parser.url).to eq "https://www.youtube.com/user/TeamCoco"
29
+ expect(parser.provider).to eq "youtube"
30
+ expect(parser.username).to eq "TeamCoco"
31
+ end
32
+ end
33
+
34
+ context "with username as url_or_username and provider" do
35
+ let(:profile_attributes){ {url_or_username: "TeamCoco", provider: "youtube"} }
36
+
37
+ it "returns the parsed attributes" do
38
+ expect(parser.url).to eq "https://www.youtube.com/user/TeamCoco"
39
+ expect(parser.provider).to eq "youtube"
40
+ expect(parser.username).to eq "TeamCoco"
41
+ end
42
+ end
43
+
44
+ context "url variations" do
45
+ it "parses username from url with trailing slash" do
46
+ parser = described_class.parse "https://www.youtube.com/user/collegehumor/"
47
+ expect(parser.username).to eq "collegehumor"
48
+ end
49
+
50
+ it "parses username from url without user" do
51
+ parser = described_class.parse "https://www.youtube.com/user/collegehumor"
52
+ expect(parser.username).to eq "collegehumor"
53
+ end
54
+
55
+ it "parses username from a users channels url" do
56
+ parser = described_class.parse "https://www.youtube.com/user/collegehumor/channels"
57
+ expect(parser.username).to eq "collegehumor"
58
+ end
59
+
60
+ it "doesn't parse channel urls - returns them as usual links" do
61
+ parser = described_class.parse "https://www.youtube.com/channel/UCn8zNIfYAQNdrFRrr8oibKw"
62
+ expect(parser.username).to eq nil
63
+ expect(parser).to be_a SocialMediaParser::Link
64
+ end
65
+
66
+ it "doesn't parse playlist urls - returns them as usual links" do
67
+ parser = described_class.parse "https://www.youtube.com/playlist?list=PLA3D67612B92CD08B"
68
+ expect(parser.username).to eq nil
69
+ expect(parser).to be_a SocialMediaParser::Link
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe SocialMediaParser do
4
+ describe ".parse" do
5
+ let(:profile_attributes){ {url: "https://twitter.com/StephenAtHome", provider: 'twitter'} }
6
+
7
+ it "returns a Twitter instance" do
8
+ expect(described_class.parse(profile_attributes)).to be_a SocialMediaParser::SocialMedia::Twitter
9
+ end
10
+
11
+ describe "string key hash" do
12
+ let(:profile_attributes){ {"url" => "https://twitter.com/StephenAtHome", "provider" => "twitter"} }
13
+
14
+ it "still works" do
15
+ expect(described_class.parse(profile_attributes)).to be_a SocialMediaParser::SocialMedia::Twitter
16
+ end
17
+ end
18
+
19
+ describe "camelized provider" do
20
+ let(:profile_attributes){ {"url" => "https://twitter.com/StephenAtHome", "provider" => "Twitter"} }
21
+
22
+ it "still works" do
23
+ expect(described_class.parse(profile_attributes)).to be_a SocialMediaParser::SocialMedia::Twitter
24
+ end
25
+ end
26
+
27
+ describe "passing just a url string" do
28
+ let(:profile_attributes){ "https://twitter.com/StephenAtHome" }
29
+
30
+ it "still works" do
31
+ expect(described_class.parse(profile_attributes)).to be_a SocialMediaParser::SocialMedia::Twitter
32
+ end
33
+ end
34
+
35
+ describe "only passing in invalid url as url" do
36
+ let(:profile_attributes){ {url: "not a url"} }
37
+
38
+ it "returns nothing" do
39
+ parser = described_class.parse(profile_attributes)
40
+ expect(parser).to be_a SocialMediaParser::Link
41
+ expect(parser.url).to be_nil
42
+ end
43
+ end
44
+
45
+ describe "only passing in invalid url as url_or_username" do
46
+ let(:profile_attributes){ {url_or_username: "not a url"} }
47
+
48
+ it "returns nothing" do
49
+ parser = described_class.parse(profile_attributes)
50
+ expect(parser).to be_a SocialMediaParser::Link
51
+ expect(parser.url).to be_nil
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require 'social_media_parser'
4
+ require 'public_suffix'
5
+ require 'pry'
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_media_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Markus Nordin
8
+ - Jonas Brusman
9
+ - Alexander Rothe
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-09-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: public_suffix
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 1.4.5
29
+ - !ruby/object:Gem::Dependency
30
+ name: rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 3.0.0
43
+ - !ruby/object:Gem::Dependency
44
+ name: pry
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ description:
58
+ email:
59
+ - dev@mynewsdesk.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/social_media_parser.rb
70
+ - lib/social_media_parser/link.rb
71
+ - lib/social_media_parser/social_media/facebook.rb
72
+ - lib/social_media_parser/social_media/github.rb
73
+ - lib/social_media_parser/social_media/google.rb
74
+ - lib/social_media_parser/social_media/instagram.rb
75
+ - lib/social_media_parser/social_media/pinterest.rb
76
+ - lib/social_media_parser/social_media/provider.rb
77
+ - lib/social_media_parser/social_media/twitter.rb
78
+ - lib/social_media_parser/social_media/youtube.rb
79
+ - lib/social_media_parser/version.rb
80
+ - social_media_parser.gemspec
81
+ - spec/social_media_parser/link_spec.rb
82
+ - spec/social_media_parser/social_media/facebook_spec.rb
83
+ - spec/social_media_parser/social_media/github_spec.rb
84
+ - spec/social_media_parser/social_media/google_spec.rb
85
+ - spec/social_media_parser/social_media/instagram_spec.rb
86
+ - spec/social_media_parser/social_media/pinterest_spec.rb
87
+ - spec/social_media_parser/social_media/twitter_spec.rb
88
+ - spec/social_media_parser/social_media/youtube_spec.rb
89
+ - spec/social_media_parser_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: http://devcorner.mynewsdesk.com
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Parse social media attributes from url or construct url from attributes
115
+ test_files:
116
+ - spec/social_media_parser/link_spec.rb
117
+ - spec/social_media_parser/social_media/facebook_spec.rb
118
+ - spec/social_media_parser/social_media/github_spec.rb
119
+ - spec/social_media_parser/social_media/google_spec.rb
120
+ - spec/social_media_parser/social_media/instagram_spec.rb
121
+ - spec/social_media_parser/social_media/pinterest_spec.rb
122
+ - spec/social_media_parser/social_media/twitter_spec.rb
123
+ - spec/social_media_parser/social_media/youtube_spec.rb
124
+ - spec/social_media_parser_spec.rb
125
+ - spec/spec_helper.rb