ids_please 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +71 -3
- data/lib/ids_please/base_parser.rb +14 -10
- data/lib/ids_please/facebook.rb +13 -11
- data/lib/ids_please/google_plus.rb +20 -12
- data/lib/ids_please/instagram.rb +4 -2
- data/lib/ids_please/odnoklassniki.rb +10 -8
- data/lib/ids_please/soundcloud.rb +4 -2
- data/lib/ids_please/tumblr.rb +13 -11
- data/lib/ids_please/twitter.rb +11 -9
- data/lib/ids_please/vimeo.rb +4 -2
- data/lib/ids_please/vkontakte.rb +14 -12
- data/lib/ids_please/youtube.rb +10 -8
- data/lib/ids_please.rb +33 -34
- data/spec/ids_please/basic_spec.rb +9 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7779235b8c59f4d9358aa29c286841278d15bf5d
|
4
|
+
data.tar.gz: 7295b6fcadf9aac2394dbd938df565b22078dcab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1211b6e2d5f4c70b5d8ddf96d7cf9e94bafe85c01ee5ebd8416b4c03eca139401dc262c955ee43a5cdee9df373726ebb9b8eac40e97ab851a2dc5e352b3dce06
|
7
|
+
data.tar.gz: 890bbc710aabcfefaa89fb3674896f2c024c9bce2e0cc9ef57cb0a0dd9f1e8e9ec8971e271baf09dfe87cf87031a41e332235120ad15fbf8357750ebb7b28312
|
data/README.md
CHANGED
@@ -1,4 +1,72 @@
|
|
1
|
-
|
2
|
-
==========
|
1
|
+
# IDs, please
|
3
2
|
|
4
|
-
|
3
|
+
Get social network IDs or screen names from links to social network accounts.
|
4
|
+
|
5
|
+
Sometimes you need to get a social network account name from a link —
|
6
|
+
to store a screen name in yor database instead of parsing the link every time,
|
7
|
+
or maybe to work with these accounts using social network APIs (as I do).
|
8
|
+
Would be easier to have a library that extracts this kind of information
|
9
|
+
from all known social networks for your pleasure.
|
10
|
+
|
11
|
+
Sponsored by [Evil Martians](http://evilmartians.com).
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
```bash
|
16
|
+
gem install ids_please
|
17
|
+
```
|
18
|
+
|
19
|
+
Or, put this in your Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'ids_please'
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
This is how you get social IDs from from links:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
ids = IdsPlease.new('https://twitter.com/gazay', 'http://facebook.com/alexey.gaziev')
|
31
|
+
ids.parse
|
32
|
+
puts ids.parsed['Twitter'] # => ["gazay"]
|
33
|
+
puts ids.parsed['Facebook'] # => ["alexey.gaziev"]
|
34
|
+
|
35
|
+
puts ids.original # => ["https://twitter.com/gazay", "http://facebook.com/alexey.gaziev"]
|
36
|
+
```
|
37
|
+
|
38
|
+
Or you can just check that the link is for a known social network:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ids = IdsPlease.new('https://twitter.com/gazay', 'http://some-unknown-network.com/gazay')
|
42
|
+
ids.recognize
|
43
|
+
puts ids.recognized # => {"Twitter"=>[#<URI::HTTP:0x007fea3bba7e30 URL:http://twitter.com/gazay>]}
|
44
|
+
puts ids.unrecognized # => ["http://some-unknown-network.com/gazay"]
|
45
|
+
```
|
46
|
+
|
47
|
+
Social networks supported at the moment:
|
48
|
+
|
49
|
+
* Facebook
|
50
|
+
* Twitter
|
51
|
+
* Instagram
|
52
|
+
* Soundcloud
|
53
|
+
* GooglePlus
|
54
|
+
* Youtube
|
55
|
+
* Tumblr
|
56
|
+
* Vimeo
|
57
|
+
* Vkontakte
|
58
|
+
* Odnoklassniki
|
59
|
+
|
60
|
+
## Contributors
|
61
|
+
|
62
|
+
* @gazay
|
63
|
+
|
64
|
+
Special thanks to @ai, @yaroslav, @whitequark
|
65
|
+
|
66
|
+
## Notes
|
67
|
+
|
68
|
+
Gem named under an impression of an awesome game called [Papers, please](http://papersplea.se/)
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The MIT License
|
@@ -1,15 +1,19 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class BaseParser
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
def self.to_sym
|
5
|
+
self.name.split('::').last.downcase.to_sym
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def self.parse(links)
|
9
|
+
links.map { |l| parse_link(l) }.compact
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
link.path.split('/')[1]
|
13
|
-
end
|
12
|
+
private
|
14
13
|
|
14
|
+
def self.parse_link(link)
|
15
|
+
link.path.split('/')[1]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
15
19
|
end
|
data/lib/ids_please/facebook.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Facebook < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /fb\.me|fb\.com|facebook/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
def self.parse_link(link)
|
7
|
+
if link.query && !link.query.empty?
|
8
|
+
query = CGI.parse(link.query)
|
9
|
+
query['id'].first if query.keys.include?('id')
|
10
|
+
elsif link.path =~ /\/pages\//
|
11
|
+
link.path.split('/').last
|
12
|
+
else
|
13
|
+
link.path.split('/')[1]
|
14
|
+
end
|
13
15
|
end
|
14
|
-
end
|
15
16
|
|
17
|
+
end
|
16
18
|
end
|
@@ -1,18 +1,26 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class GooglePlus < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /google/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def self.to_sym
|
7
|
+
:google_plus
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
link.path.split('/')[1]
|
12
|
-
else
|
13
|
-
matched = link.path.match(/\/(\d{2,})/)
|
14
|
-
matched[1] if matched
|
10
|
+
def self.parse(links)
|
11
|
+
links.map { |l| parse_link(l) }.compact
|
15
12
|
end
|
16
|
-
end
|
17
13
|
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.parse_link(link)
|
17
|
+
if link.host == 'google.com'
|
18
|
+
link.path.split('/')[1]
|
19
|
+
else
|
20
|
+
matched = link.path.match(/\/(\d{2,})/)
|
21
|
+
matched[1] if matched
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
18
26
|
end
|
data/lib/ids_please/instagram.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Odnoklassniki < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /odnoklassniki/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def self.parse_link(link)
|
7
|
+
if link.path =~ /\/profile\//
|
8
|
+
link.path.split('/').last
|
9
|
+
elsif link.path.split('/').size == 2
|
10
|
+
link.path.split('/').last
|
11
|
+
end
|
10
12
|
end
|
11
|
-
end
|
12
13
|
|
14
|
+
end
|
13
15
|
end
|
data/lib/ids_please/tumblr.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Tumblr < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /tumblr/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
def self.parse(links)
|
7
|
+
links.map do |link|
|
8
|
+
next if link.host.sub('.tumblr.com', '') == link.host
|
9
|
+
parse_link(link)
|
10
|
+
end.compact
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def self.parse_link(link)
|
14
|
+
link.host.sub('.tumblr.com', '')
|
15
|
+
end
|
15
16
|
|
17
|
+
end
|
16
18
|
end
|
data/lib/ids_please/twitter.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Twitter < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /twitter/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
def self.parse_link(link)
|
7
|
+
if link.path =~ /%23!/
|
8
|
+
id = link.path.sub(/\A\/%23!\//, '')
|
9
|
+
id.split(/[\/\?#]/).first
|
10
|
+
else
|
11
|
+
link.path.split('/')[1]
|
12
|
+
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
15
|
+
end
|
14
16
|
end
|
data/lib/ids_please/vimeo.rb
CHANGED
data/lib/ids_please/vkontakte.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Vkontakte < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /vk\.com|vkontakte/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def self.parse(links)
|
7
|
+
links.map { |l| parse_link(l) }.compact
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def self.parse_link(link)
|
11
|
+
if link.path =~ /id|club/
|
12
|
+
id = link.path.sub(/\A\/id|\A\/club/, '')
|
13
|
+
id.split(/[\/\?#]/).first
|
14
|
+
else
|
15
|
+
link.path.split('/')[1]
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
19
|
+
end
|
18
20
|
end
|
data/lib/ids_please/youtube.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
class IdsPlease
|
1
|
+
class IdsPlease
|
2
|
+
class Youtube < IdsPlease::BaseParser
|
2
3
|
|
3
|
-
|
4
|
+
MASK = /youtu\.be|youtube/i
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def self.parse_link(link)
|
7
|
+
if link.path =~ /channels/
|
8
|
+
link.path.split('/')[2]
|
9
|
+
else
|
10
|
+
link.path.split('/')[1]
|
11
|
+
end
|
10
12
|
end
|
11
|
-
end
|
12
13
|
|
14
|
+
end
|
13
15
|
end
|
data/lib/ids_please.rb
CHANGED
@@ -1,28 +1,38 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'cgi'
|
3
|
+
require_relative 'ids_please/base_parser'
|
4
|
+
require_relative 'ids_please/facebook'
|
5
|
+
require_relative 'ids_please/google_plus'
|
6
|
+
require_relative 'ids_please/instagram'
|
7
|
+
require_relative 'ids_please/twitter'
|
8
|
+
require_relative 'ids_please/tumblr'
|
9
|
+
require_relative 'ids_please/vimeo'
|
10
|
+
require_relative 'ids_please/youtube'
|
11
|
+
require_relative 'ids_please/soundcloud'
|
12
|
+
require_relative 'ids_please/vkontakte'
|
13
|
+
require_relative 'ids_please/odnoklassniki'
|
3
14
|
|
4
15
|
class IdsPlease
|
5
16
|
|
6
|
-
VERSION = '0.0
|
17
|
+
VERSION = '1.0.0'
|
7
18
|
|
8
|
-
attr_accessor :original, :recognized, :unrecognized, :
|
19
|
+
attr_accessor :original, :recognized, :unrecognized, :parsed
|
9
20
|
|
10
|
-
SOCIAL_NETWORKS =
|
11
|
-
GooglePlus
|
12
|
-
Vkontakte
|
13
|
-
Twitter
|
14
|
-
Facebook
|
15
|
-
Instagram
|
16
|
-
Soundcloud
|
17
|
-
Vimeo
|
18
|
-
Youtube
|
19
|
-
Odnoklassniki
|
20
|
-
Tumblr
|
21
|
-
|
21
|
+
SOCIAL_NETWORKS = [
|
22
|
+
IdsPlease::GooglePlus,
|
23
|
+
IdsPlease::Vkontakte,
|
24
|
+
IdsPlease::Twitter,
|
25
|
+
IdsPlease::Facebook,
|
26
|
+
IdsPlease::Instagram,
|
27
|
+
IdsPlease::Soundcloud,
|
28
|
+
IdsPlease::Vimeo,
|
29
|
+
IdsPlease::Youtube,
|
30
|
+
IdsPlease::Odnoklassniki,
|
31
|
+
IdsPlease::Tumblr
|
32
|
+
]
|
22
33
|
|
23
34
|
def initialize(*args)
|
24
35
|
duped_args = args.dup
|
25
|
-
@options = duped_args.pop if duped_args.last.is_a?(Hash)
|
26
36
|
@original = duped_args
|
27
37
|
end
|
28
38
|
|
@@ -35,38 +45,27 @@ class IdsPlease
|
|
35
45
|
def parse
|
36
46
|
recognize
|
37
47
|
@parsed = {}
|
38
|
-
recognized.each do |
|
39
|
-
|
40
|
-
|
48
|
+
recognized.each do |network_sym, links|
|
49
|
+
parser = SOCIAL_NETWORKS.find { |sn| sn.to_sym == network_sym }
|
50
|
+
|
51
|
+
@parsed[network_sym] ||= []
|
52
|
+
@parsed[network_sym] += parser.parse(links)
|
41
53
|
end
|
42
54
|
end
|
43
55
|
|
44
56
|
private
|
45
57
|
|
46
58
|
def recognize_link(link)
|
47
|
-
network, handle = nil
|
48
59
|
link = "http://#{link}" unless link =~ /\Ahttps?:\/\//
|
49
60
|
parsed_link = URI(URI.encode(link))
|
50
61
|
SOCIAL_NETWORKS.each do |network|
|
51
|
-
if parsed_link.host =~
|
52
|
-
recognized[network] ||= []
|
53
|
-
recognized[network] << parsed_link
|
62
|
+
if parsed_link.host =~ network::MASK
|
63
|
+
recognized[network.to_sym] ||= []
|
64
|
+
recognized[network.to_sym] << parsed_link
|
54
65
|
return
|
55
66
|
end
|
56
67
|
end
|
57
68
|
unrecognized << link
|
58
69
|
end
|
59
70
|
|
60
|
-
def parser(name)
|
61
|
-
"IdsPlease::#{name}".split('::').inject(Module) do |acc, val|
|
62
|
-
acc.const_get(val)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
require_relative 'ids_please/base_parser'
|
69
|
-
Dir[File.dirname(__FILE__) + '/ids_please/*.rb'].each do |file|
|
70
|
-
next if file =~ /base_parser/
|
71
|
-
require file
|
72
71
|
end
|
@@ -37,39 +37,39 @@ describe IdsPlease do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'get right id from facebook link' do
|
40
|
-
expect(@recognizer.parsed[
|
40
|
+
expect(@recognizer.parsed[:facebook].first).to eq('fb_acc')
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'get right id from instagram link' do
|
44
|
-
expect(@recognizer.parsed[
|
44
|
+
expect(@recognizer.parsed[:instagram].first).to eq('inst_acc')
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'get right id from vk link' do
|
48
|
-
expect(@recognizer.parsed[
|
48
|
+
expect(@recognizer.parsed[:vkontakte].first).to eq('vk_acc')
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'get right id from twitter link' do
|
52
|
-
expect(@recognizer.parsed[
|
52
|
+
expect(@recognizer.parsed[:twitter].first).to eq('twi_acc')
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'get right id from vimeo link' do
|
56
|
-
expect(@recognizer.parsed[
|
56
|
+
expect(@recognizer.parsed[:vimeo].first).to eq('vimeo_acc')
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'get right id from google+ link' do
|
60
|
-
expect(@recognizer.parsed[
|
60
|
+
expect(@recognizer.parsed[:google_plus].first).to eq('12341234')
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'get right id from soundcloud link' do
|
64
|
-
expect(@recognizer.parsed[
|
64
|
+
expect(@recognizer.parsed[:soundcloud].first).to eq('sc_acc')
|
65
65
|
end
|
66
66
|
|
67
67
|
it 'get right id from youtube link' do
|
68
|
-
expect(@recognizer.parsed[
|
68
|
+
expect(@recognizer.parsed[:youtube].first).to eq('yb_acc')
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'get right id from tumblr link' do
|
72
|
-
expect(@recognizer.parsed[
|
72
|
+
expect(@recognizer.parsed[:tumblr].first).to eq('tumblr-acc')
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|