neuron-client 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -50,8 +50,8 @@ module Neuron
50
50
  def configure_membase_connection
51
51
  required(@config, :membase_servers)
52
52
  self.connection = MembaseConnection.new(config.membase_servers,
53
- :local_cache_size => config[:local_cache_size],
54
- :local_cache_expires => config[:local_cache_expires]
53
+ :local_cache_size => config.local_cache_size,
54
+ :local_cache_expires => config.local_cache_expires
55
55
  )
56
56
  end
57
57
 
@@ -5,7 +5,7 @@ module Neuron
5
5
  module Client
6
6
  class MembaseConnection
7
7
 
8
- attr_reader :client
8
+ attr_reader :client, :local_cache
9
9
 
10
10
  def initialize(servers, opts={})
11
11
  @client = Dalli::Client.new(servers)
@@ -22,7 +22,7 @@ module Neuron
22
22
 
23
23
  def fetch(key, ttl=nil, options=nil, &callback)
24
24
  @local_cache.fetch(key, local_ttl(ttl)) do
25
- @client.fetch(key, options, &callback)
25
+ @client.fetch(key, ttl, options, &callback)
26
26
  end
27
27
  end
28
28
 
@@ -9,14 +9,14 @@ module Neuron
9
9
 
10
10
  def total_impressed
11
11
  key = "count_delivery_ad_#{self.id}"
12
- self.class.connection.get(key).to_f
12
+ self.class.connection.get(key,1).to_f
13
13
  end
14
14
 
15
15
  def today_impressed
16
16
  now_adjusted_for_ad_time_zone = Time.now.in_time_zone(self.time_zone)
17
17
  formatted_date = now_adjusted_for_ad_time_zone.strftime('%Y%m%d') # format to YYYYMMDD
18
18
  key = "count_delivery_#{formatted_date}_ad_#{self.id}"
19
- self.class.connection.get(key).to_f
19
+ self.class.connection.get(key,1).to_f
20
20
  end
21
21
 
22
22
  def active?
@@ -33,11 +33,13 @@ module Neuron
33
33
 
34
34
  class << self
35
35
  def find(id)
36
- ad = nil
37
- membase_key = "Ad:#{id}"
38
- cached_json = self.connection.get(membase_key)
39
- ad = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
40
- ad
36
+ self.connection.local_cache.fetch("Neuron::Client::Model::Ad:#{id}") do
37
+ ad = nil
38
+ membase_key = "Ad:#{id}"
39
+ cached_json = self.connection.get(membase_key)
40
+ ad = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
41
+ ad
42
+ end
41
43
  end
42
44
  end
43
45
  end
@@ -5,10 +5,12 @@ module Neuron
5
5
  class BlockedReferer < Common::BlockedReferer
6
6
  class << self
7
7
  def all
8
- blocked_referers = []
9
- cached_json = self.connection.get('blocked_referers')
10
- blocked_referers = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
11
- blocked_referers
8
+ self.connection.local_cache.fetch("Neuron::Client::Model::BlockedReferer:all") do
9
+ blocked_referers = []
10
+ cached_json = self.connection.get('blocked_referers')
11
+ blocked_referers = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
12
+ blocked_referers
13
+ end
12
14
  end
13
15
  end
14
16
  end
@@ -5,10 +5,12 @@ module Neuron
5
5
  class BlockedUserAgent < Common::BlockedUserAgent
6
6
  class << self
7
7
  def all
8
- blocked_user_agents = []
9
- cached_json = self.connection.get('blocked_user_agents')
10
- blocked_user_agents = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
11
- blocked_user_agents
8
+ self.connection.local_cache.fetch("Neuron::Client::Model::BlockedUserAgent:all") do
9
+ blocked_user_agents = []
10
+ cached_json = self.connection.get('blocked_user_agents')
11
+ blocked_user_agents = Yajl.load(cached_json).collect{|item| self.new(item[superclass.resource_name])} if cached_json.present?
12
+ blocked_user_agents
13
+ end
12
14
  end
