coelacanth 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +9 -1
- data/lib/coelacanth/client.rb +47 -17
- data/lib/coelacanth/version.rb +1 -1
- data/lib/coelacanth.rb +8 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29b00f207c71fb604269ddb59b70064509dce9a5abc189c5ba98a17fc3701815
|
4
|
+
data.tar.gz: fe1487e4f8b15ebbdc6312dce95d2e50b3338871fedb1cb5c49ec8098d97951c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 462bbd1ee309ca534b42bdac616c4145f7fc3320971d391c025d7bd9080850b61fb773f8a84d5286e73321c90732f897c9b1760c68c6542d5bd3abdfba5b23d9
|
7
|
+
data.tar.gz: ec1071051319687f27e989871cc4cc78a90fbbf2e07c52310e7150fa651efd908a3a2cde8424ed7abfe03755afca3661a6b64477e37be8f7b81d8427caf02f07
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
coelacanth (0.1.
|
4
|
+
coelacanth (0.1.5)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
addressable (2.8.7)
|
10
10
|
public_suffix (>= 2.0.2, < 7.0)
|
11
|
+
ansi (1.5.0)
|
11
12
|
ast (2.4.2)
|
12
13
|
concurrent-ruby (1.3.4)
|
13
14
|
diff-lcs (1.4.4)
|
@@ -18,6 +19,9 @@ GEM
|
|
18
19
|
websocket-driver (~> 0.7)
|
19
20
|
json (2.7.1)
|
20
21
|
language_server-protocol (3.17.0.3)
|
22
|
+
oga (3.4)
|
23
|
+
ast
|
24
|
+
ruby-ll (~> 2.1)
|
21
25
|
parallel (1.24.0)
|
22
26
|
parser (3.3.0.5)
|
23
27
|
ast (~> 2.4.1)
|
@@ -54,6 +58,9 @@ GEM
|
|
54
58
|
unicode-display_width (>= 2.4.0, < 3.0)
|
55
59
|
rubocop-ast (1.31.2)
|
56
60
|
parser (>= 3.3.0.4)
|
61
|
+
ruby-ll (2.1.3)
|
62
|
+
ansi
|
63
|
+
ast
|
57
64
|
ruby-progressbar (1.13.0)
|
58
65
|
unicode-display_width (2.5.0)
|
59
66
|
webrick (1.8.1)
|
@@ -68,6 +75,7 @@ PLATFORMS
|
|
68
75
|
DEPENDENCIES
|
69
76
|
coelacanth!
|
70
77
|
ferrum (~> 0.15)
|
78
|
+
oga (~> 3.4)
|
71
79
|
rake (~> 13.0)
|
72
80
|
rspec (~> 3.0)
|
73
81
|
rubocop (~> 1.21)
|
data/lib/coelacanth/client.rb
CHANGED
@@ -1,46 +1,76 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "ferrum"
|
4
|
+
require "oga"
|
4
5
|
|
5
6
|
module Coelacanth
|
6
7
|
# Coelacanth::Client
|
7
8
|
class Client
|
8
|
-
def initialize
|
9
|
-
@config =
|
9
|
+
def initialize(url = nil)
|
10
|
+
@config = Coelacanth.config
|
11
|
+
@url = url if url && valid_url?(url)
|
10
12
|
end
|
11
13
|
|
12
|
-
def valid_url?(url)
|
13
|
-
|
14
|
+
def valid_url?(url = nil)
|
15
|
+
@url = url if url
|
16
|
+
uri = URI.parse(@url)
|
14
17
|
uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
|
15
18
|
rescue URI::InvalidURIError
|
16
19
|
false
|
17
20
|
end
|
18
21
|
|
19
|
-
def resolve_redirect(url, limit = 10)
|
22
|
+
def resolve_redirect(url = nil, limit = 10)
|
23
|
+
@url = url if url && valid_url?(url)
|
20
24
|
raise Coelacanth::DeepRedirectError, "Too many redirect" if limit.zero?
|
21
|
-
raise Coelacanth::RedirectError, "Url or location is nil" if url.nil?
|
25
|
+
raise Coelacanth::RedirectError, "Url or location is nil" if @url.nil?
|
22
26
|
|
23
|
-
|
27
|
+
get_response(@url)
|
28
|
+
handle_response(@origin_response, limit)
|
29
|
+
end
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
else
|
29
|
-
raise Coelacanth::RedirectError
|
30
|
-
end
|
31
|
+
def oga(url = nil)
|
32
|
+
@url = url if url && valid_url?(url)
|
33
|
+
Oga.parse_xml(get_response(@url))
|
31
34
|
end
|
32
35
|
|
33
|
-
def get_response(url)
|
36
|
+
def get_response(url = nil)
|
37
|
+
@url = url if url && valid_url?(url)
|
34
38
|
if @config.read("use_remote_client")
|
35
|
-
|
36
|
-
remote_client.body
|
39
|
+
response_by_remote_client
|
37
40
|
else
|
38
|
-
|
41
|
+
response_by_net_http
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
42
45
|
private
|
43
46
|
|
47
|
+
def handle_response(response, limit)
|
48
|
+
codes = Net::HTTPResponse::CODE_CLASS_TO_OBJ.invert
|
49
|
+
case @status_code.to_s
|
50
|
+
when /^#{codes[Net::HTTPSuccess]}\d\d$/
|
51
|
+
@url
|
52
|
+
when /^#{codes[Net::HTTPRedirection]}\d\d$/
|
53
|
+
@url = response["location"]
|
54
|
+
resolve_redirect(response["location"], limit - 1)
|
55
|
+
else
|
56
|
+
raise Coelacanth::RedirectError
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def response_by_remote_client
|
61
|
+
remote_client.goto(@url)
|
62
|
+
@status_code = remote_client.network.status
|
63
|
+
@origin_response = remote_client
|
64
|
+
remote_client.body
|
65
|
+
end
|
66
|
+
|
67
|
+
def response_by_net_http
|
68
|
+
response = Net::HTTP.get_response(URI.parse(@url))
|
69
|
+
@status_code = response.code
|
70
|
+
@origin_response = response
|
71
|
+
response.body
|
72
|
+
end
|
73
|
+
|
44
74
|
def remote_client
|
45
75
|
if @remote_client.nil?
|
46
76
|
headers = @config.read("remote_client.headers")
|
data/lib/coelacanth/version.rb
CHANGED
data/lib/coelacanth.rb
CHANGED
@@ -12,9 +12,15 @@ module Coelacanth
|
|
12
12
|
class DeepRedirectError < StandardError; end
|
13
13
|
|
14
14
|
def self.analyze(url)
|
15
|
-
Client.
|
15
|
+
@client = Client.new(url)
|
16
|
+
@client.resolve_redirect
|
16
17
|
{
|
17
|
-
|
18
|
+
remote_client: @config.read("use_remote_client"),
|
19
|
+
oga: @client.oga
|
18
20
|
}
|
19
21
|
end
|
22
|
+
|
23
|
+
def self.config
|
24
|
+
@config ||= Configure.new
|
25
|
+
end
|
20
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coelacanth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yusuke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
coelacanth is a gem that allows you to easily parse and analyze web pages,
|