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.



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

def ssl_context
  @ssl_context
end

Class Method Details

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



49
50
51
# File 'lib/miteru/http_client.rb', line 49

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

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



53
54
55
# File 'lib/miteru/http_client.rb', line 53

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

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



61
62
63
# File 'lib/miteru/http_client.rb', line 61

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

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



57
58
59
# File 'lib/miteru/http_client.rb', line 57

def 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



35
36
37
38
39
40
41
42
# File 'lib/miteru/http_client.rb', line 35

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

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



26
27
28
29
30
31
32
33
# File 'lib/miteru/http_client.rb', line 26

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

  HTTP.follow
    .timeout(3)
    .headers(urlscan_url?(url) ? urlscan_headers : default_headers)
    .head(url, options)
end

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



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

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