Module: OmniparseClient::Connection

Included in:
Base, Parsers::HtmlParser
Defined in:
lib/omni_parse_client_ruby/connection.rb

Overview

Connection module

Defined Under Namespace

Classes: RetriesLimitReached

Constant Summary collapse

DEFAULT_URL =
'www.omniparse.com'
DEFAULT_PORT =
443
DEFAULT_VERSION =
'/api/v1'
MAX_RETRIES_COUNT =

Retries count if network error occurs

ENV['OMNI_MAX_RETRIES_COUNT']&.to_i || 5
RETRIES_DELAYS_ARRAY =

Time in seconds for consequent delays for reconnection

ENV['OMNI_RETRIES_DELAYS_ARRAY']&.split(',')&.map(&:to_i) ||
[5, 30, 2 * 60, 10 * 60, 30 * 60].freeze
EXCEPTIONS_INCLUDING_MESSAGES =
[
  SocketError,
  Net::OpenTimeout
].freeze
EXCEPTIONS_EXCLUDING_MESSAGES =
[
  Net::HTTPFatalError
].freeze
CLIENT_ERRORS =
[
  Net::HTTPInternalServerError,
  Net::HTTPNotImplemented,
  Net::HTTPBadGateway,
  Net::HTTPServiceUnavailable,
  Net::HTTPGatewayTimeout,
  Net::HTTPInsufficientStorage,
  Net::HTTPLoopDetected,
  Net::HTTPRequestTimeout,
  Net::HTTPBadRequest,
  Net::HTTPNotFound
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/omni_parse_client_ruby/connection.rb', line 10

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/omni_parse_client_ruby/connection.rb', line 10

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/omni_parse_client_ruby/connection.rb', line 10

def port
  @port
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/omni_parse_client_ruby/connection.rb', line 10

def version
  @version
end

Instance Method Details

#base_urlObject



80
81
82
83
84
# File 'lib/omni_parse_client_ruby/connection.rb', line 80

def base_url
  # request = "localhost:3001//api/v1/html_parser/parse"
  components = { host: host, port: port }
  URI::HTTPS.build(components).to_s
end

#headersObject

headers



74
75
76
77
78
# File 'lib/omni_parse_client_ruby/connection.rb', line 74

def headers
  {
    'Authorization' => "Bearer #{@api_key}"
  }
end

#omni_get(request_url, params = {}, headers = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omni_parse_client_ruby/connection.rb', line 49

def omni_get(request_url, params = {}, headers = {})
  uri = URI(request_url)
  uri.port = port
  uri.query = URI.encode_www_form(params)
  req = Net::HTTP::Get.new(uri)
  headers.each do |header, val|
    req[header] = val
  end

  make_request(req)
end

#omni_post(request_url, params = {}, headers = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/omni_parse_client_ruby/connection.rb', line 61

def omni_post(request_url, params = {}, headers = {})
  uri = URI(request_url)
  uri.port = port
  req = Net::HTTP::Post.new(uri)
  req.set_form_data(params)
  headers.each do |header, val|
    req[header] = val
  end

  make_request(req)
end

#setup_connection(p = {}) ⇒ Object

constructor



42
43
44
45
46
47
# File 'lib/omni_parse_client_ruby/connection.rb', line 42

def setup_connection(p = {})
  @host       = p[:host] || DEFAULT_URL
  @port       = p[:port] || DEFAULT_PORT
  @version    = p[:version] || DEFAULT_VERSION
  @api_key    = p[:api_key]
end

#test_connectionObject

TEST connection



87
88
89
90
91
# File 'lib/omni_parse_client_ruby/connection.rb', line 87

def test_connection
  request_url = base_url + test_connection_path
  response = omni_get(request_url, {}, headers)
  response
end

#test_connection_pathObject

Paths



94
95
96
# File 'lib/omni_parse_client_ruby/connection.rb', line 94

def test_connection_path
  "/#{version}/test_connection"
end