Module: CachedResponses

Included in:
Arachnid2::Typhoeus
Defined in:
lib/arachnid2/cached_responses.rb

Constant Summary collapse

CACHE_SERVICE_URL =
ENV['ARACHNID_CACHED_SERVICE_ADDRESS'].freeze

Instance Method Summary collapse

Instance Method Details

#check_configObject



38
39
40
# File 'lib/arachnid2/cached_responses.rb', line 38

def check_config
  CACHE_SERVICE_URL.nil?
end

#load_data(_url, _options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arachnid2/cached_responses.rb', line 6

def load_data(_url, _options)
  return if check_config

  uri = URI("#{CACHE_SERVICE_URL}/typhoeus_responses?url=#{@url}&options=#{@options}")
  req = Net::HTTP::Get.new(uri)
  req['Accept'] = 'json'
  Net::HTTP.start(uri.hostname, uri.port) do |http|
    response = http.request(req)
    return nil if response.code != '200'

    body = ::JSON.parse(response.body)
    responses_list = Base64.decode64(body['encrypted_response'])
    return Marshal.load responses_list # here we get an Array of `Typhoeus::Response`s
  end
rescue StandardError
  nil
end

#put_cached_data(url, options, data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arachnid2/cached_responses.rb', line 24

def put_cached_data(url, options, data)
  return if check_config

  uri = URI("#{CACHE_SERVICE_URL}/typhoeus_responses")

  header = { 'Content-Type': 'application/json' }
  req = Net::HTTP::Post.new(uri, header)
  processed_data = Base64.encode64(Marshal.dump(data))
  req.body = { url: url, options: options, encrypted_response: processed_data }.to_json
  Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end
end