Class: Funky::Connection::Base Private

Inherits:
Object
  • Object
show all
Defined in:
lib/funky/connections/base.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

API

Class Method Summary collapse

Class Method Details

.get_http_request(uri) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/funky/connections/base.rb', line 21

def self.get_http_request(uri)
  Net::HTTP::Get.new(uri.request_uri)
end

.instrument(uri, request, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/funky/connections/base.rb', line 8

def self.instrument(uri, request, &block)
  data = {
    request: request,
    method: request.method,
    request_uri: uri
  }
  if defined?(ActiveSupport::Notifications)
    ActiveSupport::Notifications.instrument 'request.funky', data, &block
  else
    block.call(data)
  end
end

.json_for(uri, max_retries = 3) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
# File 'lib/funky/connections/base.rb', line 52

def self.json_for(uri, max_retries = 3)
  response = response_for(get_http_request(uri), uri)
  JSON.parse(response.body, symbolize_names: true)
end

.response_for(http_request, uri, max_retries = 5) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/funky/connections/base.rb', line 25

def self.response_for(http_request, uri, max_retries = 5)
  response = instrument(uri, http_request) do |data|
    data[:response] = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
      http.request http_request
    end
  end

  if response.is_a? Net::HTTPSuccess
    response
  elsif response.is_a? Net::HTTPServerError
    sleep_and_retry_response_for(http_request, uri, max_retries, response.body)
  else
    raise ContentNotFound, "Error #{response.code}: #{response.body}"
  end
rescue SocketError => e
  sleep_and_retry_response_for(http_request, uri, max_retries, e.message)
end

.sleep_and_retry_response_for(http_request, uri, retries, message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
50
# File 'lib/funky/connections/base.rb', line 43

def self.sleep_and_retry_response_for(http_request, uri, retries, message)
  if retries > 0
    sleep (6 - retries) ** 2
    response_for http_request, uri, retries - 1
  else
    raise ConnectionError, message
  end
end