Class: Miteru::HTTPClient

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

Constant Summary collapse

DEFAULT_UA =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
URLSCAN_UA =
"miteru/#{Miteru::VERSION}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTTPClient

Returns a new instance of HTTPClient.



14
15
16
17
18
# File 'lib/miteru/http_client.rb', line 14

def initialize
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @ssl_context = ctx
end

Instance Attribute Details

#ssl_contextObject (readonly)

Returns the value of attribute ssl_context.



13
14
15
# File 'lib/miteru/http_client.rb', line 13

def ssl_context
  @ssl_context
end

Class Method Details

.download(url, base_dir = "/tmp") ⇒ Object



26
27
28
# File 'lib/miteru/http_client.rb', line 26

def self.download(url, base_dir = "/tmp")
  new.download(url, base_dir)
end

.get(url, options = {}) ⇒ Object



39
40
41
# File 'lib/miteru/http_client.rb', line 39

def self.get(url, options = {})
  new.get url, options
end

.post(url, options = {}) ⇒ Object



47
48
49
# File 'lib/miteru/http_client.rb', line 47

def self.post(url, options = {})
  new.post url, options
end

Instance Method Details

#download(url, destination) ⇒ Object



20
21
22
23
24
# File 'lib/miteru/http_client.rb', line 20

def download(url, destination)
  down = Down::Http.new(default_options) { |client| client.headers(default_headers) }
  down.download(url, destination: destination)
  destination
end

#get(url, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/miteru/http_client.rb', line 30

def get(url, options = {})
  options = options.merge default_options

  HTTP.follow
      .timeout(write: 2, connect: 5, read: 10)
      .headers(urlscan_url?(url) ? urlscan_headers : default_headers)
      .get(url, options)
end

#post(url, options = {}) ⇒ Object



43
44
45
# File 'lib/miteru/http_client.rb', line 43

def post(url, options = {})
  HTTP.post url, options
end