Class: Dist::YamlLoader

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/dist/yaml_loader.rb

Instance Method Summary collapse

Methods included from Error

#error, #error_at

Constructor Details

#initialize(options = {}) ⇒ YamlLoader

Returns a new instance of YamlLoader.



4
5
6
7
8
9
10
# File 'lib/dist/yaml_loader.rb', line 4

def initialize(options = {})
  @local = options[:local]
  unless @local
    require "net/https"
    require "uri"
  end
end

Instance Method Details

#load(filename) ⇒ Object



12
13
14
# File 'lib/dist/yaml_loader.rb', line 12

def load(filename)
  @local ? load_from_local_file(filename) : load_from_url(filename)
end

#load_from_local_file(filename) ⇒ Object



16
17
18
# File 'lib/dist/yaml_loader.rb', line 16

def load_from_local_file(filename)
  YAML.load_file File.expand_path("../../#{filename}.yml", __FILE__)
end

#load_from_url(filename) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dist/yaml_loader.rb', line 20

def load_from_url(filename)
  uri = URI.parse("https://raw.githubusercontent.com/manastech/dist/master/lib/#{filename}.yml")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  if response.code != '200'
    error "fetching '#{uri}' got status code #{response.code}. You can try running dist with --local if the problem persists."
  end
  YAML.load response.body
rescue SocketError => ex
  error "couldn't fetch '#{uri}'. You can try running dist --local if you don't have internet access.\n(Exception is: #{ex.message})"
end