soulheart 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 701b63e892265647a1615c790e81ab3e4132bb0a
4
- data.tar.gz: baa1a417ca1bedf0efff4cd795a5f0f6679c60e8
3
+ metadata.gz: eb5870c5f1cf50c4ebac25363602c2f3cf0c4dd8
4
+ data.tar.gz: ab86a41e246856d395eacc5b896f36f2cb0ed254
5
5
  SHA512:
6
- metadata.gz: 37fdeb72ef3cb8179b4aafc1eb9860e8d96a469a7fe866b51160004fb8298da877496b22d46df5537f0b4f10dd35fb927720c1d3420b7a489179c0a3537b5c46
7
- data.tar.gz: 8580e37cbd146a1b03d15dbb4ddd913ed1e30c4d4c08c79f7fa4b7b6b46cc63013c0bd47a4902216235491fc9c3b61e5a41fd6b08cb47ee9f8dff0d5ee744a86
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
- if File.exists?(file)
100
- start_time = Time.now.to_i
101
- loader = Soulheart::Loader.new
102
-
103
- puts "Loading items in batches of #{BATCH_SIZE} ..."
104
- count = 0
105
- begin
106
- f = File.open(file)
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
- ensure
118
- f.close
134
+ else
135
+ puts "unknown File type"
119
136
  end
120
- puts "Loaded a total of #{count} items in #{Time.now.to_i - start_time} second(s)"
121
- else
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)
@@ -48,7 +48,7 @@ module Soulheart
48
48
  end
49
49
 
50
50
  def cache_id(type='all')
51
- "#{base_id}:cache:#{type}:"
51
+ "#{base_id}cache:#{type}:"
52
52
  end
53
53
  end
54
54
  end
@@ -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
 
@@ -8,7 +8,7 @@ module Soulheart
8
8
 
9
9
  def self.default_params_hash
10
10
  {
11
- 'page' => 0,
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
- page = @opts['page'].to_i
57
- per_page = @opts['per_page'].to_i
58
- ids = redis.zrevrange(cachekey, page*per_page, per_page-1) # Using 'ids', even though keys are now terms - because clarity?
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
@@ -17,7 +17,7 @@ module Soulheart
17
17
 
18
18
  get '/' do
19
19
  matches = Matcher.new(params).matches
20
- MultiJson.encode({ results: matches })
20
+ MultiJson.encode({ matches: matches })
21
21
  end
22
22
 
23
23
  get '/status' do
@@ -1,3 +1,3 @@
1
1
  module Soulheart
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/logo.png CHANGED
Binary file
@@ -15,7 +15,7 @@ describe Soulheart::Loader do
15
15
  it "doesn't overwrite the submitted params" do
16
16
  item = {
17
17
  'text' => 'Cool ',
18
- 'priority' => 50,
18
+ 'priority' => '50',
19
19
  'category' => 'Gooble',
20
20
  'data' => {
21
21
  'id' => 199
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soulheart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Herr