redis-bid 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/redis_bid.rb +66 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3dc1a94a8e12bc67bbec949edd5cd604126b3b5
4
+ data.tar.gz: a955e6cee9d98855e47c28d3eaba476920ca1fc3
5
+ SHA512:
6
+ metadata.gz: fbbcf8318bf5a7a2fab691a613767940176be613c7765dc3d01906f9b579e4f6f0094df9f91c7a45c443175f7682eec42762ba6a8dd205f4f275cb9abc193786
7
+ data.tar.gz: 43b78e951bd5da6f772e819b842fadbcf34e6d086908fc1cf330079c16f58d990c99d1259429911770842c1c285f2ccf7915677e019ef6c53215570eadabf0d2
data/lib/redis_bid.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'redis'
2
+
3
+ class RedisBid
4
+
5
+ DEFAULT_USER_ID = 0
6
+ MIN_BID_SCORE = 0
7
+
8
+ BID_ITEMS_KEY_PREFIX = "bid_items"
9
+ BID_ITEM_KEY_PREFIX = "bid"
10
+
11
+ def self.redis=(conn) @@redis = conn end
12
+ def self.redis
13
+ @@redis ||= Redis.new(:host => 'localhost', :port => 6379)
14
+ end
15
+
16
+ def self.register_bid_item(bid_id, options = {})
17
+ options = { min_score: MIN_BID_SCORE }.merge(options)
18
+ redis.sadd(BID_ITEMS_KEY_PREFIX, bid_id)
19
+ set_min_bid(bid_id, options[:min_bid_score])
20
+ end
21
+
22
+ def self.is_registered?(bid_id)
23
+ redis.sismember(BID_ITEMS_KEY_PREFIX, bid_id)
24
+ end
25
+
26
+ def self.set_min_bid(bid_id, score)
27
+ return false unless is_registered?(bid_id)
28
+ redis.hset("min_bid", bid_id, score)
29
+ true
30
+ end
31
+
32
+ def self.get_min_bid(bid_id)
33
+ redis.hget("min_bid", bid_id).to_i
34
+ end
35
+
36
+ def self.bid_items
37
+ redis.smembers(BID_ITEMS_KEY_PREFIX)
38
+ end
39
+
40
+ def self.add_user_bid(user_id, bid_id, score)
41
+ return false if score < MIN_BID_SCORE
42
+ return false unless is_registered?(bid_id)
43
+ redis.zadd("BID_ITEM_KEY_PREFIX:#{bid_id}", score, user_id)
44
+ true
45
+ end
46
+
47
+ def self.remove_user_bid(user_id, bid_id)
48
+ redis.zrem("BID_ITEM_KEY_PREFIX:#{bid_id}", user_id)
49
+ end
50
+
51
+ def self.num_user_bids(bid_id)
52
+ redis.zcard("BID_ITEM_KEY_PREFIX:#{bid_id}")
53
+ end
54
+
55
+ def self.list_user_bids(bid_id, options = {})
56
+ options = { min_score: MIN_BID_SCORE, max_score: "+inf", with_scores: false, order: "desc"}.merge(options)
57
+ return list_user_bids_in_descending_order(bid_id, options) if options[:order] == "desc"
58
+ redis.zrangebyscore("BID_ITEM_KEY_PREFIX:#{bid_id}", options[:min_score], options[:max_score], with_scores: options[:with_scores])
59
+ end
60
+
61
+ private
62
+
63
+ def self.list_user_bids_in_descending_order(bid_id, options = {})
64
+ redis.zrevrangebyscore("BID_ITEM_KEY_PREFIX:#{bid_id}", options[:max_score], options[:min_score], with_scores: options[:with_scores])
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-bid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - tkang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Bid System
14
+ email: tkang1@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/redis_bid.rb
20
+ homepage: http://rubygems.org/gems/redis-bid
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.0
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: redis-bid!
44
+ test_files: []