module Bucky::Utils::Requests

Constants

USER_AGENT_STRING

Public Instance Methods

get_response(uri, device, open_timeout, read_timeout) click to toggle source

@param [String] uri @param [String] device @param [Integer/Float] open_timeout max wait time until open page @param [Integer/Float] read_timeout max wait time until recieve response @return [Net::HTTP] HttpStatusCode

# 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 = parsed_uri.query ? "?#{parsed_uri.query}" : ''
  # 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