quandl-elasticsearch 2.1.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rubocop.yml +34 -0
  4. data/COMMANDS.md +29 -0
  5. data/Gemfile +10 -0
  6. data/Gemfile.lock +155 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +50 -0
  9. data/Rakefile +1 -0
  10. data/config/elasticsearch.yml +32 -0
  11. data/elasticsearch/elasticsearch.yml +386 -0
  12. data/elasticsearch/logging.yml +56 -0
  13. data/elasticsearch/stopwords/english.txt +38 -0
  14. data/elasticsearch/synonyms/synonyms_english.txt +318 -0
  15. data/fixtures/vcr_cassettes/search_spec_database_1.yml +38 -0
  16. data/fixtures/vcr_cassettes/search_spec_database_2.yml +38 -0
  17. data/fixtures/vcr_cassettes/search_spec_dataset_1.yml +48 -0
  18. data/fixtures/vcr_cassettes/search_spec_dataset_2.yml +41 -0
  19. data/fixtures/vcr_cassettes/setup.yml +139 -0
  20. data/lib/quandl/elasticsearch.rb +61 -0
  21. data/lib/quandl/elasticsearch/base.rb +20 -0
  22. data/lib/quandl/elasticsearch/database.rb +22 -0
  23. data/lib/quandl/elasticsearch/dataset.rb +51 -0
  24. data/lib/quandl/elasticsearch/indice.rb +96 -0
  25. data/lib/quandl/elasticsearch/query.rb +282 -0
  26. data/lib/quandl/elasticsearch/search.rb +150 -0
  27. data/lib/quandl/elasticsearch/tag.rb +21 -0
  28. data/lib/quandl/elasticsearch/template.rb +189 -0
  29. data/lib/quandl/elasticsearch/utility.rb +6 -0
  30. data/lib/quandl/elasticsearch/version.rb +6 -0
  31. data/quandl +77 -0
  32. data/quandl-elasticsearch.gemspec +34 -0
  33. data/solano.yml +20 -0
  34. data/spec/lib/quandl/elasticsearch/database_spec.rb +98 -0
  35. data/spec/lib/quandl/elasticsearch/dataset_spec.rb +124 -0
  36. data/spec/lib/quandl/elasticsearch/indice_spec.rb +10 -0
  37. data/spec/lib/quandl/elasticsearch/query_spec.rb +239 -0
  38. data/spec/lib/quandl/elasticsearch/search_spec.rb +83 -0
  39. data/spec/lib/quandl/elasticsearch/template_spec.rb +182 -0
  40. data/spec/lib/quandl/elasticsearch/utility_spec.rb +10 -0
  41. data/spec/lib/quandl/elasticsearch_spec.rb +99 -0
  42. data/spec/spec_helper.rb +27 -0
  43. data/templates/database_mapping.json +11 -0
  44. data/templates/dataset_mapping.json +9 -0
  45. data/templates/quandl_delimiter.json +0 -0
  46. data/templates/search_term_mapping.json +13 -0
  47. data/tests/Database-Ratings.csv +405 -0
  48. data/tests/Database-Tags.csv +341 -0
  49. data/tests/compare.csv +1431 -0
  50. data/tests/compare.rb +33 -0
  51. data/tests/console.rb +4 -0
  52. data/tests/generated_db_tags.csv +341 -0
  53. data/tests/search.rb +14 -0
  54. data/tests/search_db_mapping.txt +402 -0
  55. data/tests/status.rb +2 -0
  56. data/tests/test_search.csv +87 -0
  57. data/tests/test_search.rb +113 -0
  58. data/tests/testing-list.txt +183 -0
  59. data/tests/top500searches.csv +477 -0
  60. metadata +300 -0
@@ -0,0 +1,189 @@
1
+ require_relative 'version'
2
+
3
+ module Quandl
4
+ module Elasticsearch
5
+ class Template
6
+ class << self
7
+ def gem_path
8
+ Pathname.new(Gem::Specification.find_by_name('quandl-elasticsearch', Quandl::Elasticsearch::VERSION).gem_dir)
9
+ end
10
+
11
+ def main_index
12
+ { settings: { index: index_settings,
13
+ similarity: { quandl_bm25: quandl_bm25 },
14
+ analysis: { analyzer: analyzer, filter: filter, char_filter: char_filter } },
15
+ mappings: { database: { properties: database_mapping },
16
+ dataset: { _parent: { type: 'database' }, properties: dataset_mapping } } }
17
+ end
18
+
19
+ private
20
+
21
+ def analyzer
22
+ {
23
+ keyword_lowercase: keyword_lowercase_analyzer,
24
+ quandl_index: quandl_text_analyzer,
25
+ quandl_search: quandl_search_analyzer
26
+ }
27
+ end
28
+
29
+ def filter
30
+ {
31
+ quandl_synonym_filter: quandl_synonym_filter,
32
+ quandl_stemmer: quandl_stemmer,
33
+ quandl_stop_words: quandl_stop_words,
34
+ quandl_delimiter: quandl_delimiter
35
+ }
36
+ end
37
+
38
+ def char_filter
39
+ {
40
+ quandl_char_filter: quandl_char_filter
41
+ }
42
+ end
43
+
44
+ def quandl_text_analyzer
45
+ {
46
+ type: 'custom',
47
+ tokenizer: 'whitespace',
48
+ filter: %w(quandl_delimiter lowercase quandl_synonym_filter quandl_stop_words quandl_stemmer trim),
49
+ char_filter: %w(quandl_char_filter)
50
+ }
51
+ end
52
+
53
+ def quandl_search_analyzer
54
+ ## without quandl_synonym_filte
55
+ {
56
+ type: 'custom',
57
+ tokenizer: 'whitespace',
58
+ filter: %w(quandl_delimiter lowercase quandl_stop_words quandl_stemmer trim),
59
+ char_filter: %w(quandl_char_filter)
60
+ }
61
+ end
62
+
63
+ def keyword_lowercase_analyzer
64
+ {
65
+ type: 'english',
66
+ tokenizer: 'keyword',
67
+ filter: 'lowercase',
68
+ stopwords: '_none_'
69
+ }
70
+ end
71
+
72
+ def quandl_synonym_filter
73
+ synonyms = []
74
+ File.read(gem_path.join('elasticsearch/synonyms/synonyms_english.txt')).each_line do |line|
75
+ clean_line = line.strip
76
+ next if clean_line[0] == '#' || clean_line =~ /^\s+$/
77
+ synonyms << clean_line
78
+ end
79
+
80
+ {
81
+ type: 'synonym',
82
+ synonyms: synonyms
83
+ }
84
+ end
85
+
86
+ def quandl_char_filter
87
+ # unicode table
88
+ {
89
+ type: 'mapping',
90
+ mappings: [
91
+ '\\u0060=>\\u0020', # `
92
+ '\\u002F=>\\u0020', # /
93
+ '\\u003A=>\\u0020', # :
94
+ '\\u002D=>\\u0020', # -
95
+ '\\u0028=>\\u0020', # (
96
+ '\\u0029=>\\u0020', # )
97
+ '\\u002C=>\\u0020', # ,
98
+ '\\u003B=>\\u0020' # ;
99
+ ]
100
+ }
101
+ end
102
+
103
+ def quandl_stemmer
104
+ {
105
+ type: 'stemmer',
106
+ name: 'english'
107
+ }
108
+ end
109
+
110
+ def quandl_stop_words
111
+ stop_words = []
112
+ File.read(gem_path.join('elasticsearch/stopwords/english.txt')).each_line do |line|
113
+ clean_line = line.strip
114
+ next if clean_line[0] == '#' || clean_line =~ /^\s+$/
115
+ stop_words << clean_line
116
+ end
117
+
118
+ {
119
+ type: 'stop',
120
+ ignore_case: true,
121
+ stopwords: stop_words
122
+ }
123
+ end
124
+
125
+ def quandl_delimiter
126
+ {
127
+ type: 'word_delimiter',
128
+ split_on_case_change: true,
129
+ catenate_numbers: true,
130
+ preserve_original: true,
131
+ generate_word_parts: true,
132
+ split_on_numbers: true,
133
+ stem_english_prossessive: true,
134
+ type_table: ['& => ALPHA',
135
+ '_ => ALPHA',
136
+ '$ => DIGIT',
137
+ '% => DIGIT'],
138
+ protected_word: ['u.s.', 'u.s.a.']
139
+ }
140
+ end
141
+
142
+ def database_mapping
143
+ {
144
+ code: { type: 'string', store: true, norms: { enabled: false }, analyzer: 'keyword_lowercase' },
145
+ name: { type: 'string', store: true, norms: { enabled: true }, analyzer: 'quandl_index', search_analyzer: 'quandl_search' },
146
+ updated_at: { type: 'date' },
147
+ description: { type: 'string', store: true, norms: { enabled: true }, analyzer: 'quandl_index' },
148
+ documentation: { type: 'string', store: true, norms: { enabled: true }, analyzer: 'quandl_index' },
149
+ premium: { type: 'boolean', store: true, norms: { enabled: false } },
150
+ hidden: { type: 'boolean', store: true, norms: { enabled: false } },
151
+ exclusive: { type: 'boolean', store: true, norms: { enabled: false } },
152
+ type: { type: 'string', store: true, norms: { enabled: false }, index: 'not_analyzed' },
153
+ rating: { type: 'integer', store: true, norms: { enabled: false }, index: 'not_analyzed' },
154
+ tags: { type: 'string', store: true, norms: { enabled: false }, index: 'not_analyzed' }
155
+ }
156
+ end
157
+
158
+ def dataset_mapping
159
+ {
160
+ code: { type: 'string', store: true, norms: { enabled: false }, analyzer: 'keyword_lowercase' },
161
+ name: { type: 'string', store: true, norms: { enabled: true }, analyzer: 'quandl_index', search_analyzer: 'quandl_search' },
162
+ is_private: { type: 'boolean', norms: { enabled: false }, store: false },
163
+ type: { type: 'string', store: true, norms: { enabled: false }, index: 'not_analyzed' },
164
+ frequency: { type: 'string', store: false, norms: { enabled: false }, index: 'not_analyzed' },
165
+ to_date: { type: 'date', format: 'date', store: false, norms: { enabled: false } },
166
+ db_hidden: { type: 'boolean', norms: { enabled: false }, store: false },
167
+ db_exclusive: { type: 'boolean', norms: { enabled: false }, store: false },
168
+ sample: { type: 'boolean', norms: { enabled: false }, store: false }
169
+ }
170
+ end
171
+
172
+ def index_settings
173
+ {
174
+ number_of_shards: 1,
175
+ number_of_replicas: 0
176
+ }
177
+ end
178
+
179
+ def quandl_bm25
180
+ {
181
+ type: 'BM25',
182
+ k1: 0,
183
+ b: 0.75
184
+ }
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,6 @@
1
+ module Quandl
2
+ module Elasticsearch
3
+ class Utility
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Quandl
2
+ ## Elasticsearch
3
+ module Elasticsearch
4
+ VERSION = '2.1.0.rc5'.freeze
5
+ end
6
+ end
data/quandl ADDED
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'commander/import'
5
+ require './tests/search.rb'
6
+
7
+ program :version, '0.0.1'
8
+ program :description, 'quandl-elasticsearch command line tools'
9
+ default_command :search
10
+
11
+ command :search do |c|
12
+ c.syntax = 'quandl_es search [options]'
13
+ c.summary = 'search v3 on staging'
14
+ c.description = c.summary
15
+ c.example 'search on query strings', './quand_es search -q "bank"'
16
+ c.example 'search based on query strings', './quandl_es search --query "natural gas future"'
17
+ c.example 'show search results for page 3.', './quandl_es search --q "bank" -p 3'
18
+ c.example 'show search results for page 2 and each page shows 25 results.', './quandl_es search --q "oil" -p 2 -s 25'
19
+ c.example 'serch for databases with term aaple and with tags USA and STOCKS', './quandl_es search --q "aapl" -g USA,STOCKS'
20
+
21
+ c.option '-q', '--query "query strings"', String, 'query strings'
22
+ c.option '-g', '--tags "tags list"', String, 'tag list'
23
+ c.option '-p', '--page PAGE_NUMBER', 'search results page number', Integer, 'page number'
24
+ c.option '-s', '--size PER_PAGE', 'search results per page', Integer, 'per page result'
25
+ c.option '-m', '--premium', String, 'only premium databases'
26
+ c.option '-f', '--free', String, 'only free databases'
27
+ c.option '-w', '--with-dataset', 'show dadatasets', String, 'show dataset'
28
+ c.action do |_args, options|
29
+ page = options.page ? options.page.to_i : 0
30
+ tags = options.tags ? options.tags.to_s : ''
31
+
32
+ f = options.free ? true : false
33
+ p = options.premium ? true : false
34
+
35
+ premium = nil
36
+ premium = false if f
37
+ premium = true if p
38
+
39
+ unless tags.empty?
40
+ tags.split(',').each do |t|
41
+ fail 'tags must be one of tag list' unless tag_list.include?(t)
42
+ end
43
+ end
44
+ if options.query
45
+ result = search(options.query, tags, options.with_dataset, premium, page)
46
+ db_codes = result.map { |s| s['code'] }.reject(&:nil?)
47
+ r = { 'databases' => result }
48
+ puts JSON.pretty_generate(r)
49
+ puts "#{db_codes.count} hit databases code: #{db_codes.join(',')}"
50
+ else
51
+ system('./quandl_es search --help')
52
+ end
53
+ end
54
+ end
55
+
56
+ command :dataset do |c|
57
+ c.option '-i', '--id DATABASE_id', String, 'database id'
58
+ c.option '-q', '--query SEARCH_QUERY', String, 'search query'
59
+ c.option '-p', '--page PAGE_NUMBER', 'search results page number', Integer, 'page number'
60
+ c.option '-s', '--size PER_PAGE', 'search results per page', Integer, 'per page result'
61
+ c.option '-f', '--frequency FREQUENCY_LIST', String, 'frequency list'
62
+ frequency_list = %w(daily weekly monthly "quarterly annual)
63
+
64
+ c.action do |_args, options|
65
+ frequency = options.frequency ? options.frequency : nil
66
+ page = options.page ? options.page.to_i : 0
67
+ # size = options.size ? options.size.to_i : 10
68
+ unless frequency.nil?
69
+ frequency.split(',').each do |d|
70
+ fail "frequency should be in #{frequency_list}" unless frequency_list.include?(d)
71
+ end
72
+ end
73
+ query = options.query && options.query.strip != '' ? options.query : nil
74
+ r = dataset_search(options.id, query, frequency, page)
75
+ puts JSON.pretty_generate(r)
76
+ end
77
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quandl/elasticsearch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'quandl-elasticsearch'
8
+ spec.version = Quandl::Elasticsearch::VERSION
9
+ spec.authors = ['Jun Li']
10
+ spec.email = ['jun@quandl.com']
11
+ spec.description = 'Elasticsearch interface for Quandl'
12
+ spec.summary = 'Elasticsearch interface for Quandl'
13
+ spec.homepage = 'https://github.com/quandl/quandl-elasticsearch'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'elasticsearch', '~> 1.0.17'
22
+ spec.add_runtime_dependency 'commander', '~> 4.3'
23
+ spec.add_runtime_dependency 'json', '~> 1.8.2'
24
+ spec.add_runtime_dependency 'quandl-config', '> 0.0.3', '< 2.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'rubocopter'
28
+ spec.add_development_dependency 'activesupport'
29
+ spec.add_development_dependency 'vcr'
30
+ spec.add_development_dependency 'simplecov'
31
+ spec.add_development_dependency 'webmock'
32
+ spec.add_development_dependency 'bundler', '~> 1.3'
33
+ spec.add_development_dependency 'rake'
34
+ end
data/solano.yml ADDED
@@ -0,0 +1,20 @@
1
+ environment:
2
+ 'QUANDL_PROJECT': 'Elasticsearch Gem'
3
+ 'SLACK_WEBHOOK': 'https://hooks.slack.com/services/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'
4
+ 'TZ': 'UTC'
5
+ tool_config:
6
+ gc: '4.6'
7
+ ruby_version: 2.2.2
8
+ bundler_version: 1.10.5
9
+ rake:
10
+ rails_env: 'test'
11
+ solr: false
12
+ rabbitmq: false
13
+ redis: false
14
+ tests:
15
+ - bundle exec rubocopter --commit origin/master
16
+ test_pattern:
17
+ - spec/**_spec.rb
18
+ coverage: true
19
+ hooks:
20
+ post_build: 'gem install quandl_utility && qutil ci notify_slack'
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'quandl/elasticsearch/indice'
3
+
4
+ describe Quandl::Elasticsearch::Database do
5
+ describe '.initialize' do
6
+ it 'can return the correct object' do
7
+ record = { 'id' => 4 }
8
+ database = Quandl::Elasticsearch::Database.new(record)
9
+ expect(database).to be_a_kind_of(Quandl::Elasticsearch::Database)
10
+ end
11
+ end
12
+
13
+ describe '.id' do
14
+ it 'can return the correct id' do
15
+ record = { 'id' => 4 }
16
+ database = Quandl::Elasticsearch::Database.new(record)
17
+ expect(database.id).to eq(4)
18
+ end
19
+ end
20
+
21
+ describe '.data' do
22
+ it 'can return the correct data and does not flag as remove' do
23
+ record = { 'id' => 4, 'name' => 'Name', 'description' => 'Description',
24
+ 'documentation' => 'Documentation', 'premium' => true, 'rating' => 5,
25
+ 'deleted_at' => nil, 'type' => nil, 'hidden' => 'f',
26
+ 'elasticsearch_flags' => 'USA,STOCKS', 'exclusive' => true }
27
+ database = Quandl::Elasticsearch::Database.new(record)
28
+ expect(database.remove?).to eq(false)
29
+ expect(database.data).to eq(name: 'Name', code: nil, description: 'Description', documentation: 'Documentation',
30
+ premium: true, hidden: false, tags: %w(USA STOCKS), exclusive: true)
31
+ end
32
+ end
33
+
34
+ describe '.remove' do
35
+ it 'can not remove correctly if it is hidden' do
36
+ record = { 'hidden' => 't', 'deleted_at' => nil, 'rating' => 1, 'type' => nil }
37
+ database1 = Quandl::Elasticsearch::Database.new(record)
38
+ expect(database1.remove?).to eq(false)
39
+ record = { 'hidden' => 'f', 'deleted_at' => nil, 'rating' => 1, 'type' => nil }
40
+ database2 = Quandl::Elasticsearch::Database.new(record)
41
+ expect(database2.remove?).to eq(false)
42
+ end
43
+
44
+ it 'can remove correctly if it is deleted_at' do
45
+ record = { 'deleted_at' => nil, 'rating' => 5, 'type' => nil }
46
+ database1 = Quandl::Elasticsearch::Database.new(record)
47
+ expect(database1.remove?).to eq(false)
48
+ record = { 'deleted_at' => '2015-01-01', 'rating' => 5, 'type' => nil }
49
+ database2 = Quandl::Elasticsearch::Database.new(record)
50
+ expect(database2.remove?).to eq(true)
51
+ end
52
+
53
+ it 'can not remove correctly if it is rated zero or lower' do
54
+ record = { 'deleted_at' => nil, 'hidden' => 'f', 'rating' => 1, 'type' => nil }
55
+ database1 = Quandl::Elasticsearch::Database.new(record)
56
+ expect(database1.remove?).to eq(false)
57
+ record = { 'deleted_at' => nil, 'hidden' => 'f', 'rating' => 0, 'type' => nil }
58
+ database2 = Quandl::Elasticsearch::Database.new(record)
59
+ expect(database2.remove?).to eq(false)
60
+ record = { 'rating' => -10, 'deleted_at' => nil, 'hidden' => 'f', 'type' => nil }
61
+ database3 = Quandl::Elasticsearch::Database.new(record)
62
+ expect(database3.remove?).to eq(false)
63
+ record = { 'rating' => 4, 'deleted_at' => nil, 'hidden' => 'f', 'type' => nil }
64
+ database4 = Quandl::Elasticsearch::Database.new(record)
65
+ expect(database4.remove?).to eq(false)
66
+ end
67
+
68
+ it 'can remove correctly if it is not a Source' do
69
+ record = { 'type' => 'UserSource', 'rating' => 1 }
70
+ database1 = Quandl::Elasticsearch::Database.new(record)
71
+ expect(database1.remove?).to eq(true)
72
+ record = { 'type' => nil, 'rating' => 1 }
73
+ database2 = Quandl::Elasticsearch::Database.new(record)
74
+ expect(database2.remove?).to eq(false)
75
+ end
76
+
77
+ it 'can remove correctly if rating <= 0' do
78
+ record = { 'rating' => 1 }
79
+ database1 = Quandl::Elasticsearch::Database.new(record)
80
+ expect(database1.remove?).to eq(false)
81
+ record = { 'rating' => nil }
82
+ database2 = Quandl::Elasticsearch::Database.new(record)
83
+ expect(database2.remove?).to eq(false)
84
+ record = { 'rating' => 0 }
85
+ database3 = Quandl::Elasticsearch::Database.new(record)
86
+ expect(database3.remove?).to eq(false)
87
+ end
88
+
89
+ it 'can not remove correctly if it is hidden' do
90
+ record = { 'hidden' => 't' }
91
+ database1 = Quandl::Elasticsearch::Database.new(record)
92
+ expect(database1.remove?).to eq(false)
93
+ record = { 'hidden' => 'f', 'deleted_at' => nil, 'rating' => 1, 'type' => nil }
94
+ database2 = Quandl::Elasticsearch::Database.new(record)
95
+ expect(database2.remove?).to eq(false)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+ require 'quandl/elasticsearch/indice'
3
+
4
+ describe Quandl::Elasticsearch::Dataset do
5
+ describe '.initialize' do
6
+ it 'can return the correct object' do
7
+ record = { 'id' => 9, 'hidden' => 'f', 'deleted_at' => nil, 'rating' => 1, 'type' => nil }
8
+ dataset = Quandl::Elasticsearch::Dataset.new(record)
9
+ expect(dataset).to be_a_kind_of(Quandl::Elasticsearch::Dataset)
10
+ end
11
+ end
12
+
13
+ describe '.id' do
14
+ it 'can return the correct value' do
15
+ record = { 'id' => 9, 'type' => nil, 'deleted_at' => nil, 'state' => 'published' }
16
+ dataset = Quandl::Elasticsearch::Dataset.new(record)
17
+ expect(dataset.id).to eq(9)
18
+ expect(dataset.remove?).to eq(false)
19
+ end
20
+ end
21
+
22
+ describe '.database_id' do
23
+ it 'can return the correct value' do
24
+ record = { 'id' => 9, 'database_id' => 598_393 }
25
+ dataset = Quandl::Elasticsearch::Dataset.new(record)
26
+ expect(dataset.database_id).to eq(598_393)
27
+ end
28
+ end
29
+
30
+ describe '.data' do
31
+ it 'can return the correct value and does not flag as remove' do
32
+ # {:code=>nil, :name=>nil, :to_date=>nil, :frequency=>nil}
33
+ record = { 'id' => 9, 'deleted_at' => nil, 'type' => nil, 'state' => 'published',
34
+ 'code' => 'Code', 'name' => 'Name', 'to_date' => '12122015',
35
+ 'is_private' => false, 'frequency' => 'daily', 'db_hidden' => true,
36
+ 'db_exclusive' => true, 'db_premium' => true, 'db_preview_enabled' => true,
37
+ 'db_preview_datasets' => '' }
38
+ dataset = Quandl::Elasticsearch::Dataset.new(record)
39
+ expect(dataset.data).to eq(code: 'Code', name: 'Name', to_date: '12122015',
40
+ is_private: false, frequency: 'daily',
41
+ db_hidden: true, db_exclusive: true, sample: true)
42
+ expect(dataset.remove?).to eq(false)
43
+ end
44
+ end
45
+
46
+ describe '.remove' do
47
+ it 'can remove correctly if it is deleted_at' do
48
+ record = { 'type' => nil, 'deleted_at' => nil, 'is_private' => 'f' }
49
+ dataset1 = Quandl::Elasticsearch::Dataset.new(record)
50
+ expect(dataset1.remove?).to eq(false)
51
+ record = { 'deleted_at' => '2015-01-01' }
52
+ dataset2 = Quandl::Elasticsearch::Dataset.new(record)
53
+ expect(dataset2.remove?).to eq(true)
54
+ end
55
+
56
+ it 'can remove correctly if it is not a dataset' do
57
+ record = { 'type' => 'Superset' }
58
+ dataset1 = Quandl::Elasticsearch::Dataset.new(record)
59
+ expect(dataset1.remove?).to eq(true)
60
+ record = { 'type' => nil, 'deleted_at' => nil, 'is_private' => 'f' }
61
+ dataset2 = Quandl::Elasticsearch::Dataset.new(record)
62
+ expect(dataset2.remove?).to eq(false)
63
+ end
64
+ end
65
+
66
+ describe 'sample' do
67
+ subject { Quandl::Elasticsearch::Dataset.new(record).data }
68
+
69
+ let(:db_preview_datasets) { 'BBB,AAA,CCC' }
70
+ let(:db_preview_enabled) { 't' }
71
+ let(:db_premium) { 't' }
72
+ let(:code) { 'AAA' }
73
+
74
+ let(:record) do
75
+ {
76
+ 'db_preview_datasets' => db_preview_datasets,
77
+ 'db_preview_enabled' => db_preview_enabled,
78
+ 'db_premium' => db_premium,
79
+ 'code' => code
80
+ }
81
+ end
82
+
83
+ context 'premium' do
84
+ context 'code is in preview datasets list' do
85
+ context 'previews not enabled' do
86
+ let(:db_preview_enabled) { 'f' }
87
+
88
+ it 'is not previewable' do
89
+ expect(subject[:sample]).to eq(false)
90
+ end
91
+ end
92
+
93
+ context 'previews enabled' do
94
+ it 'is previewable' do
95
+ expect(subject[:sample]).to eq(true)
96
+ end
97
+ end
98
+ end
99
+
100
+ context 'code is not in preview datasets list' do
101
+ let(:db_preview_datasets) { 'BBB,AA,CCC' }
102
+
103
+ it 'is not previewable' do
104
+ expect(subject[:sample]).to eq(false)
105
+ end
106
+ end
107
+
108
+ context 'preview datasets list is empty' do
109
+ let(:db_preview_datasets) { '' }
110
+
111
+ it 'is previewable since empty means all datasets are whitelisted' do
112
+ expect(subject[:sample]).to eq(true)
113
+ end
114
+ end
115
+ end
116
+
117
+ context 'free' do
118
+ let(:db_premium) { 'f' }
119
+ it 'is not previewable' do
120
+ expect(subject[:sample]).to eq(false)
121
+ end
122
+ end
123
+ end
124
+ end