Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
public-apis
Commits
5fbf817c
Commit
5fbf817c
authored
Jul 12, 2017
by
Dave Machado
Browse files
check for duplicate urls
parent
80e8997d
Changes
1
Hide whitespace changes
Inline
Side-by-side
build/validate_links.rb
View file @
5fbf817c
#!/usr/bin/env ruby
#!/usr/bin/env ruby
require
'
farada
y'
require
'
httpart
y'
require
'uri'
require
'uri'
allowed_codes
=
[
200
,
302
,
403
]
allowed_codes
=
[
200
,
302
,
403
]
args
=
ARGV
args
=
ARGV
filename
=
args
[
0
]
filename
=
args
[
0
]
fail_flag
=
false
fail_flag
=
false
contents
=
File
.
open
(
filename
,
'rb'
)
{
|
f
|
f
.
read
}
contents
=
File
.
open
(
filename
,
'rb'
)
{
|
f
|
f
.
read
}
links
=
URI
.
extract
(
contents
,
[
'http'
,
'https'
])
raw_links
=
URI
.
extract
(
contents
,
[
'http'
,
'https'
])
dup
=
links
.
select
{
|
element
|
links
.
count
(
element
)
>
1
}
# Remove trailing ')' from entry URLs
links
=
[]
raw_links
.
each
do
|
link
|
if
link
.
end_with?
(
')'
)
links
.
push
(
link
[
0
...-
1
])
else
links
.
push
(
link
)
end
end
# Fail on any duplicate elements
dup
=
links
.
select
{
|
element
|
links
.
count
(
element
)
>
1
}
if
dup
.
uniq
.
length
>
0
if
dup
.
uniq
.
length
>
0
dup
.
uniq
.
each
do
|
link
|
dup
.
uniq
.
each
do
|
e
|
if
link
.
end_with?
(
')'
)
puts
"Duplicate link:
#{
e
}
"
puts
link
[
0
...-
1
]
end
end
end
exit
(
1
)
fail_flag
=
true
end
end
# Remove any duplicates from array
links
=
links
.
uniq
count
=
0
total
=
links
.
length
fails
=
[]
# GET each link and check for valid response code from allowed_codes
links
.
each
do
|
link
|
links
.
each
do
|
link
|
if
link
.
end_with?
(
')'
)
begin
link
=
link
[
0
...-
1
]
count
+=
1
end
puts
"(
#{
count
}
/
#{
total
}
)
#{
link
}
"
res
=
Faraday
.
get
(
link
)
res
=
HTTParty
.
get
(
link
,
timeout:
10
)
if
!
allowed_codes
.
include?
(
res
.
status
)
if
!
allowed_codes
.
include?
(
res
.
code
)
puts
"(
#{
res
.
status
}
):
#{
link
}
"
fails
.
push
(
"(
#{
res
.
code
}
):
#{
link
}
"
)
fail_flag
=
true
else
puts
"
\t
(
#{
res
.
code
}
)"
end
rescue
puts
"FAIL: (
#{
res
.
code
}
)
#{
link
}
"
fails
.
push
(
"(
#{
res
.
code
}
):
#{
link
}
"
)
fail_flag
=
true
fail_flag
=
true
end
end
end
end
fails
.
each
do
|
e
|
puts
e
end
if
fail_flag
if
fail_flag
exit
(
1
)
exit
(
1
)
else
else
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment