ids_please 0.0.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 828c4e253714ec465733c938e21b05fc32424328
4
- data.tar.gz: ea65f9b285ed018eae2e4f9f4c6f49fabee2535d
3
+ metadata.gz: 7779235b8c59f4d9358aa29c286841278d15bf5d
4
+ data.tar.gz: 7295b6fcadf9aac2394dbd938df565b22078dcab
5
5
  SHA512:
6
- metadata.gz: ae6eed3675cf44e11e658d89a7ba4493bbb57bcf75264d300e1e88ede43b0c85acfc75106554a38521866fb13134a84884cbbbdd5b449281e57ee7902afd3042
7
- data.tar.gz: 68bd1add5001dea4def229ab4abf2bb98327a293764aa69ac648e10615fb29ed183b124959518135e1e1b2be279f27b5716ac655e446b05f03abcdc6c3134714
6
+ metadata.gz: 1211b6e2d5f4c70b5d8ddf96d7cf9e94bafe85c01ee5ebd8416b4c03eca139401dc262c955ee43a5cdee9df373726ebb9b8eac40e97ab851a2dc5e352b3dce06
7
+ data.tar.gz: 890bbc710aabcfefaa89fb3674896f2c024c9bce2e0cc9ef57cb0a0dd9f1e8e9ec8971e271baf09dfe87cf87031a41e332235120ad15fbf8357750ebb7b28312
data/README.md CHANGED
@@ -1,4 +1,72 @@
1
- ids_please
2
- ==========
1
+ # IDs, please
3
2
 
4
- Helps to get ids or screen names from links to social network accounts
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::BaseParser
1
+ class IdsPlease
2
+ class BaseParser
2
3
 
3
- def self.network_name
4
- self.name.split('::').last
5
- end
4
+ def self.to_sym
5
+ self.name.split('::').last.downcase.to_sym
6
+ end
6
7
 
7
- def self.parse(links)
8
- links.map { |l| parse_link(l) }.compact
9
- end
8
+ def self.parse(links)
9
+ links.map { |l| parse_link(l) }.compact
10
+ end
10
11
 
11
- def self.parse_link(link)
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
@@ -1,16 +1,18 @@
1
- class IdsPlease::Facebook < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Facebook < IdsPlease::BaseParser
2
3
 
3
- MASK = /fb\.me|fb\.com|facebook/i
4
+ MASK = /fb\.me|fb\.com|facebook/i
4
5
 
5
- def self.parse_link(link)
6
- if link.query && !link.query.empty?
7
- query = CGI.parse(link.query)
8
- query['id'].first if query.keys.include?('id')
9
- elsif link.path =~ /\/pages\//
10
- link.path.split('/').last
11
- else
12
- link.path.split('/')[1]
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::GooglePlus < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class GooglePlus < IdsPlease::BaseParser
2
3
 
3
- MASK = /google/i
4
+ MASK = /google/i
4
5
 
5
- def self.parse(links)
6
- links.map { |l| parse_link(l) }.compact
7
- end
6
+ def self.to_sym
7
+ :google_plus
8
+ end
8
9
 
9
- def self.parse_link(link)
10
- if link.host == 'google.com'
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
@@ -1,5 +1,7 @@
1
- class IdsPlease::Instagram < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Instagram < IdsPlease::BaseParser
2
3
 
3
- MASK = /instagr/i
4
+ MASK = /instagr/i
4
5
 
6
+ end
5
7
  end
@@ -1,13 +1,15 @@
1
- class IdsPlease::Odnoklassniki < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Odnoklassniki < IdsPlease::BaseParser
2
3
 
3
- MASK = /odnoklassniki/i
4
+ MASK = /odnoklassniki/i
4
5
 
5
- def self.parse_link(link)
6
- if link.path =~ /\/profile\//
7
- link.path.split('/').last
8
- elsif link.path.split('/').size == 2
9
- link.path.split('/').last
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
@@ -1,5 +1,7 @@
1
- class IdsPlease::Soundcloud < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Soundcloud < IdsPlease::BaseParser
2
3
 
