Class: Applitools::Connectivity::ServerConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/applitools/connectivity/server_connector.rb

Constant Summary collapse

DEFAULT_SERVER_URL =
'https://eyessdk.applitools.com'.freeze
SSL_CERT =
File.join(File.dirname(File.expand_path(__FILE__)), '../../../certs/cacert.pem').to_s.freeze
DEFAULT_TIMEOUT =
300
API_SESSIONS_RUNNING =
'/api/sessions/running/'.freeze
API_SINGLE_TEST =
'/api/sessions/'.freeze
HTTP_STATUS_CODES =
{
  created: 201,
  accepted: 202,
  ok: 200,
  gone: 410
}.freeze
RETRY_DELAY =
0.5
RETRY_STEP_FACTOR =
1.5
RETRY_MAX_DELAY =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ ServerConnector

Returns a new instance of ServerConnector.



28
29
30
# File 'lib/applitools/connectivity/server_connector.rb', line 28

def initialize(url = nil)
  self.server_url = url
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



24
25
26
# File 'lib/applitools/connectivity/server_connector.rb', line 24

def api_key
  @api_key
end

#endpoint_urlObject (readonly)

Returns the value of attribute endpoint_url.



25
26
27
# File 'lib/applitools/connectivity/server_connector.rb', line 25

def endpoint_url
  @endpoint_url
end

#proxyObject

Returns the value of attribute proxy.



26
27
28
# File 'lib/applitools/connectivity/server_connector.rb', line 26

def proxy
  @proxy
end

#server_urlObject

Returns the value of attribute server_url.



24
25
26
# File 'lib/applitools/connectivity/server_connector.rb', line 24

def server_url
  @server_url
end

Instance Method Details

#match_single_window(data) ⇒ Object



97
98
99
100
# File 'lib/applitools/connectivity/server_connector.rb', line 97

def match_single_window(data)
  res = match_single_window_data(data)
  Applitools::TestResults.new Oj.load(res.body)
end

#match_single_window_data(data) ⇒ Object

Raises:

  • (Applitools::EyesError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/applitools/connectivity/server_connector.rb', line 69

def match_single_window_data(data)
  # Notice that this does not include the screenshot.
  json_data = Oj.dump(data.to_hash).force_encoding('BINARY')
  body = [json_data.length].pack('L>') + json_data + data.screenshot
  # Applitools::EyesLogger.debug json_data
  begin
    Applitools::EyesLogger.debug 'Sending match data...'
    res = long_post(
      @single_check_endpoint_url,
      content_type: 'application/octet-stream',
      body: body,
      query: { agent_id: data.agent_id }
    )
  rescue Errno::EWOULDBLOCK, Faraday::ConnectionFailed
    @delays ||= request_delay(RETRY_DELAY, RETRY_STEP_FACTOR, RETRY_MAX_DELAY)
    begin
      sleep @delays.next
    rescue StopIteration
      raise Applitools::UnknownNetworkStackError.new('Unknown network stack error')
    end
    res = match_single_window_data(data)
  ensure
    @delays = nil
  end
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.headers} #{res.body}") unless res.success?
  res
end

#match_window(session, data) ⇒ Object

Raises:

  • (Applitools::EyesError)


54
55
56
57
58
59
60
61
62
63
# File 'lib/applitools/connectivity/server_connector.rb', line 54

def match_window(session, data)
  # Notice that this does not include the screenshot.
  json_data = Oj.dump(Applitools::Utils.camelcase_hash_keys(data.to_hash)).force_encoding('BINARY')
  body = [json_data.length].pack('L>') + json_data + data.screenshot
  Applitools::EyesLogger.debug 'Sending match data...'
  # Applitools::EyesLogger.debug json_data
  res = long_post(URI.join(endpoint_url, session.id.to_s), content_type: 'application/octet-stream', body: body)
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.headers}") unless res.success?
  Applitools::MatchResult.new Oj.load(res.body)
end

#set_proxy(uri, user = nil, password = nil) ⇒ Object



42
43
44
# File 'lib/applitools/connectivity/server_connector.rb', line 42

def set_proxy(uri, user = nil, password = nil)
  self.proxy = Proxy.new uri, user, password
end

#start_session(session_start_info) ⇒ Object

Raises:

  • (Applitools::EyesError)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/applitools/connectivity/server_connector.rb', line 102

def start_session(session_start_info)
  request_body = Oj.dump(
    startInfo: Applitools::Utils.camelcase_hash_keys(session_start_info.to_hash)
  )
  res = post(
    endpoint_url, body: request_body
  )
  raise Applitools::EyesError.new("Request failed: #{res.status} #{res.body} #{request_body}") unless res.success?

  response = Oj.load(res.body)
  Applitools::Session.new(response['id'], response['url'], res.status == HTTP_STATUS_CODES[:created])
end

#stop_session(session, aborted = nil, save = false) ⇒ Object

Raises:

  • (Applitools::EyesError)


115
116
117
118
119
120
121
# File 'lib/applitools/connectivity/server_connector.rb', line 115

def stop_session(session, aborted = nil, save = false)
  res = long_delete(URI.join(endpoint_url, session.id.to_s), query: { aborted: aborted, updateBaseline: save })
  raise Applitools::EyesError.new("Request failed: #{res.status}") unless res.success?

  response = Oj.load(res.body)
  Applitools::TestResults.new(response)
end