Class: Quandl::Database

Inherits:
ModelBase show all
Includes:
Operations::Get, Operations::List
Defined in:
lib/quandl/model/database.rb

Instance Method Summary collapse

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_pathObject



25
26
27
28
29
# File 'lib/quandl/model/database.rb', line 25

def bulk_download_path
  path = self.class.default_path + '/data'
  path = Quandl::Util.constructed_path(path, id: database_code)
  path
end

#bulk_download_to_file(file_or_folder_path, options = {}) ⇒ Object



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
61
62
63
64
65
66
67
68
# File 'lib/quandl/model/database.rb', line 31

def bulk_download_to_file(file_or_folder_path, options = {})
  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
  path = bulk_download_path
  download_url = Quandl::Connection.request(:get, path, options) 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)
    if File.directory?(file_or_folder_path)
      file_or_folder_path = Pathname.new(file_or_folder_path.to_s).join(File.basename(uri.path))
    end
    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(options = {})
  options.assert_valid_keys(:params)

  url = bulk_download_path
  url = Quandl::ApiConfig.api_base + '/' + url
  url = Quandl::Util.constructed_path(url, id: database_code)

  params = options[:params] || {}
  params['api_key'] = Quandl::ApiConfig.api_key if Quandl::ApiConfig.api_key
  params['api_version'] = Quandl::ApiConfig.api_version if Quandl::ApiConfig.api_version

  url += '?' + params.to_query if params.any?
  url
end

#datasets(options = {}) ⇒ Object



6
7
8
# File 'lib/quandl/model/database.rb', line 6

def datasets(options = {})
  Quandl::Dataset.all({ params: { database_code: database_code, query: nil, page: 1 } }.deep_merge(options))
end