omni_parse_client_ruby 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d4e6021e30306fb1f32578de382bac72ed381c4469e4607262dc9771fce78ff5
|
4
|
+
data.tar.gz: 48ec8a6616d12dfcebb97f640aee54243c484c51ab7217c1ed0e191e2bc6c836
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4da5ce3973fdbdc97da0d0179ea2047ea424e802cc2df3ea3337f85d11c586edab768ae3a14de3dac5d34f4495a9749c6c54827fb3499826ee241cee34671dcd
|
7
|
+
data.tar.gz: 2822ad0d8bc94b621d2b92d56d4c1d3888233cf9eec40ba2cd38b5390b69bacf7f978a1b4289cac96f9ff91c01d0d32475c01557a8bb8801794e965bc459b3e9
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniparseClient
|
4
|
+
# Main Class for the library
|
5
|
+
# here all modules are beeing included into class
|
6
|
+
class Base
|
7
|
+
attr_reader :html, :vcf, :fixture
|
8
|
+
|
9
|
+
include Connection
|
10
|
+
|
11
|
+
def initialize(p = {})
|
12
|
+
setup_connection(p)
|
13
|
+
@html = Parsers::HtmlParser.new(p)
|
14
|
+
@vcf = Parsers::VcfParser.new(p)
|
15
|
+
@fixture = Parsers::OmniFixture.new(p)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module OmniparseClient
|
6
|
+
# Connection module
|
7
|
+
module Connection
|
8
|
+
class RetriesLimitReached < StandardError; end
|
9
|
+
|
10
|
+
attr_reader :api_key, :port, :version, :host
|
11
|
+
|
12
|
+
DEFAULT_URL = 'www.omniparse.com'
|
13
|
+
DEFAULT_PORT = 443
|
14
|
+
DEFAULT_VERSION = '/api/v1'
|
15
|
+
# Retries count if network error occurs
|
16
|
+
MAX_RETRIES_COUNT = ENV['OMNI_MAX_RETRIES_COUNT']&.to_i || 5
|
17
|
+
# Time in seconds for consequent delays for reconnection
|
18
|
+
RETRIES_DELAYS_ARRAY =
|
19
|
+
ENV['OMNI_RETRIES_DELAYS_ARRAY']&.split(',')&.map(&:to_i) ||
|
20
|
+
[5, 30, 2 * 60, 10 * 60, 30 * 60].freeze
|
21
|
+
EXCEPTIONS_INCLUDING_MESSAGES = [
|
22
|
+
SocketError,
|
23
|
+
Net::OpenTimeout
|
24
|
+
].freeze
|
25
|
+
EXCEPTIONS_EXCLUDING_MESSAGES = [
|
26
|
+
Net::HTTPFatalError
|
27
|
+
].freeze
|
28
|
+
CLIENT_ERRORS = [
|
29
|
+
Net::HTTPInternalServerError,
|
30
|
+
Net::HTTPNotImplemented,
|
31
|
+
Net::HTTPBadGateway,
|
32
|
+
Net::HTTPServiceUnavailable,
|
33
|
+
Net::HTTPGatewayTimeout,
|
34
|
+
Net::HTTPInsufficientStorage,
|
35
|
+
Net::HTTPLoopDetected,
|
36
|
+
Net::HTTPRequestTimeout,
|
37
|
+
Net::HTTPBadRequest,
|
38
|
+
Net::HTTPNotFound
|
39
|
+
].freeze
|
40
|
+
|
41
|
+
# constructor
|
42
|
+
def setup_connection(p = {})
|
43
|
+
@host = p[:host] || DEFAULT_URL
|
44
|
+
@port = p[:port] || DEFAULT_PORT
|
45
|
+
@version = p[:version] || DEFAULT_VERSION
|
46
|
+
@api_key = p[:api_key]
|
47
|
+
end
|
48
|
+
|
49
|
+
def omni_get(request_url, params = {}, headers = {})
|
50
|
+
uri = URI(request_url)
|
51
|
+
uri.port = port
|
52
|
+
uri.query = URI.encode_www_form(params)
|
53
|
+
req = Net::HTTP::Get.new(uri)
|
54
|
+
headers.each do |header, val|
|
55
|
+
req[header] = val
|
56
|
+
end
|
57
|
+
|
58
|
+
make_request(req)
|
59
|
+
end
|
60
|
+
|
61
|
+
def omni_post(request_url, params = {}, headers = {})
|
62
|
+
uri = URI(request_url)
|
63
|
+
uri.port = port
|
64
|
+
req = Net::HTTP::Post.new(uri)
|
65
|
+
req.set_form_data(params)
|
66
|
+
headers.each do |header, val|
|
67
|
+
req[header] = val
|
68
|
+
end
|
69
|
+
|
70
|
+
make_request(req)
|
71
|
+
end
|
72
|
+
|
73
|
+
# headers
|
74
|
+
def headers
|
75
|
+
{
|
76
|
+
'Authorization' => "Bearer #{@api_key}"
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def base_url
|
81
|
+
# request = "localhost:3001//api/v1/html_parser/parse"
|
82
|
+
components = { host: host, port: port }
|
83
|
+
URI::HTTPS.build(components).to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
# TEST connection
|
87
|
+
def test_connection
|
88
|
+
request_url = base_url + test_connection_path
|
89
|
+
response = omni_get(request_url, {}, headers)
|
90
|
+
response
|
91
|
+
end
|
92
|
+
|
93
|
+
# Paths
|
94
|
+
def test_connection_path
|
95
|
+
"/#{version}/test_connection"
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def retry_on_server_error(&block)
|
101
|
+
@retry_index = 0
|
102
|
+
begin
|
103
|
+
yield block
|
104
|
+
rescue *EXCEPTIONS_INCLUDING_MESSAGES => e
|
105
|
+
raise RetriesLimitReached, e.message if retries_limit_reached?
|
106
|
+
|
107
|
+
sleep(RETRIES_DELAYS_ARRAY[@retry_index])
|
108
|
+
@retry_index += 1
|
109
|
+
retry
|
110
|
+
rescue *EXCEPTIONS_EXCLUDING_MESSAGES => e
|
111
|
+
if retries_limit_reached?
|
112
|
+
message = "#{e&.data&.code} #{e&.data&.code_type}"
|
113
|
+
raise RetriesLimitReached, message
|
114
|
+
end
|
115
|
+
|
116
|
+
sleep(RETRIES_DELAYS_ARRAY[@retry_index])
|
117
|
+
@retry_index += 1
|
118
|
+
retry
|
119
|
+
rescue Net::HTTPClientException => e
|
120
|
+
raise Net::HTTPClientException.new(e.message.tr('"', ''), e.response)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def make_request(request)
|
125
|
+
retry_on_server_error do
|
126
|
+
uri = request.uri
|
127
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
128
|
+
http.request(request)
|
129
|
+
end
|
130
|
+
res.error! if !res.is_a?(Net::HTTPSuccess) && CLIENT_ERRORS.include?(res.code_type)
|
131
|
+
|
132
|
+
res
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def retries_limit_reached?
|
137
|
+
@retry_index >= MAX_RETRIES_COUNT
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniparseClient
|
4
|
+
module Parsers
|
5
|
+
# PARSER
|
6
|
+
# @omni = Omni::Client.new(url: 'www.example.com',
|
7
|
+
# version: 'v1',
|
8
|
+
# api_key: 'token')
|
9
|
+
# @omni_html = @omni.html
|
10
|
+
class HtmlParser
|
11
|
+
attr_reader :html, :omni_parser_id
|
12
|
+
|
13
|
+
include Connection
|
14
|
+
include Loggable
|
15
|
+
|
16
|
+
def initialize(p = {})
|
17
|
+
setup_connection(p)
|
18
|
+
end
|
19
|
+
|
20
|
+
# fetching all parsers
|
21
|
+
# @omni_html.all(parser_id)
|
22
|
+
def all
|
23
|
+
self.last_request = base_url + index_path
|
24
|
+
response = omni_get(last_request, {}, headers)
|
25
|
+
self.last_response = Response.new(response)
|
26
|
+
last_response
|
27
|
+
end
|
28
|
+
|
29
|
+
# preparing / building html params
|
30
|
+
# @omni_html.prepare(parser_id, html)
|
31
|
+
def prepare(p = {})
|
32
|
+
@html = p[:html]
|
33
|
+
@omni_parser_id = p[:omni_parser_id]
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
# Post parse run
|
38
|
+
# @omni_html.parse(parser_id, html)
|
39
|
+
def parse(p = {})
|
40
|
+
return false unless p[:html]
|
41
|
+
return false unless p[:omni_parser_id]
|
42
|
+
|
43
|
+
prepare(p)
|
44
|
+
|
45
|
+
self.last_request = base_url + parse_path
|
46
|
+
response = omni_post(last_request,
|
47
|
+
params,
|
48
|
+
headers)
|
49
|
+
self.last_response = Response.new(response)
|
50
|
+
last_response
|
51
|
+
end
|
52
|
+
|
53
|
+
# Post trial run
|
54
|
+
# @omni_html.parse(parser_id, html)
|
55
|
+
def trial_run(p = {})
|
56
|
+
return false unless p[:omni_parser_id]
|
57
|
+
|
58
|
+
prepare(p)
|
59
|
+
|
60
|
+
self.last_request = base_url + trial_run_path
|
61
|
+
response = omni_post(last_request,
|
62
|
+
params,
|
63
|
+
headers)
|
64
|
+
self.last_response = Response.new(response)
|
65
|
+
last_response
|
66
|
+
end
|
67
|
+
|
68
|
+
# parsing params
|
69
|
+
def params
|
70
|
+
{
|
71
|
+
omni_parser_id: @omni_parser_id,
|
72
|
+
html: @html
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
# Paths
|
77
|
+
def parse_path
|
78
|
+
"/#{version}/html_parser/parse"
|
79
|
+
end
|
80
|
+
|
81
|
+
def trial_run_path
|
82
|
+
"/#{version}/html_parser/trial_run"
|
83
|
+
end
|
84
|
+
|
85
|
+
def index_path
|
86
|
+
"/#{version}/html_parsers"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require_relative 'omni_parse_client_ruby/loggable'
|
5
|
+
require_relative 'omni_parse_client_ruby/connection'
|
6
|
+
require_relative 'omni_parse_client_ruby/base'
|
7
|
+
require_relative 'omni_parse_client_ruby/response'
|
8
|
+
require_relative 'omni_parse_client_ruby/parsers/html_parser'
|
9
|
+
require_relative 'omni_parse_client_ruby/parsers/vcf_parser'
|
10
|
+
require_relative 'omni_parse_client_ruby/parsers/omni_fixture'
|
11
|
+
|
12
|
+
# Wrapper class
|
13
|
+
# Example:
|
14
|
+
# @omi = Omni::Client.new(url: 'www.example.com',
|
15
|
+
# version: 'v1',
|
16
|
+
# api_key: 'token')
|
17
|
+
module Omni
|
18
|
+
class Client < OmniparseClient::Base; end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omni_parse_client_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Long
|
8
|
+
- Michał Marzec
|
9
|
+
- Paweł Jermalonek
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2023-09-28 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: net-http
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.4.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 0.4.1
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: minitest-stub-const
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0.6'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.6'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: webmock
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.24'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '3.24'
|
57
|
+
description: Client for omniparse API
|
58
|
+
email: brian.long@firstmoversadvantage.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- lib/omni_parse_client_ruby.rb
|
64
|
+
- lib/omni_parse_client_ruby/base.rb
|
65
|
+
- lib/omni_parse_client_ruby/connection.rb
|
66
|
+
- lib/omni_parse_client_ruby/loggable.rb
|
67
|
+
- lib/omni_parse_client_ruby/parsers/html_parser.rb
|
68
|
+
homepage: https://www.fmadata.com/
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.1.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Client for omniparse API
|
91
|
+
test_files: []
|