Class: Request

Inherits:
Object
  • Object
show all
Defined in:
lib/Request.rb

Class Method Summary collapse

Class Method Details

.body(response) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/Request.rb', line 54

def self.body(response)
  if response.nil? || (response && response.code.to_i != 200)
    nil
  else
    response.read_body
  end
end

.html(response) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/Request.rb', line 46

def self.html(response)
  if response.nil? || (response && response.code.to_i != 200)
    nil
  else
    Nokogiri::HTML(response.read_body)
  end
end

.URL(url, method = 'GET', data = nil, retryCount = 0) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/Request.rb', line 7

def self.URL(url, method = 'GET', data = nil, retryCount = 0)
    retryCount += 1
    
    uri = URI(url)
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true

    if method.upcase == "GET"
        request = Net::HTTP::Get.new(uri)
        request['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.17.375.766 Safari/537.36';
    else
        request = Net::HTTP::Post.new(uri)
        request['Content-Type'] = 'application/json'
        if !data.nil?
            request.body = JSON.dump(data)
        end
    end

    begin
      response = https.request(request)
      # 3XX Redirect
      if response.code.to_i >= 300 && response.code.to_i <= 399 && !response['location'].nil? && response['location'] != ''
          if retryCount >= 10
              raise "Error: Retry limit reached. path: #{url}"
          else
              location = response['location']
              if !location.match? /^(http)/
                  location = "#{uri.scheme}://#{uri.host}#{location}"
              end
              response = self.URL(location, method, data)
          end
      end
    rescue
      
    end

    response
end