3
- MASK = /snd\.cc|soundcloud/i
4
+ MASK = /snd\.cc|soundcloud/i
4
5
 
6
+ end
5
7
  end
@@ -1,16 +1,18 @@
1
- class IdsPlease::Tumblr < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Tumblr < IdsPlease::BaseParser
2
3
 
3
- MASK = /tumblr/i
4
+ MASK = /tumblr/i
4
5
 
5
- def self.parse(links)
6
- links.map do |link|
7
- next if link.host.sub('.tumblr.com', '') == link.host
8
- parse_link(link)
9
- end.compact
10
- end
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
- def self.parse_link(link)
13
- link.host.sub('.tumblr.com', '')
14
- end
13
+ def self.parse_link(link)
14
+ link.host.sub('.tumblr.com', '')
15
+ end
15
16
 
17
+ end
16
18
  end
@@ -1,14 +1,16 @@
1
- class IdsPlease::Twitter < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Twitter < IdsPlease::BaseParser
2
3
 
3
- MASK = /twitter/i
4
+ MASK = /twitter/i
4
5
 
5
- def self.parse_link(link)
6
- if link.path =~ /%23!/
7
- id = link.path.sub(/\A\/%23!\//, '')
8
- id.split(/[\/\?#]/).first
9
- else
10
- link.path.split('/')[1]
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
@@ -1,5 +1,7 @@
1
- class IdsPlease::Vimeo < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Vimeo < IdsPlease::BaseParser
2
3
 
3
- MASK = /vimeo/i
4
+ MASK = /vimeo/i
4
5
 
6
+ end
5
7
  end
@@ -1,18 +1,20 @@
1
- class IdsPlease::Vkontakte < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Vkontakte < IdsPlease::BaseParser
2
3
 
3
- MASK = /vk\.com|vkontakte/i
4
+ MASK = /vk\.com|vkontakte/i
4
5
 
5
- def self.parse(links)
6
- links.map { |l| parse_link(l) }.compact
7
- end
6
+ def self.parse(links)
7
+ links.map { |l| parse_link(l) }.compact
8
+ end
8
9
 
9
- def self.parse_link(link)
10
- if link.path =~ /id|club/
11
- id = link.path.sub(/\A\/id|\A\/club/, '')
12
- id.split(/[\/\?#]/).first
13
- else
14
- link.path.split('/')[1]
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
@@ -1,13 +1,15 @@
1
- class IdsPlease::Youtube < IdsPlease::BaseParser
1
+ class IdsPlease
2
+ class Youtube < IdsPlease::BaseParser
2
3
 
3
- MASK = /youtu\.be|youtube/i
4
+ MASK = /youtu\.be|youtube/i
4
5
 
5
- def self.parse_link(link)
6
- if link.path =~ /channels/
7
- link.path.split('/')[2]
8
- else
9
- link.path.split('/')[1]
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.3'
17
+ VERSION = '1.0.0'
7
18
 
8
- attr_accessor :original, :recognized, :unrecognized, :options, :parsed
19
+ attr_accessor :original, :recognized, :unrecognized, :parsed
9
20
 
10
- SOCIAL_NETWORKS = %w(
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 |klass_name, links|
39
- @parsed[klass_name] ||= []
40
- @parsed[klass_name] += parser(klass_name).parse(links)
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 =~ parser(network)::MASK
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['Facebook'].first).to eq('fb_acc')
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['Instagram'].first).to eq('inst_acc')
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['Vkontakte'].first).to eq('vk_acc')
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['Twitter'].first).to eq('twi_acc')
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['Vimeo'].first).to eq('vimeo_acc')
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['GooglePlus'].first).to eq('12341234')
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['Soundcloud'].first).to eq('sc_acc')
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['Youtube'].first).to eq('yb_acc')
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['Tumblr'].first).to eq('tumblr-acc')
72
+ expect(@recognizer.parsed[:tumblr].first).to eq('tumblr-acc')
73
73
  end
74
74
 
75
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ids_please
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gazay