Class: Quandl::Database
- Includes:
- Operations::Get, Operations::List
- Defined in:
- lib/quandl/model/database.rb
Instance Method Summary collapse
- #bulk_download_to_file(file_or_folder_path, options = {}) ⇒ Object
- #bulk_download_url(options = {}) ⇒ Object
- #datasets(options = {}) ⇒ Object
Methods inherited from ModelBase
#column_names, #data_fields, #initialize, #inspect, #to_a
Constructor Details
This class inherits a constructor from Quandl::ModelBase
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Quandl::ModelBase
Instance Method Details
#bulk_download_to_file(file_or_folder_path, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/quandl/model/database.rb', line 25 def bulk_download_to_file(file_or_folder_path, = {}) fail(QuandlError, 'You must specific a file handle or folder to write to.') if file_or_folder_path.blank? # Retrieve the location of the bulk download url url = bulk_download_url({ path_only: true }.merge()) download_url = Quandl::Connection.request(:get, url) do |response, _request, _result, &_block| if response.code == 302 response.headers[:location] else Quandl::Connection.handle_api_error(response) if response fail(QuandlError, 'Unexpected result when fetching bulk download URI.') end end uri = URI.parse(download_url) # Check that we can write to the directory file = file_or_folder_path unless file_or_folder_path.is_a?(File) file_or_folder_path = Pathname.new(file_or_folder_path.to_s).join(File.basename(uri.path)) file = File.open(file_or_folder_path, 'wb') end # Download the file begin http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') http.request_get(uri.request_uri) do |resp| resp.read_body do |segment| file.write(segment) end end ensure file.close end file.path end |
#bulk_download_url(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/quandl/model/database.rb', line 10 def bulk_download_url( = {}) .assert_valid_keys(:download_type, :path_only) url = self.class.default_path + '/data' url = Quandl::ApiConfig.api_base + '/' + url unless [:path_only] url = Quandl::Util.constructed_path(url, id: database_code) params = {} params['download_type'] = [:download_type] if [:download_type] params['api_key'] = Quandl::ApiConfig.api_key if Quandl::ApiConfig.api_key url += '?' + params.to_query if params.any? url end |