soulheart 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/soulheart +33 -15
- data/lib/soulheart/base.rb +1 -1
- data/lib/soulheart/loader.rb +1 -0
- data/lib/soulheart/matcher.rb +7 -5
- data/lib/soulheart/server.rb +1 -1
- data/lib/soulheart/version.rb +1 -1
- data/logo.png +0 -0
- data/spec/soulheart/loader_spec.rb +1 -1
- data/spec/soulheart/matcher_spec.rb +27 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb5870c5f1cf50c4ebac25363602c2f3cf0c4dd8
|
4
|
+
data.tar.gz: ab86a41e246856d395eacc5b896f36f2cb0ed254
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83fff39abcf059750d36f89a5aae935e2a8cc579cbfa248b0ede8abe29b2d79316ec25f486415132c8570e4a6a05f8a9300d5a8f41eea1517aeaeaede5830d31
|
7
|
+
data.tar.gz: 5f1ec6654a3b373ea5a4ab686005b85484e4a11d2ffd7b7e00d39932121736475d5d8176801f8f3823500780c14d4283f6c749ab663caed75ebefe0337f59ef1
|
data/bin/soulheart
CHANGED
@@ -96,14 +96,33 @@ def generate(type, file)
|
|
96
96
|
end
|
97
97
|
|
98
98
|
def load(file)
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
99
|
+
require 'uri'
|
100
|
+
if file =~ URI::regexp
|
101
|
+
require 'open-uri'
|
102
|
+
f = open(file)
|
103
|
+
elsif File.exists?(file)
|
104
|
+
f = File.open(file)
|
105
|
+
else
|
106
|
+
puts "Couldn't open file: #{file}"
|
107
|
+
return true
|
108
|
+
end
|
109
|
+
|
110
|
+
start_time = Time.now.to_i
|
111
|
+
count = 0
|
112
|
+
loader = Soulheart::Loader.new
|
113
|
+
lines = []
|
114
|
+
begin
|
115
|
+
if file.match(/(c|t)sv\z/i)
|
116
|
+
puts "Reading a CSV"
|
117
|
+
require 'csv'
|
118
|
+
sep = file.match(/tsv\z/i) ? "\t" : ","
|
119
|
+
CSV.foreach(f, headers: true, col_sep: sep) do |row|
|
120
|
+
lines << row.to_hash
|
121
|
+
count += 1
|
122
|
+
end
|
123
|
+
elsif file.match(/json\z/i)
|
124
|
+
puts "Reading JSON"
|
125
|
+
puts "Loading items in batches of #{BATCH_SIZE} ..."
|
107
126
|
while !f.eof?
|
108
127
|
lines = []
|
109
128
|
BATCH_SIZE.times do
|
@@ -111,16 +130,15 @@ def load(file)
|
|
111
130
|
lines << MultiJson.decode(f.gets)
|
112
131
|
count += 1
|
113
132
|
end
|
114
|
-
loader.load(lines)
|
115
|
-
puts "Loaded #{count} items ..." unless f.eof?
|
116
133
|
end
|
117
|
-
|
118
|
-
|
134
|
+
else
|
135
|
+
puts "unknown File type"
|
119
136
|
end
|
120
|
-
|
121
|
-
|
122
|
-
puts "Couldn't open file: #{file}"
|
137
|
+
ensure
|
138
|
+
f.close
|
123
139
|
end
|
140
|
+
loader.load(lines)
|
141
|
+
puts "Loaded a total of #{count} items in #{Time.now.to_i - start_time} second(s)"
|
124
142
|
end
|
125
143
|
|
126
144
|
def add(type)
|
data/lib/soulheart/base.rb
CHANGED
data/lib/soulheart/loader.rb
CHANGED
@@ -60,6 +60,7 @@ module Soulheart
|
|
60
60
|
raise ArgumentError, "Items must have text" unless item["text"]
|
61
61
|
default_items_hash(item.delete('text'), item.delete('category')).
|
62
62
|
tap{ |i| i['data'].merge!(item.delete('data')) if item['data'] }.
|
63
|
+
tap{ |i| i['priority'] = item.delete('priority').to_f if item['priority'] }.
|
63
64
|
merge item
|
64
65
|
end
|
65
66
|
|
data/lib/soulheart/matcher.rb
CHANGED
@@ -8,7 +8,7 @@ module Soulheart
|
|
8
8
|
|
9
9
|
def self.default_params_hash
|
10
10
|
{
|
11
|
-
'page' =>
|
11
|
+
'page' => 1,
|
12
12
|
'per_page' => 5,
|
13
13
|
'categories' => [],
|
14
14
|
'query' => '',
|
@@ -36,7 +36,7 @@ module Soulheart
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def cache_id_from_opts
|
39
|
-
"#{cache_id(categories_string)}#{@opts['query'].join(':')}
|
39
|
+
"#{cache_id(categories_string)}#{@opts['query'].join(':')}"
|
40
40
|
end
|
41
41
|
|
42
42
|
def interkeys_from_opts(cid)
|
@@ -53,9 +53,11 @@ module Soulheart
|
|
53
53
|
redis.zinterstore(cachekey, interkeys)
|
54
54
|
redis.expire(cachekey, cache_length) # cache_length is set in base.rb
|
55
55
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
offset = (@opts['page'].to_i - 1) * @opts['per_page'].to_i
|
57
|
+
limit = @opts['per_page'].to_i + offset - 1
|
58
|
+
|
59
|
+
limit = 0 if limit < 0
|
60
|
+
ids = redis.zrevrange(cachekey, offset, limit) # Using 'ids', even though keys are now terms
|
59
61
|
if ids.size > 0
|
60
62
|
results = redis.hmget(results_hashes_id, *ids)
|
61
63
|
results = results.reject{ |r| r.nil? } # handle cached results for ids which have since been deleted
|
data/lib/soulheart/server.rb
CHANGED
data/lib/soulheart/version.rb
CHANGED
data/logo.png
CHANGED
Binary file
|
@@ -72,6 +72,33 @@ describe Soulheart::Matcher do
|
|
72
72
|
expect(matches.count).to eq(2)
|
73
73
|
expect(matches[0]['text']).to eq('Jannd')
|
74
74
|
end
|
75
|
+
|
76
|
+
it "Gets pages and uses them" do
|
77
|
+
# Pagination wrecked my mind, hence the multitude of tests
|
78
|
+
items = [
|
79
|
+
{"text" => 'First item', 'priority' => '11000' },
|
80
|
+
{"text" => 'Second item', 'priority' => '1999' },
|
81
|
+
{"text" => 'Third item', 'priority' => 1900 },
|
82
|
+
{"text" => 'Fourth item', 'priority' => 1800 },
|
83
|
+
{"text" => 'Fifth item', 'priority' => 1750 },
|
84
|
+
{"text" => 'Sixth item', 'priority' => 1700 },
|
85
|
+
{"text" => 'Seventh item', 'priority' => 1699 }
|
86
|
+
]
|
87
|
+
loader = Soulheart::Loader.new
|
88
|
+
loader.delete_categories
|
89
|
+
loader.load(items)
|
90
|
+
|
91
|
+
page1 = Soulheart::Matcher.new({'per_page' => 1, 'cache' => false}).matches
|
92
|
+
expect(page1[0]['text']).to eq('First item')
|
93
|
+
|
94
|
+
page2 = Soulheart::Matcher.new({'per_page' => 1, 'page' => 2, 'cache' => false}).matches
|
95
|
+
expect(page2.count).to eq(1)
|
96
|
+
expect(page2[0]['text']).to eq('Second item')
|
97
|
+
|
98
|
+
page3 = Soulheart::Matcher.new({'per_page' => 2, 'page' => 3, 'cache' => false}).matches
|
99
|
+
expect(page3[0]['text']).to eq('Fifth item')
|
100
|
+
expect(page3[1]['text']).to eq('Sixth item')
|
101
|
+
end
|
75
102
|
end
|
76
103
|
|
77
104
|
end
|