Class: Sportsflix::Utils::HTTP

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

Constant Summary collapse

DEFAULT_USER_AGENTS =
[
    'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3099.0 Safari/537.36'
]
DEFAULT_HEADERS =
{
    'User-Agent' => DEFAULT_USER_AGENTS.sample
}

Instance Method Summary collapse

Constructor Details

#initializeHTTP



21
22
23
24
# File 'lib/sportsflix/utils/http.rb', line 21

def initialize
  @headers = DEFAULT_HEADERS
  @params  = {}
end

Instance Method Details

#get(raw_url, extra_params = {}, extra_headers = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sportsflix/utils/http.rb', line 26

def get(raw_url, extra_params = {}, extra_headers = {})
  uri       = URI.parse(raw_url)
  uri.query = URI.encode_www_form(@params.merge(extra_params))
  req       = Net::HTTP::Get.new(uri, @headers.merge(extra_headers))

  session         = Net::HTTP.new(uri.host, uri.port)
  session.use_ssl = true if req.uri.scheme == 'https'
  res             = session.start {|http| http.request(req)}

  get_lambda = lambda {|url| get(url, extra_params, extra_headers)}

  with_cf_bypass(res, get_lambda)
end

#post(raw_url, body, extra_params = {}, extra_headers = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sportsflix/utils/http.rb', line 40

def post(raw_url, body, extra_params = {}, extra_headers = {})
  uri            = URI.parse(raw_url)
  uri.query      = URI.encode_www_form(@params.merge(extra_params))
  merged_headers = @headers
                       .merge({'Content-Type' => 'application/x-www-form-urlencoded'})
                       .merge(extra_headers)
  req            = Net::HTTP::Post.new(uri, merged_headers)

  session         = Net::HTTP.new(uri.host, uri.port)
  session.use_ssl = true if req.uri.scheme == 'https'
  res             = session.start {|http| http.request(req)}

  post_lambda = lambda {|url| post(url, body, extra_params, extra_headers)}

  with_cf_bypass(res, post_lambda)
end