Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/github_link_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'net/http'
require 'open-uri'

module GithubLinkValidator
GITHUB_URL_REGEX = /\A(http:\/\/|https:\/\/|www.)*github.com\/([\w\-]+)\/([\w\-]+)(\/[\w\-]*\/*)*(.git)*\z/

def github_url?(url)
url =~ GITHUB_URL_REGEX
end

def append_https(url)
if url.start_with?('https')
url
elsif url.start_with?('http')
url.gsub!(/http:/, 'https:')
else
url = 'https://' + url
end
end

def remove_www(url)
return url.gsub!(/www./, '') if url.match(/www./)
url
end

def valid_url?(url)
github_preferred_url = remove_www(append_https(url))

begin
response = Net::HTTP.get_response(URI(github_preferred_url))
response.code == "200"
rescue
false
end
end

def parse_github_url(url)
url =~ GITHUB_URL_REGEX
github_org = $2
github_repo = $3

{github_org: github_org, github_repo: github_repo}
end

def get_github_details(url)
if github_url?(url) && valid_url?(url)
parse_github_url(url)
end
end
end
75 changes: 75 additions & 0 deletions spec/lib/github_link_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'spec_helper'
require "#{Rails.root}/lib/github_link_validator"

describe GithubLinkValidator do
include GithubLinkValidator

let (:valid_github_urls) { ['http://www.github.com/codemontagehq/codemontage',
'https://www.github.com/courte/codemontage',
'http://github.com/dbness/codemontage',
'www.github.com/codemontagehq/codemontage',
'https://github.com/courte/codemontage',
'github.com/dbness/codemontage',
'https://github.com/CodeMontageHQ/codemontage/pull/269'] }

let (:valid_github_url) { 'www.github.com/codemontagehq/codemontage' }

describe '#github_url?' do

it 'returns true for accurate github repo urls' do
good_github_urls = valid_github_urls + ['https://github.com/fake/repo', 'www.github.com/banana/pudding-cup']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😋!


good_github_urls.each do |github_url|
expect(github_url?(github_url)).to be_true
end
end

it 'returns false for inaccurate github repo urls' do
urls = ['http://www.cnn.com', 'http://www.google.com/search?q=this+american+life', 'github.com/courte', 'https://commonplacebooks.com']

urls.each do |url|
expect(github_url?(url)).to be_false
end
end
end

describe '#valid_url?' do

it 'returns true for urls that are ok or redirect' do

valid_github_urls.each do |valid_url|
expect(valid_url?(valid_url)).to be_true
end
end

it 'returns false for invalid urls' do
invalid_urls = ["http://www.ajiofj.io", "https://bananacrabcakepalace.net", "https://github.com/woinvewnus/cuxywwe", "www.github.com/courte/notarepo", "github.com/ospreys-2014/phase-1-guide"]

invalid_urls.each do |invalid_url|
expect(valid_url?(invalid_url)).to be_false
end
end
end

describe '#parse_github_url' do

it 'returns the github org, pulled from the input url' do
url = 'www.github.com/codemontagehq/codemontage'
github_details = parse_github_url(valid_github_url)
expect(github_details[:github_org]).to eq('codemontagehq')
end

it 'returns the github repo, pulled from the input url' do
github_details = parse_github_url(valid_github_url)
expect(github_details[:github_repo]).to eq('codemontage')
end
end

describe '#get_github_details' do

it "returns the github org and repo in a hash" do
github_details = get_github_details(valid_github_url)
expect(github_details).to eq({github_org: 'codemontagehq', github_repo: 'codemontage'})
end
end
end