trifle 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -9,11 +9,14 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- trifle (0.0.1)
12
+ trifle (0.0.2)
13
13
 
14
14
  GEM
15
15
  remote: http://rubygems.org/
16
16
  specs:
17
+ active_support (3.0.0)
18
+ activesupport (= 3.0.0)
19
+ activesupport (3.0.0)
17
20
  diff-lcs (1.1.3)
18
21
  redis (3.0.1)
19
22
  rspec (2.10.0)
@@ -30,6 +33,7 @@ PLATFORMS
30
33
  ruby
31
34
 
32
35
  DEPENDENCIES
36
+ active_support
33
37
  fakeredis!
34
38
  rspec
35
39
  rujitsu
data/README.md CHANGED
@@ -51,4 +51,10 @@ trifle.find "192.168.1.1"
51
51
  ```
52
52
 
53
53
 
54
+ ## Release notes
55
+
56
+ * **0.0.2** Added support for custom Redis key
57
+ * **0.0.1** First draft
58
+
59
+
54
60
 
data/lib/trifle/finder.rb CHANGED
@@ -29,18 +29,18 @@ class Trifle
29
29
  end
30
30
 
31
31
  def max
32
- redis.llen Trifle::KEY
32
+ redis.llen(key)
33
33
  end
34
34
 
35
35
  def entry_for index
36
- entry = redis.lindex(Trifle::KEY, index).split(":")
36
+ entry = redis.lindex(key, index).split(":")
37
37
  entry[0] = entry[0].to_i
38
38
  entry[1] = entry[1].to_i
39
39
  entry
40
40
  end
41
41
 
42
42
  def in_range range, ip_i
43
- ip_i >= range[0] && ip_i <= range[1]
43
+ range[0] <= ip_i && ip_i <= range[1]
44
44
  end
45
45
  end
46
46
  end
@@ -1,10 +1,12 @@
1
1
  class Trifle
2
2
  module InitializeWithRedis
3
3
  attr_accessor :redis
4
+ attr_accessor :key
4
5
 
5
- def initialize redis
6
- raise ArgumentError.new("Redis object expected") unless redis.is_a?(Redis)
6
+ def initialize redis, options = {}
7
+ raise ArgumentError.new("Redis-like object expected") unless redis
7
8
  self.redis = redis
9
+ self.key = options[:key] || "trifle"
8
10
  end
9
11
  end
10
12
  end
data/lib/trifle/loader.rb CHANGED
@@ -37,7 +37,7 @@ class Trifle
37
37
 
38
38
  def append row
39
39
  entry = row.values_at(2,3,4,5).join(":")
40
- redis.rpush Trifle::KEY, entry
40
+ redis.rpush key, entry
41
41
  end
42
42
 
43
43
  def sort data
@@ -51,9 +51,7 @@ class Trifle
51
51
 
52
52
  def valid? data
53
53
  if data.is_a?(Array) && data.count > 0
54
- return data.map do |row|
55
- is_number(row[2]) && is_number(row[3])
56
- end.select{|valid| !valid}.count == 0
54
+ return data.detect {|row| !is_number(row[2]) || !is_number(row[3])}.nil?
57
55
  end
58
56
  false
59
57
  end
@@ -63,7 +61,7 @@ class Trifle
63
61
  end
64
62
 
65
63
  def clear
66
- redis.del(Trifle::KEY)
64
+ redis.del key
67
65
  end
68
66
  end
69
67
  end
@@ -1,3 +1,3 @@
1
1
  class Trifle
2
- VERSION = "0.0.1" unless defined? Trifle::VERSION
2
+ VERSION = "0.0.2" unless defined? Trifle::VERSION
3
3
  end
data/lib/trifle.rb CHANGED
@@ -7,8 +7,6 @@ require "trifle/initialize_with_redis"
7
7
  class Trifle
8
8
  include InitializeWithRedis
9
9
 
10
- KEY = "trifle".freeze
11
-
12
10
  def load options = {}
13
11
  loader.handle options
14
12
  end
@@ -20,10 +18,10 @@ class Trifle
20
18
  protected
21
19
 
22
20
  def loader
23
- @loader = Loader.new(redis)
21
+ @loader = Loader.new(redis, key: key)
24
22
  end
25
23
 
26
24
  def finder
27
- @finder = Finder.new(redis)
25
+ @finder = Finder.new(redis, key: key)
28
26
  end
29
27
  end
@@ -15,7 +15,11 @@ describe Trifle::InitializeWithRedis do
15
15
 
16
16
  it "should fail without a redis instance" do
17
17
  -> { @klass.new(nil) }.should raise_error(ArgumentError)
18
- -> { @klass.new("http://127.0.0.1:3000/") }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "should accept an optional redis key" do
21
+ instance = @klass.new(Redis.new, key: "foo")
22
+ instance.key.should be == "foo"
19
23
  end
20
24
  end
21
25
 
@@ -1,4 +1,5 @@
1
1
  require "spec_helper"
2
+ require 'active_support/core_ext/string/strip'
2
3
 
3
4
  describe Trifle::Loader do
4
5
 
@@ -8,10 +9,10 @@ describe Trifle::Loader do
8
9
  ["223.255.254.0","223.255.254.255","3758095872","3758096127","SG","Singapore"],
9
10
  ["223.255.255.0","223.255.255.255","3758096128","3758096383","AU","Australia"]
10
11
  ]
11
- @csv = <<-EOD
12
- "223.255.254.0","223.255.254.255","3758095872","3758096127","SG","Singapore"
13
- "223.255.255.0","223.255.255.255","3758096128","3758096383","AU","Australia"
14
- EOD
12
+ @csv = <<-CSV.strip_heredoc
13
+ "223.255.254.0","223.255.254.255","3758095872","3758096127","SG","Singapore"
14
+ "223.255.255.0","223.255.255.255","3758096128","3758096383","AU","Australia"
15
+ CSV
15
16
  end
16
17
 
17
18
  it "should extend InitializeWithRedis" do
@@ -63,7 +64,7 @@ describe Trifle::Loader do
63
64
 
64
65
  it "should read the files" do
65
66
  @loader.load_files @filenames
66
- @loader.redis.llen(Trifle::KEY).should be == 4
67
+ @loader.redis.llen(@loader.key).should be == 4
67
68
  end
68
69
  end
69
70
 
@@ -96,7 +97,7 @@ describe Trifle::Loader do
96
97
 
97
98
  it "should load it in redis" do
98
99
  @loader.load_data @valid_data
99
- @loader.redis.llen(Trifle::KEY).should be == 2
100
+ @loader.redis.llen(@loader.key).should be == 2
100
101
  end
101
102
  end
102
103
 
@@ -124,9 +125,9 @@ describe Trifle::Loader do
124
125
  end
125
126
 
126
127
  it "should handle ipv6 data" do
127
- @ipv6csv = <<-CSV
128
- "2c0f:ffe8::", "2c0f:ffe8:ffff:ffff:ffff:ffff:ffff:ffff", "58569106662796955307479896348547874816", "58569106742025117821744233942091825151", "NG", "Nigeria"
129
- "2c0f:fff0::", "2c0f:fff0:ffff:ffff:ffff:ffff:ffff:ffff", "58569107296622255421594597096899477504", "58569107375850417935858934690443427839", "NG", "Nigeria"
128
+ @ipv6csv = <<-CSV.strip_heredoc
129
+ "2c0f:ffe8::", "2c0f:ffe8:ffff:ffff:ffff:ffff:ffff:ffff", "58569106662796955307479896348547874816", "58569106742025117821744233942091825151", "NG", "Nigeria"
130
+ "2c0f:fff0::", "2c0f:fff0:ffff:ffff:ffff:ffff:ffff:ffff", "58569107296622255421594597096899477504", "58569107375850417935858934690443427839", "NG", "Nigeria"
130
131
  CSV
131
132
  -> { @loader.parse(@ipv6csv) }.should_not raise_error
132
133
  end
@@ -162,7 +163,7 @@ describe Trifle::Loader do
162
163
  it "should clear existing data" do
163
164
  @loader.load_data @valid_data
164
165
  @loader.clear
165
- @loader.redis.llen(Trifle::KEY).should be == 0
166
+ @loader.redis.llen(@loader.key).should be == 0
166
167
  end
167
168
  end
168
169
 
data/spec/trifle_spec.rb CHANGED
@@ -8,13 +8,14 @@ describe Trifle do
8
8
 
9
9
  describe "#load" do
10
10
  before do
11
- @trifle = Trifle.new(Redis.new)
11
+ @redis = Redis.new
12
+ @trifle = Trifle.new(@redis)
12
13
  @options = { filenames: "foobar.csv" }
13
14
  end
14
15
 
15
16
  it "should pass this to the loader" do
16
17
  loader = mock Trifle::Loader
17
- Trifle::Loader.should_receive(:new).and_return(loader)
18
+ Trifle::Loader.should_receive(:new).with(@redis, key: @trifle.key).and_return(loader)
18
19
  loader.should_receive(:handle).with(@options)
19
20
  @trifle.load @options
20
21
  end
@@ -22,13 +23,14 @@ describe Trifle do
22
23
 
23
24
  describe "#find" do
24
25
  before do
25
- @trifle = Trifle.new(Redis.new)
26
+ @redis = Redis.new
27
+ @trifle = Trifle.new(@redis)
26
28
  @ip = "127.0.0.1"
27
29
  end
28
30
 
29
31
  it "should pass this to the finder" do
30
32
  finder = mock Trifle::Finder
31
- Trifle::Finder.should_receive(:new).and_return(finder)
33
+ Trifle::Finder.should_receive(:new).with(@redis, key: @trifle.key).and_return(finder)
32
34
  finder.should_receive(:handle).with(@ip)
33
35
  @trifle.find @ip
34
36
  end
data/trifle.gemspec CHANGED
@@ -20,5 +20,6 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_development_dependency 'rspec'
22
22
  s.add_development_dependency 'rujitsu'
23
+ s.add_development_dependency 'active_support'
23
24
  s.add_development_dependency "fakeredis", "~> 0.3.0"
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trifle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-20 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70353203055680 !ruby/object:Gem::Requirement
16
+ requirement: &70340253404740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70353203055680
24
+ version_requirements: *70340253404740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rujitsu
27
- requirement: &70353203079000 !ruby/object:Gem::Requirement
27
+ requirement: &70340253404100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70353203079000
35
+ version_requirements: *70340253404100
36
+ - !ruby/object:Gem::Dependency
37
+ name: active_support
38
+ requirement: &70340253403320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70340253403320
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: fakeredis
38
- requirement: &70353203078460 !ruby/object:Gem::Requirement
49
+ requirement: &70340253402820 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: 0.3.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70353203078460
57
+ version_requirements: *70340253402820
47
58
  description: Stores the GeoIP databases in Redis and gives it a simple way to lookup
48
59
  IPs and map them to countries
49
60
  email: