class NativeExtFetcher::Fetcher

Constants

InvalidDigest

Attributes

download_path[R]

Public Instance Methods

config(name, host, lib_path, checksums, options = {}) click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 7
def config(name, host, lib_path, checksums, options = {})
  @lib_path = File.expand_path(lib_path)

  raise ArgumentError, 'Checksums must be a hash' unless Hash === checksums
  raise ArgumentError, 'Library path not found ' unless Dir.exists?(@lib_path)

  @host = host
  @checksums = checksums
  @options = options

  @download_path = expand_download_path

  if options[:static]
    @file_ext = Platform.native_extension_static_file_ext
    @file_postfix = Platform.native_extension_static_file_postfix
  else
    @file_ext = Platform.native_extension_file_ext
    @file_postfix = Platform.native_extension_file_postfix
  end
end
fetch!(library) click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 28
def fetch!(library)
  File.open(library_local_path(library), 'w+') do |file|
    file.truncate 0 # empty file

    uri = library_request_uri(library)
    digest = http_client.get_file(file, uri)

    unless digest == expected_digest
      raise InvalidDigest, "invalid checksums for #{library} from #{uri}"
    end
  end
end

Protected Instance Methods

expand_download_path() click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 47
def expand_download_path
  File.join(@lib_path, 'native', Platform.native_extension_tuple.join('-')).tap do |native_path|
    FileUtils.mkdir_p native_path
  end
end
expected_digest() click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 43
def expected_digest
  @checksums[Platform.native_extension_key]
end
library_local_path(library) click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 53
def library_local_path(library)
  File.join @download_path, "#{library}#{@file_ext}"
end
library_request_uri(library) click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 57
def library_request_uri(library)
  "https://#{@host}/#{library}#{@file_postfix}"
end

Private Instance Methods

http_client() click to toggle source
# File lib/native_ext_fetcher/fetcher.rb, line 63
def http_client
  @http_client ||= Http.new(@options)
end