coelacanth 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25c5b81d90d4db4cfe123670105ad4cf91f3ff8a54ffa8c242385a39d1ef9827
4
- data.tar.gz: 567ffbebc172d3747f85dd158610bc4e3baabadae8387306912f7bf4dd1dcc7f
3
+ metadata.gz: 29b00f207c71fb604269ddb59b70064509dce9a5abc189c5ba98a17fc3701815
4
+ data.tar.gz: fe1487e4f8b15ebbdc6312dce95d2e50b3338871fedb1cb5c49ec8098d97951c
5
5
  SHA512:
6
- metadata.gz: 3c05022d1bcca228c5c977776f89057087010dfd5da8617a529984d7cdaedcec8418a5167ef59afef2f0e628fbda425d4fffec528ade81413cf36a7fb6e1cbb4
7
- data.tar.gz: e0560ede407f54ea6d840c9096bdfd67557832b4fd797c2fa60746b35f2295a55b5f016b94a7957704eb7457eb88f2710a3b76b2b01b5c2277d604140c6d7fba
6
+ metadata.gz: 462bbd1ee309ca534b42bdac616c4145f7fc3320971d391c025d7bd9080850b61fb773f8a84d5286e73321c90732f897c9b1760c68c6542d5bd3abdfba5b23d9
7
+ data.tar.gz: ec1071051319687f27e989871cc4cc78a90fbbf2e07c52310e7150fa651efd908a3a2cde8424ed7abfe03755afca3661a6b64477e37be8f7b81d8427caf02f07
data/Gemfile CHANGED
@@ -9,3 +9,4 @@ gem "ferrum", "~> 0.15"
9
9
  gem "rake", "~> 13.0"
10
10
  gem "rspec", "~> 3.0"
11
11
  gem "rubocop", "~> 1.21"
12
+ gem "oga", "~> 3.4"
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- coelacanth (0.1.4)
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)
@@ -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 = Configure.new
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
- uri = URI.parse(url)
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
- response = get_response(url)
27
+ get_response(@url)
28
+ handle_response(@origin_response, limit)
29
+ end
24
30
 
25
- case response
26
- when Net::HTTPSuccess then url
27
- when Net::HTTPRedirection then resolve_redirect(response["location"], limit - 1)
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
- remote_client.goto(url)
36
- remote_client.body
39
+ response_by_remote_client
37
40
  else
38
- Net::HTTP.get_response(url)
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")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coelacanth
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
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.get_response(url)
15
+ @client = Client.new(url)
16
+ @client.resolve_redirect
16
17
  {
17
- todo: "implement me"
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
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-28 00:00:00.000000000 Z
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,