13
15
  end
14
16
  end
@@ -14,11 +14,13 @@ module Neuron
14
14
 
15
15
  class << self
16
16
  def find(id)
17
- zone = nil
18
- membase_key = "Zone:#{id}"
19
- cached_json = self.connection.get(membase_key)
20
- zone = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
21
- zone
17
+ self.connection.local_cache.fetch("Neuron::Client::Model::Zone:#{id}") do
18
+ zone = nil
19
+ membase_key = "Zone:#{id}"
20
+ cached_json = self.connection.get(membase_key)
21
+ zone = self.new(Yajl.load(cached_json)[superclass.resource_name]) if cached_json.present?
22
+ zone
23
+ end
22
24
  end
23
25
  end
24
26
  end
@@ -1,5 +1,5 @@
1
1
  module Neuron
2
2
  module Client
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
5
5
  end
@@ -7,7 +7,7 @@ module Neuron
7
7
  it "should call the expected methods and return the expected value" do
8
8
  a = Ad.allocate
9
9
  a.should_receive(:id).and_return(7)
10
- Ad.stub_chain(:connection, :get).with('count_delivery_ad_7').and_return('999.999')
10
+ Ad.stub_chain(:connection, :get).with('count_delivery_ad_7',1).and_return('999.999')
11
11
 
12
12
  a.total_impressed.should == 999.999
13
13
  end
@@ -19,7 +19,7 @@ module Neuron
19
19
  a.should_receive(:time_zone).and_return('time_zone_value')
20
20
  Time.stub_chain(:now, :in_time_zone).with('time_zone_value').and_return(Time.parse("2011-05-06 07:08"))
21
21
  a.should_receive(:id).and_return(7)
22
- Ad.stub_chain(:connection, :get).with('count_delivery_20110506_ad_7').and_return('99.99')
22
+ Ad.stub_chain(:connection, :get).with('count_delivery_20110506_ad_7',1).and_return('99.99')
23
23
 
24
24
  a.today_impressed.should == 99.99
25
25
  end
@@ -28,7 +28,9 @@ module Neuron
28
28
  describe "Ad.find(id)" do
29
29
  context "when the connection returns a value" do
30
30
  it "should call the expected methods and return the expected value" do
31
- Ad.stub_chain(:connection, :get).with('Ad:7').and_return('{"ad":{"attr":"value","attr2":"value2"}}')
31
+ connection = MembaseConnection.new("example.com:11211")
32
+ Ad.stub(:connection).and_return(connection)
33
+ connection.stub(:get).with('Ad:7').and_return('{"ad":{"attr":"value","attr2":"value2"}}')
32
34
  a = stub(:ad)
33
35
  Ad.should_receive(:new).with({'attr' => 'value', 'attr2' => 'value2'}).and_return(a)
34
36
 
@@ -37,7 +39,9 @@ module Neuron
37
39
  end
38
40
  context "when the connection returns nil" do
39
41
  it "should call the expected methods and return nil" do
40
- Ad.stub_chain(:connection, :get).with('Ad:7').and_return(nil)
42
+ connection = MembaseConnection.new("example.com:11211")
43
+ Ad.stub(:connection).and_return(connection)
44
+ connection.stub(:get).with('Ad:7').and_return(nil)
41
45
 
42
46
  Ad.find(7).should be_nil
43
47
  end
@@ -6,7 +6,9 @@ module Neuron
6
6
  describe "BlockedReferer.all" do
7
7
  context "when connection returns a value" do
8
8
  it "should call the expected methods and return the expected value" do
9
- BlockedReferer.stub_chain(:connection, :get).with('blocked_referers').and_return('[{"blocked_referer":{"attr":"value"}},{"blocked_referer":{"attr2":"value2"}}]')
9
+ conn = MembaseConnection.new("example.com:11211")
10
+ BlockedReferer.stub(:connection).and_return(conn)
11
+ conn.stub(:get).with('blocked_referers').and_return('[{"blocked_referer":{"attr":"value"}},{"blocked_referer":{"attr2":"value2"}}]')
10
12
  br = stub(:blocked_referer)
