Sha256: b1b3f8fb6fc7f362a45c8661bfc78aadb93482febb7c719b336643217c9efa09

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require "http"
require "down/http"
require "securerandom"

module Miteru
  class HTTPClient
    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"
    attr_reader :ssl_context
    def initialize
      ctx = OpenSSL::SSL::SSLContext.new
      ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_context = ctx
    end

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

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

    def get(url)
      HTTP.headers(default_headers).get(url, default_options)
    end

    def self.get(url)
      new.get url
    end

    private

    def default_headers
      { user_agent: DEFAULT_UA }
    end

    def default_options
      { ssl_context: ssl_context }
    end

    def save_filename
      "#{SecureRandom.alphanumeric}.zip"
    end

    def download_to(base_dir, save_filename)
      "#{base_dir}/#{save_filename}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
miteru-0.3.2 lib/miteru/http_client.rb