Method: TreasureData::Updater::ModuleDefinition#fetch

Defined in:
lib/td/updater.rb

#fetch(url) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/td/updater.rb', line 111

def fetch(url)
  require 'net/http'
  require 'openssl'

  http_class = Command.get_http_class

  # open-uri can't treat 'http -> https' redirection and
  # Net::HTTP.get_response can't get response from HTTPS endpoint.
  # So we use following code to avoid these issues.
  uri = URI(url)
  response =
    if uri.scheme == 'https'
      http = http_class.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.request(Net::HTTP::Get.new(uri.path))
    else
      http_class.get_response(uri)
    end

  case response
  when Net::HTTPSuccess then response.body
  when Net::HTTPRedirection then fetch(response['Location'])
  else
    raise_error "An error occurred when fetching from '#{url}'."
    response.error!
  end
end