11
13
  br2 = stub(:blocked_referer2)
12
14
  BlockedReferer.should_receive(:new).with({'attr' => 'value'}).and_return(br)
@@ -17,7 +19,9 @@ module Neuron
17
19
  end
18
20
  context "when connection returns nil" do
19
21
  it "should call the expected methods and return the expected value" do
20
- BlockedReferer.stub_chain(:connection, :get).with('blocked_referers').and_return(nil)
22
+ conn = MembaseConnection.new("example.com:11211")
23
+ BlockedReferer.stub(:connection).and_return(conn)
24
+ conn.stub(:get).with('blocked_referers').and_return(nil)
21
25
 
22
26
  BlockedReferer.all.should == []
23
27
  end
@@ -6,7 +6,9 @@ module Neuron
6
6
  describe "BlockedUserAgent.all" do
7
7
  context "when connection returns a value" do
8
8
  it "should call the expected methods and return the expected value" do
9
- BlockedUserAgent.stub_chain(:connection, :get).with('blocked_user_agents').and_return('[{"blocked_user_agent":{"attr":"value"}},{"blocked_user_agent":{"attr2":"value2"}}]')
9
+ conn = MembaseConnection.new("example.com:11211")
10
+ BlockedUserAgent.stub(:connection).and_return(conn)
11
+ conn.stub(:get).with('blocked_user_agents').and_return('[{"blocked_user_agent":{"attr":"value"}},{"blocked_user_agent":{"attr2":"value2"}}]')
10
12
  bua = stub(:blocked_user_agent)
11
13
  bua2 = stub(:blocked_user_agent2)
12
14
  BlockedUserAgent.should_receive(:new).with({'attr' => 'value'}).and_return(bua)
@@ -17,7 +19,9 @@ module Neuron
17
19
  end
18
20
  context "when connection returns nil" do
19
21
  it "should call the expected methods and return the expected value" do
20
- BlockedUserAgent.stub_chain(:connection, :get).with('blocked_user_agents').and_return(nil)
22
+ conn = MembaseConnection.new("example.com:11211")
23
+ BlockedUserAgent.stub(:connection).and_return(conn)
24
+ conn.stub(:get).with('blocked_user_agents').and_return(nil)
21
25
 
22
26
  BlockedUserAgent.all.should == []
23
27
  end
@@ -6,7 +6,9 @@ module Neuron
6
6
  describe "Zone.all" do
7
7
  context "when connection.get returns a value" do
8
8
  it "should call the expected methods and return the expected result" do
9
- Zone.stub_chain(:connection, :get).with('Zone:7').and_return('{"zone":{"attr":"value","attr2":"value2"}}')
9
+ conn = MembaseConnection.new("example.com:11211")
10
+ Zone.stub(:connection).and_return(conn)
11
+ conn.stub(:get).with('Zone:7').and_return('{"zone":{"attr":"value","attr2":"value2"}}')
10
12
  z = stub(:zone)
11
13
  Zone.should_receive(:new).with({'attr' => 'value', 'attr2' => 'value2'}).and_return(z)
12
14
 
@@ -15,7 +17,9 @@ module Neuron
15
17
  end
16
18
  context "when connection.get returns nil" do
17
19
  it "should call the expected methods and return nil" do
18
- Zone.stub_chain(:connection, :get).with('Zone:7').and_return(nil)
20
+ conn = MembaseConnection.new("example.com:11211")
21
+ Zone.stub(:connection).and_return(conn)
22
+ conn.stub(:get).with('Zone:7').and_return(nil)
19
23
 
20
24
  Zone.find(7).should be_nil
21
25
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neuron-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - RMM Online