Module: Bucky::Utils::Requests

Included in:
TestEquipment::Verifications::StatusChecker
Defined in:
lib/bucky/utils/requests.rb

Constant Summary collapse

USER_AGENT_STRING =
{
  pc: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
  sp: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13E238 Safari/601.1'
}.freeze

Instance Method Summary collapse

Instance Method Details

#get_response(uri, device, open_timeout, read_timeout) ⇒ Net::HTTP

Returns HttpStatusCode.

Parameters:

  • uri (String)
  • device (String)
  • open_timeout (Integer/Float)

    max wait time until open page

  • read_timeout (Integer/Float)

    max wait time until recieve response

Returns:

  • (Net::HTTP)

    HttpStatusCode



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bucky/utils/requests.rb', line 19

def get_response(uri, device, open_timeout, read_timeout)
  parsed_uri = Addressable::URI.parse(uri.to_str.strip)
  query = if parsed_uri.query.nil?
            ''
          else
            '?' + parsed_uri.query_values.map { |k, v| "#{CGI.escape(k)}=#{CGI.escape(v)}" }.join('&')
          end
  # If path is empty, add "/" e.g) http://example.com
  path = parsed_uri.path.empty? ? '/' : parsed_uri.path

  Net::HTTP.start(parsed_uri.host, parsed_uri.port, use_ssl: parsed_uri.scheme == 'https') do |http|
    http.open_timeout = open_timeout
    http.read_timeout = read_timeout
    http.get("#{path}#{query}", 'User-Agent' => USER_AGENT_STRING[device.to_sym])
  end
end