-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added extract social regex and social methods
- Loading branch information
1 parent
e8f1f9c
commit ffd81fd
Showing
6 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module SocialsRegex | ||
class Extraction | ||
attr_accessor text: string | ||
|
||
def initialize: (text: string)-> void | ||
|
||
def extract_matches_per_platform: -> Hash[Symbol, Array[Hash[Symbol, string]]] | ||
|
||
def extract_matches_by_regex: (regex: Regexp | string) -> Array[Hash[Symbol, string]] | ||
|
||
def extract_matches_by_platform: (platform: string)-> Hash[Symbol, Array[Hash[Symbol, string]]] | ||
|
||
private | ||
|
||
def platform_matches: (regexes: Hash[Symbol, Regexp], platform: string | Symbol) -> Hash[Symbol, Array[Hash[Symbol, string]]] | ||
|
||
def matches: (regex: Regexp | string) -> Array[Hash[Symbol, string]] | ||
|
||
def reformat_matches: (matches: Array[MatchData]) -> Array[Hash[Symbol, string]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module SocialsRegex | ||
class Socials | ||
ERROR_MSG_UNKNOWN_PLATFORM: string | ||
PLATFORMS_REGEX: Hash[Symbol, Hash[Symbol, Regexp]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe SocialsRegex::Extraction do | ||
describe '#initialize' do | ||
it 'uses that text' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088' | ||
extraction = described_class.new(text: text) | ||
expect(extraction.text).to eq text | ||
end | ||
end | ||
|
||
describe '#extract_matches_per_platform' do | ||
context 'with one link' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088' | ||
extraction = described_class.new(text: text) | ||
let(:matches) { extraction.extract_matches_per_platform } | ||
|
||
it '#validate platform' do | ||
expect(matches.keys).to eq [:twitter] | ||
end | ||
|
||
it '#validate nested named_captures for example status in twitter' do | ||
expect(matches[:twitter].key?(:status)).to be true | ||
end | ||
|
||
it '#validate nested named_captures for example user in twitter' do | ||
expect(matches[:twitter].key?(:user)).to be true | ||
end | ||
|
||
it '#validate matched text like input' do | ||
expect(matches[:twitter][:status][0][:matched]).to eq text | ||
end | ||
|
||
it '#validate size of matching' do | ||
expect(matches[:twitter].keys.length).to eq 2 | ||
end | ||
end | ||
|
||
context 'with different link with same platform' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088 \ | ||
https://twitter.com/karllorey12/status/12599240820673740883' | ||
extraction = described_class.new(text: text) | ||
matches = extraction.extract_matches_per_platform | ||
|
||
it '#validate platform' do | ||
expect(matches.keys).to eq [:twitter] | ||
end | ||
|
||
it '#validate nested named_captures for example status in twitter' do | ||
expect(matches[:twitter].key?(:status)).to be true | ||
end | ||
|
||
it '#validate nested named_captures for example user in twitter' do | ||
expect(matches[:twitter].key?(:user)).to be true | ||
end | ||
|
||
it '#validate size of matching with same platform with different link' do | ||
expect(matches[:twitter].keys.length).to eq 2 | ||
end | ||
|
||
it '#validate size of links with nested keys like #user in twitter with same platform with different link' do | ||
expect(matches[:twitter][:user].length).to eq 2 | ||
end | ||
|
||
it '#validate size of links with nested keys like #status in twitter with same platform with different link' do | ||
expect(matches[:twitter][:status].length).to eq 2 | ||
end | ||
end | ||
|
||
context 'with multiple link with different platform' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088' \ | ||
'https://twitter.com/karllorey12/status/12599240820673740883' \ | ||
'http://crunchbase.com/organization/acme-corp [email protected] mailto:[email protected]' \ | ||
'https://facebook.com/peter.parker https://www.facebook.com/profile.php?id=100004123456789' \ | ||
'https://github.com/talaatmagdyx https://github.com/talaatmagdyx/socials_regex' \ | ||
'https://news.ycombinator.com/item?id=23290375 https://instagram.com/__disco__dude' \ | ||
'https://www.linkedin.com/in/talaatmagdyx/ https://medium.com/does-exist/some-post-123abc' | ||
extraction = described_class.new(text: text) | ||
matches = extraction.extract_matches_per_platform | ||
|
||
it '#validate platform with multiple link with different platform' do | ||
expect(matches.keys).to eq %i[crunchbase medium hackernews email instagram twitter | ||
linkedin github facebook] | ||
end | ||
|
||
it '#validate nested named_captures for example status in twitter with multiple link with different platform' do | ||
expect(matches[:twitter].key?(:status)).to be true | ||
end | ||
|
||
it '#validate nested named_captures for example user in twitter with multiple link with different platform' do | ||
expect(matches[:twitter].key?(:user)).to be true | ||
end | ||
|
||
it '#validate size of matching with multiple link with different platform' do | ||
expect(matches[:twitter].keys.length).to eq 2 | ||
end | ||
|
||
it '#validate size of links with nested keys like #user in twitter with multiple link with different platform' do | ||
expect(matches[:twitter][:user].length).to eq 2 | ||
end | ||
end | ||
end | ||
|
||
describe '#extract_matches_by_regex' do | ||
it 'extract data from string using regex' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088' | ||
extraction = described_class.new(text: text) | ||
matches = extraction.extract_matches_by_regex(regex: SocialsRegex::Regexes::TWITTER_URL_REGEX[:status]) | ||
expect(matches[0][:matched]).to eq text | ||
end | ||
end | ||
|
||
describe '#extract_matches_by_platform' do | ||
it 'extract data from string using platform' do | ||
text = 'https://twitter.com/karllorey/status/1259924082067374088' | ||
extraction = described_class.new(text: text) | ||
matches = extraction.extract_matches_by_platform(platform: SocialsRegex::Platforms::PLATFORM_TWITTER) | ||
expect(matches[SocialsRegex::Platforms::PLATFORM_TWITTER][:status][0][:matched]).to eq text | ||
end | ||
end | ||
end |