social_parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a618bb6a7babe7d50e706484b86710031402339
4
+ data.tar.gz: a4ab008d5797a3db34626c50d9535c8dc418117b
5
+ SHA512:
6
+ metadata.gz: 47f493a6042826145f021114a59e1fc67afa39eaed096e1bc1c274be7e453e29e5a9f6ce16fb7941eed6746cab75f7b4b088724b6283cb031ad0c56ae86f4b9a
7
+ data.tar.gz: b4a96da1125289a00b27a6ce0a712467747b6119812a01abef1f84e9e9010886ed83e10cb9194fb7f51c2268dcbe02b3fb20334250f8b0f4764ae21b77346dd6
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hyde31415@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gem 'public_suffix'
6
+
7
+ group :development, :test do
8
+ gem 'rspec'
9
+ end
10
+
11
+ # Specify your gem's dependencies in social_parser.gemspec
12
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ social_parser (0.1.0)
5
+ public_suffix
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ public_suffix (3.0.2)
12
+ rake (10.5.0)
13
+ rspec (3.7.0)
14
+ rspec-core (~> 3.7.0)
15
+ rspec-expectations (~> 3.7.0)
16
+ rspec-mocks (~> 3.7.0)
17
+ rspec-core (3.7.1)
18
+ rspec-support (~> 3.7.0)
19
+ rspec-expectations (3.7.0)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-mocks (3.7.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.7.0)
25
+ rspec-support (3.7.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ public_suffix
33
+ rake (~> 10.0)
34
+ rspec
35
+ social_parser!
36
+
37
+ BUNDLED WITH
38
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 pokohide
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # SocialParser
2
+
3
+ Parse social media attributes from url or construct url from attributes.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'social_parser'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```bash
17
+ $ bundle install
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```bash
23
+ $ gem install social_parser
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ The following code is an example of Twitter.
29
+
30
+ ```ruby
31
+ parser = SocialParser.parse 'https://www.twitter.com/hyde141421356'
32
+ => #<SocialParser::Provider::Twitter:0x007fc57720a690 @url="https://www.twitter.com/hyde141421356">
33
+
34
+ parser.username
35
+ => 'hyde141421356'
36
+
37
+ parser.provider
38
+ => :twitter
39
+
40
+ parser.url
41
+ => 'https://www.twitter.com/hyde141421356'
42
+ ```
43
+
44
+ Facebook, Google+, LinkedIn, etc... as well.
45
+
46
+ ```ruby
47
+ parser = SocialParser.parse 'https://github.com/hyde2able'
48
+ => #<SocialParser::Provider::Github:0x007fc5771e3c98 @url="https://github.com/hyde2able">
49
+
50
+ parser.username
51
+ => 'hyde2able'
52
+
53
+ parser.provider
54
+ => :github
55
+
56
+ parser.url
57
+ => 'https://github.com/hyde2able'
58
+ ```
59
+
60
+ An example of Youtube.
61
+
62
+ ```ruby
63
+ parser = SocialParser.parse 'https://www.youtube.com/watch?v=WOvdMz4yM9U'
64
+ => #<SocialParser::Provider::Youtube:0x007faa5e26fd58 @url="https://www.youtube.com/watch?v=WOvdMz4yM9U", @type="video">
65
+
66
+ parser.id # alias parser.username
67
+ => 'WOvdMz4yM9U'
68
+
69
+ parser.embed_url
70
+ => 'https://www.youtube.com/embed/WOvdMz4yM9U'
71
+ ```
72
+
73
+ When an embedded URL is not provided
74
+
75
+ ```ruby
76
+ parser = SocialParser.parse 'https://github.com/hyde2able'
77
+ => #<SocialParser::Provider::Github:0x007faa5e06d7a8 @url="https://github.com/hyde2able">
78
+
79
+ parser.embed_url
80
+ # SocialParser::InvalidURIError: SocialParser::InvalidURIError
81
+ ```
82
+
83
+ ## Test
84
+
85
+ Excute this code
86
+
87
+ ```
88
+ bundle exec rspec
89
+ ```
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "social_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ require 'social_parser/version'
2
+
3
+ Dir[File.join(File.dirname(__FILE__), 'social_parser', 'provider', '*.rb')].each { |file| require file }
4
+
5
+ module SocialParser
6
+
7
+ def self.parse(attrs)
8
+ if attrs.is_a? String
9
+ parse(url: attrs)
10
+ else
11
+ Provider::Base.parse(attrs)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,52 @@
1
+ require 'public_suffix'
2
+ require 'uri'
3
+
4
+ module SocialParser
5
+
6
+ class Link
7
+
8
+ def initialize(attrs)
9
+ attrs.each do |key, value|
10
+ instance_variable_set("@#{key}", value) unless value.nil?
11
+ end
12
+ end
13
+
14
+ def username
15
+ nil
16
+ end
17
+
18
+ def provider
19
+ nil
20
+ end
21
+
22
+ def url
23
+ url_from_attributes
24
+ end
25
+
26
+ def attributes
27
+ {
28
+ url: url,
29
+ provider: provider,
30
+ username: username
31
+ }
32
+ end
33
+
34
+ private
35
+
36
+ def url_from_attributes
37
+ valid_url_format(@url) or valid_url_format(@url_or_username)
38
+ end
39
+
40
+ def valid_url_format(url)
41
+ uri = URI.parse(url)
42
+ return uri.to_s if %w(http https).include?(uri.scheme)
43
+ return "http://#{url}" if PublicSuffix.valid?(URI.parse("http://#{url}").host)
44
+ rescue URI::BadURIError, URI::InvalidURIError
45
+ nil
46
+ end
47
+
48
+ def invalid_url_format?(url)
49
+ !valid_url_format(url)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,67 @@
1
+ require 'social_parser/link'
2
+
3
+ module SocialParser
4
+
5
+ class InvalidURIError < Exception; end
6
+
7
+ module Provider
8
+ class Base < ::SocialParser::Link
9
+
10
+ def self.parse(attrs)
11
+ if attrs[:provider]
12
+
13
+ SocialParser::Provider.const_get(attrs[:provider].to_s.capitalize).new(attrs)
14
+ else
15
+
16
+ providers.map do |provider|
17
+ SocialParser::Provider.const_get(provider.to_s.capitalize).new(attrs)
18
+ end.find(&:valid?) or ::SocialParser::Link.new(attrs)
19
+ end
20
+ end
21
+
22
+ def username
23
+ return @username if @username
24
+ if @url_or_username and invalid_url_format?(@url_or_username)
25
+ @url_or_username
26
+ elsif url_from_attributes
27
+ parse_from_url
28
+ end
29
+ end
30
+
31
+ def url
32
+ return url_from_attributes if url_from_attributes
33
+ "https://www.#{provider.to_s}.com/#{username}"
34
+ end
35
+
36
+ def domain
37
+ 'com'
38
+ end
39
+
40
+ def embed_url
41
+ raise SocialParser::InvalidURIError
42
+ end
43
+
44
+ def valid?
45
+ (@provider and @provider == provider) or
46
+ (username and URI.parse(url_from_attributes).host.match("#{provider.to_s}.#{domain}"))
47
+ rescue URI::BadURIError, URI::InvalidURIError
48
+ false
49
+ end
50
+
51
+ alias_method :id, :username
52
+
53
+ private
54
+
55
+ def parse_from_url
56
+ URI.parse(url_from_attributes).path.split('/')[1]
57
+ rescue URI::BadURIError, URI::InvalidURIError
58
+ nil
59
+ end
60
+
61
+ def self.providers
62
+ @providers ||= [:facebook, :github, :twitter, :youtube, :instagram, :linkedin,
63
+ :medium, :qiita, :pinterest, :google, :vimeo]
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,28 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Facebook < Base
6
+ URL_FORMATS = {
7
+ full: /\A((https?)?:\/\/)?(www\.)?facebook\.com\/(\w*#!\/)?(pages\/[\w\-]*)?([?\d\-]*\/)?(profile\.php\?id=(?=\d.*))?(?<id>[\w\-\.]*)?/i,
8
+ # full: /((http|https):\/\/)?(www\.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/[\w\-]*)?(?:[?\d\-]*\/)?(?:profile.php\?id=(?=\d.*))?(?<id>[\w\-\.]*)?/i,
9
+ regular: /\Ahttps?:\/\/www\.facebook\.com\/(?!sharer\/)(?!share\.php\?)(?!sharer\.php\?)(?<id>.+?)\/?\Z/,
10
+ shorter: /\Ahttps?:\/\/facebook\.com\/(?!sharer\/)(?!share\.php\?)(?!sharer\.php\?)(?<id>.+?)\/?\Z/
11
+ }
12
+
13
+ def provider
14
+ :facebook
15
+ end
16
+
17
+ private
18
+
19
+ def parse_from_url
20
+ URL_FORMATS.values.each do |format|
21
+ m = format.match(url_from_attributes)
22
+ return m[:id] if m
23
+ end
24
+ nil
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Github < Base
6
+ URL_FORMATS = {
7
+ full: /((https?)?:\/\/)?(www\.)?github\.com\/(?<id>[\w\-\.]+)?/i
8
+ # regular: /\Ahttps?:\/\/github\.com\/(?<id>.+?)\/?\Z/
9
+ }
10
+
11
+ def provider
12
+ :github
13
+ end
14
+
15
+ def url
16
+ "https://github.#{domain}/#{username}"
17
+ end
18
+
19
+ private
20
+
21
+ def parse_from_url
22
+ URL_FORMATS.values.each do |format|
23
+ m = format.match(url_from_attributes)
24
+ return m[:id] if m
25
+ end
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Google < Base
6
+ URL_FORMATS = {
7
+ full: /((https?)?:\/\/)?plus\.google\.com\/u\/\d{1,}\/(?<id>[\w\-\.\+]+)?/i,
8
+ shorter: /((https?)?:\/\/)?plus.google.com\/(?<id>[\w\-\.\+]+)?/i
9
+ }
10
+
11
+ def provider
12
+ :google
13
+ end
14
+
15
+ def url
16
+ "https://plus.google.#{domain}/#{username}"
17
+ end
18
+
19
+ private
20
+
21
+ def parse_from_url
22
+ URL_FORMATS.values.each do |format|
23
+ m = format.match(url_from_attributes)
24
+ return m[:id] if m
25
+ end
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Instagram < Base
6
+ URL_FORMATS = {
7
+ regular: /\A((https?)?:\/\/)?(www\.)?instagram\.com\/(stories\/)?(?<instagram>.+?)\/?\Z/
8
+ }
9
+
10
+ def provider
11
+ :instagram
12
+ end
13
+
14
+ private
15
+
16
+ def parse_from_url
17
+ URL_FORMATS.values.each do |format|
18
+ m = format.match(url_from_attributes)
19
+ return m[:instagram] if m
20
+ end
21
+ nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Linkedin < Base
6
+ URL_FORMATS = {
7
+ full: /\A((https?):\/\/)?(www\.)?linkedin\.com\/(?<type>(in|company|school))?\/(?<id>[\w\-\.ぁ-んァ-ヴ一-龠]+)\/?/iu
8
+ }
9
+
10
+ def provider
11
+ :linkedin
12
+ end
13
+
14
+ def type
15
+ @type || 'in'
16
+ end
17
+
18
+ def url
19
+ "https://www.linkedin.#{domain}/#{type}/#{username}"
20
+ end
21
+
22
+ private
23
+
24
+ def parse_from_url
25
+ URL_FORMATS.values.each do |format|
26
+ m = format.match(url_from_attributes)
27
+ next unless m
28
+ @type = m[:type] if m.names.include?('type')
29
+ return m[:id]
30
+ end
31
+ nil
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Medium < Base
6
+ URL_FORMATS = {
7
+ full: /\A((https?)?:\/\/)?(www\.)?medium\.com\/(?<id>@[\w\-\.]+)\/?/i
8
+ }
9
+
10
+ def provider
11
+ :medium
12
+ end
13
+
14
+ def url
15
+ "https://medium.#{domain}/#{username}"
16
+ end
17
+
18
+ private
19
+
20
+ def parse_from_url
21
+ URL_FORMATS.values.each do |format|
22
+ m = format.match(url_from_attributes)
23
+ return m[:id] if m
24
+ end
25
+ nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Pinterest < Base
6
+ URL_FORMATS = {
7
+ full: /\A((https?)?:\/\/)?(www\.)?pinterest\.(?<domain>(com|jp))\/(?<id>[\w\-\.]+)\/?/i
8
+ }
9
+
10
+ def provider
11
+ :pinterest
12
+ end
13
+
14
+ def domain
15
+ @domain || 'com'
16
+ end
17
+
18
+ def url
19
+ @domain ||= 'com'
20
+ "https://www.pinterest.#{domain}/#{username}"
21
+ end
22
+
23
+ private
24
+
25
+ def parse_from_url
26
+ URL_FORMATS.values.each do |format|
27
+ m = format.match(url_from_attributes)
28
+ if m
29
+ @domain = m[:domain]
30
+ return m[:id]
31
+ end
32
+ end
33
+ nil
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Qiita < Base
6
+ URL_FORMATS = {
7
+ regular: /\A((https?)?:\/\/)?(www\.)?qiita\.com\/(?<id>[\w\-\.]+)/i
8
+ }
9
+
10
+ def provider
11
+ :qiita
12
+ end
13
+
14
+ def url
15
+ "https://qiita.#{domain}/#{username}"
16
+ end
17
+
18
+ private
19
+
20
+ def parse_from_url
21
+ URL_FORMATS.values.each do |format|
22
+ m = format.match(url_from_attributes)
23
+ return m[:id] if m
24
+ end
25
+ nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Twitter < Base
6
+ URL_FORMATS = {
7
+ full: /\A((https?):\/\/)?(www\.)?twitter\.com\/(?:(?:\w)*#!\/)?(?<id>\w+)/i
8
+ # regular: /\Ahttps?:\/\/(www\.)?twitter\.com\/(?!share)(?!share\?)(?!intent\/)(?<id>.+?)\/?\Z/
9
+ }
10
+
11
+ def provider
12
+ :twitter
13
+ end
14
+
15
+ private
16
+
17
+ def parse_from_url
18
+ URL_FORMATS.values.each do |format|
19
+ m = format.match(url_from_attributes)
20
+ return m[:id] if m
21
+ end
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Vimeo < Base
6
+ URL_FORMATS = {
7
+ video: /\A((https?)?:\/\/)?(www\.)?vimeo\.com\/(?<id>\d+)\/?/i,
8
+ channels: /\A((https?)?:\/\/)?(www\.)?vimeo\.com\/channels\/(?<id>[\w\-\.]+)\/?/i,
9
+ user: /\A((https?)?:\/\/)?(www\.)?vimeo\.com\/(?<id>[\w\-\.]+)\/?/i
10
+ }
11
+
12
+ def provider
13
+ :vimeo
14
+ end
15
+
16
+ def type
17
+ @type || 'user'
18
+ end
19
+
20
+ def url
21
+ if video?
22
+ "https://vimeo.#{domain}/#{id}"
23
+ elsif channels?
24
+ "https://vimeo.#{domain}/channels/#{username}"
25
+ else
26
+ "https://vimeo.#{domain}/#{username}"
27
+ end
28
+ end
29
+
30
+ def embed_url
31
+ return super unless video?
32
+ "https://player.vimeo.#{domain}/video/#{id}"
33
+ end
34
+
35
+ private
36
+
37
+ def video?; type == 'video' end
38
+ def channels?; type =='channels' end
39
+
40
+ def parse_from_url
41
+ URL_FORMATS.each do |key, format|
42
+ m = format.match(url_from_attributes)
43
+ next unless m
44
+ @type = key.to_s
45
+ return m[:id]
46
+ end
47
+ nil
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ require 'social_parser/provider/base'
2
+
3
+ module SocialParser
4
+ module Provider
5
+ class Youtube < Base
6
+ URL_FORMATS = {
7
+ video: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/watch\?v=(?<id>[\w\-\.]+)\/?/i,
8
+ full: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/(?<type>(user|channel|playlist))\/(?<id>[\w\-\.]+)\/?/i,
9
+ shortend: /\A((https?)?:\/\/)?(www\.)?youtube\.com\/(?<id>[\w\-\.]+)\/?/i,
10
+ }
11
+
12
+ def provider
13
+ :youtube
14
+ end
15
+
16
+ def type
17
+ @type || 'user'
18
+ end
19
+
20
+ def url
21
+ if video?
22
+ "https://www.youtube.#{domain}/watch?v=#{username}"
23
+ else
24
+ "https://www.youtube.#{domain}/#{type}/#{username}"
25
+ end
26
+ end
27
+
28
+ def embed_url
29
+ return super unless video?
30
+ "https://www.youtube.#{domain}/embed/#{username}"
31
+ end
32
+
33
+ private
34
+
35
+ def video?
36
+ type == 'video'
37
+ end
38
+
39
+ def parse_from_url
40
+ URL_FORMATS.each do |key, format|
41
+ m = format.match(url_from_attributes)
42
+ next unless m
43
+
44
+ if key == :full
45
+ @type = m[:type]
46
+ return nil if @type != 'user'
47
+ elsif key == :video
48
+ @type = 'video'
49
+ end
50
+ return m[:id]
51
+ end
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module SocialParser
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "social_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "social_parser"
8
+ spec.version = SocialParser::VERSION
9
+ spec.authors = ["pokohide"]
10
+ spec.email = ["hyde14142@gmail.com"]
11
+
12
+ spec.summary = %q{Parse social media attributes from url or construct url from attributes}
13
+ spec.description = %q{Parse social media attributes from url or construct url from attributes}
14
+ spec.homepage = 'https://github.com/hyde2able'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_dependency 'public_suffix'
29
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - pokohide
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: public_suffix
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Parse social media attributes from url or construct url from attributes
70
+ email:
71
+ - hyde14142@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/social_parser.rb
88
+ - lib/social_parser/link.rb
89
+ - lib/social_parser/provider/base.rb
90
+ - lib/social_parser/provider/facebook.rb
91
+ - lib/social_parser/provider/github.rb
92
+ - lib/social_parser/provider/google.rb
93
+ - lib/social_parser/provider/instagram.rb
94
+ - lib/social_parser/provider/linkedin.rb
95
+ - lib/social_parser/provider/medium.rb
96
+ - lib/social_parser/provider/pinterest.rb
97
+ - lib/social_parser/provider/qiita.rb
98
+ - lib/social_parser/provider/twitter.rb
99
+ - lib/social_parser/provider/vimeo.rb
100
+ - lib/social_parser/provider/youtube.rb
101
+ - lib/social_parser/version.rb
102
+ - social_parser.gemspec
103
+ homepage: https://github.com/hyde2able
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Parse social media attributes from url or construct url from attributes
127
+ test_files: []