Class: Yawast::Shared::Http

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

Class Method Summary collapse

Class Method Details

.get(uri) ⇒ Object



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

def self.get(uri)
  body = ''

  begin
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    res = req.request_get(uri.path, get_headers)
    body = res.read_body
  rescue
    #do nothing for now
  end

  body
end

.get_headersObject



56
57
58
59
60
61
62
63
64
# File 'lib/shared/http.rb', line 56

def self.get_headers
  if @cookie == nil
    headers = { 'User-Agent' => HTTP_UA }
  else
    headers = { 'User-Agent' => HTTP_UA, 'Cookie' => @cookie }
  end

  headers
end

.get_http(uri) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/shared/http.rb', line 46

def self.get_http(uri)
  if @proxy
    req = Net::HTTP.new(uri.host, uri.port, @proxy_host, @proxy_port)
  else
    req = Net::HTTP.new(uri.host, uri.port)
  end

  req
end

.get_status_code(uri) ⇒ Object



39
40
41
42
43
44
# File 'lib/shared/http.rb', line 39

def self.get_status_code(uri)
  req = get_http(uri)
  req.use_ssl = uri.scheme == 'https'
  res = req.head(uri.path, get_headers)
  res.code
end

.head(uri) ⇒ Object



18
19
20
21
22
# File 'lib/shared/http.rb', line 18

def self.head(uri)
  req = get_http(uri)
  req.use_ssl = uri.scheme == 'https'
  req.head(uri.path, get_headers)
end

.setup(proxy, cookie) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/shared/http.rb', line 4

def self.setup(proxy, cookie)
  if proxy != nil && proxy.include?(':')
    @proxy_host, @proxy_port = proxy.split(':')
    @proxy = true

    puts "Using Proxy: #{proxy}"
  else
    @proxy = false
  end

  @cookie = cookie
  puts "Using Cookie: #{@cookie}" if @cookie != nil
end