Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
zealot128 committed Sep 22, 2020
0 parents commit f24ae95
Show file tree
Hide file tree
Showing 29 changed files with 1,154 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
spec/.failures.txt

1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source "https://rubygems.org"

# Specify your gem's dependencies in network_profile.gemspec
gemspec

gem "rake", "~> 12.0"
gem "pry"
gem "rspec", ">= 3.5"
gem "vcr"
gem "webmock"
gem 'pry'
92 changes: 92 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
PATH
remote: .
specs:
network_profile (0.1.0)
activesupport (>= 5.0.0)
nokogiri
rdf-microdata
typhoeus

GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
coderay (1.1.3)
concurrent-ruby (1.1.7)
crack (0.4.4)
diff-lcs (1.4.4)
ethon (0.12.0)
ffi (>= 1.3.0)
ffi (1.13.1)
hamster (3.0.0)
concurrent-ruby (~> 1.0)
hashdiff (1.0.1)
htmlentities (4.3.4)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
link_header (0.0.8)
method_source (1.0.0)
mini_portile2 (2.4.0)
minitest (5.14.2)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
rake (12.3.3)
rdf (3.1.6)
hamster (~> 3.0)
link_header (~> 0.0, >= 0.0.8)
rdf-microdata (3.1.1)
htmlentities (~> 4.3)
nokogiri (~> 1.10)
rdf (~> 3.1)
rdf-xsd (~> 3.1)
rdf-xsd (3.1.0)
rdf (~> 3.1)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
thread_safe (0.3.6)
typhoeus (1.4.0)
ethon (>= 0.9.0)
tzinfo (1.2.7)
thread_safe (~> 0.1)
vcr (6.0.0)
webmock (3.9.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.4.0)

PLATFORMS
ruby

DEPENDENCIES
network_profile!
pry
rake (~> 12.0)
rspec (>= 3.5)
vcr
webmock

BUNDLED WITH
2.1.4
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Stefan Wienert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# NetworkProfile

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/network_profile`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'network_profile'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install network_profile

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zealot128/network_profile.


## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"
task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "network_profile"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
23 changes: 23 additions & 0 deletions lib/network_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "network_profile/version"
require 'network_profile/extractor'
require 'network_profile/extractors/default_profile'
require 'active_support/core_ext/module/attribute_accessors'

module NetworkProfile
class Error < StandardError; end

mattr_accessor :headers, :github_api_key

self.headers = {
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language' => 'de,en-US;q=0.7,en;q=0.3',
'Referer' => 'https://www.google.com',
'DNT' => '1',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:73.0) Gecko/20100101 Firefox/73.0',
}
self.github_api_key = nil

def self.parse(link, include_fallback_custom: false)
NetworkProfile::DefaultProfile.parse(link, include_fallback_custom: include_fallback_custom)
end
end
65 changes: 65 additions & 0 deletions lib/network_profile/extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'active_support/core_ext/string/filters'
class NetworkProfile::Extractor
# Logic from:
# https://github.com/tenderlove/rails_autolink/blob/master/lib/rails_autolink/helpers.rb
AUTO_LINK_RE = %r{
(?: ((?:ed2k|ftp|http|https|irc|mailto|news|gopher|nntp|telnet|webcal|xmpp|callto|feed|svn|urn|aim|rsync|tag|ssh|sftp|rtsp|afs|file):)// | www\. )
[^\s<\u00A0"]+
}ix.freeze
WORD_PATTERN = '\p{Word}'.freeze
BRACKETS = { ']' => '[', ')' => '(', '}' => '{' }.freeze

def self.call(string)
new(string).extracted_links!
end

def initialize(string)
@string = string
end

def extracted_links!
extracted = links.map do |l|
NetworkProfile.parse(l)
rescue StandardError => e
p e
nil
end
extracted.compact
end

def links
return @links if @links

@links ||= []
mapped_string.scan(AUTO_LINK_RE) { |_|
scheme = Regexp.last_match(1)
href = $&
punctuation = []
while href.sub!(%r{[^#{WORD_PATTERN}/-=&]$}, '')
punctuation.push($&)
if opening = BRACKETS[punctuation.last] and href.scan(opening).size > href.scan(punctuation.last).size
href << punctuation.pop
break
end
end
href = 'https://' + href unless scheme
@links << href
}
@links.uniq
end

TLD = /(?<tld>com|de|net|fr|at|ch)/.freeze
HOST_PART = %r{(?<host>[a-z\-\.0-9]+)}.freeze

def mapped_string
@string.
gsub(%r{ (#{HOST_PART}\.#{TLD}/)}) { |_|
host = Regexp.last_match['host']
"https://#{host}.#{Regexp.last_match['tld']}/"
}.
gsub(%r{ www *\. +#{HOST_PART} *\. *#{TLD}(?<path>[^<\u00A0"]+)}) { |_|
path = Regexp.last_match['path'].remove(' ')
"www.#{Regexp.last_match['host']}.#{Regexp.last_match['tld']}#{path}"
}
end
end
9 changes: 9 additions & 0 deletions lib/network_profile/extractors/custom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NetworkProfile
class Custom < DefaultProfile
self.mdi_icon = 'open-in-new'

def self.handle?(link)
true
end
end
end
Loading

0 comments on commit f24ae95

Please sign in to comment.