hide_and_seek 0.0.4 → 0.0.5

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: e6bf4e77c74f301d4926d042aa2c214737f8313d
4
- data.tar.gz: 6a75a4359ffbeafc02e699082621fdee30b8b028
3
+ metadata.gz: e211ac4514e856978526436d162c72fb8a9f93ca
4
+ data.tar.gz: 8244ea52ff3ef1f62b3c1a751849deac3882dece
5
5
  SHA512:
6
- metadata.gz: fee4d3701b107bc43e39cefa779c59c01453ed03d5605bb4cb06dbf1729919ce338bb16c37b85062ac145be39b1ba1e52b63e4c61735b487c5ff9966d31ed513
7
- data.tar.gz: 6d66a72405dc445aec7fcd8be513d05d6b489d2c6a1d743d39274e0218955645e0eb03b34aab4d70a6d522383d890211ada45bb563ea2895c94273065c13c074
6
+ metadata.gz: 5cc6990c1e8ba613dc1758c3778cc532a5ed41e96a303ea516dc6746a42204df3c8a53f3c8fa47a91a566152c068f6ce359cb2d0698bf0a226c1ef57fcc1f668
7
+ data.tar.gz: 2f409298c88886922477c68e0f3be84d196d34cec3ed11b425f09c0e6f6292e2e3571989414fda7988f763d2fed7d03b5777763bc70453f708ec016994f91b3c
@@ -2,12 +2,10 @@ require_dependency "hide_and_seek/application_controller"
2
2
 
3
3
  module HideAndSeek
4
4
  class ItemsController < ApplicationController
5
- before_filter :determine_identifier
6
-
7
5
  def show
8
6
  respond_to do |format|
9
7
  format.json {
10
- render json: [display: $hide_and_seek.display?(params[:id], @id)]
8
+ render json: [display: hide_and_seek.display?]
11
9
  }
12
10
  end
13
11
  end
@@ -15,20 +13,24 @@ module HideAndSeek
15
13
  def update
16
14
  respond_to do |format|
17
15
  format.json do
18
- if $hide_and_seek.hide(params[:id], @id)
16
+ if hide_and_seek.hide
19
17
  render status: 200, json: [true]
20
18
  else
21
19
  render status: 502, json: [false]
22
20
  end
23
21
  end
24
22
  end
23
+ end
25
24
 
25
+ def hide_and_seek
26
+ @hide_and_seek ||= HideAndSeek::Item.new(params[:item_name], user_identifier)
26
27
  end
28
+ hide_action :hide_and_seek
27
29
 
28
30
  private
29
- def determine_identifier
30
- @id = params[:user_id] || current_user.id
31
- end
32
31
 
32
+ def user_identifier
33
+ params[:user_id] || current_user.id
34
+ end
33
35
  end
34
36
  end
@@ -1,28 +1,26 @@
1
1
  class HideAndSeek::Item
2
- attr_accessor :item_name, :user_identifier
2
+ cattr_accessor :storage, instance_writer: false
3
+ attr_reader :item_name, :user_identifier
3
4
 
4
- def initialize(storage)
5
- @storage = storage
6
- end
7
-
8
- def display?(item_name, user_identifier)
5
+ def initialize(item_name, user_identifier)
9
6
  @item_name = item_name
10
7
  @user_identifier = user_identifier
11
- return false if $redis.exists(key_name)
8
+ end
9
+
10
+ def display?
11
+ return false if storage.exists(key_name)
12
12
  return true
13
13
  end
14
14
 
15
- def hide(item_name, user_identifier)
16
- @item_name = item_name
17
- @user_identifier = user_identifier
18
- if $redis.set(key_name, Time.now) == "OK"
15
+ def hide
16
+ if storage.set(key_name, Time.current) == "OK"
19
17
  return true
20
18
  end
21
19
  return false
22
20
  end
23
21
 
24
22
  def key_name
25
- "#{@item_name}-#{@user_identifier}"
23
+ "#{item_name}-#{user_identifier}"
26
24
  end
27
25
 
28
26
  end
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  HideAndSeek::Engine.routes.draw do
2
- resources :items, only: [:show, :update]
2
+ resources :items, only: [:show, :update], param: :item_name
3
3
  end
@@ -7,7 +7,7 @@ module HideAndSeek
7
7
  end
8
8
 
9
9
  Engine.routes.draw do
10
- resources :items, only: [:show, :update]
10
+ resources :items, only: [:show, :update], param: :item_name
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,22 @@
1
+ module HideAndSeek
2
+ module Helpers
3
+ # Checks if an item should be displayed for a
4
+ # given user.
5
+ #
6
+ # @param item_name [String]
7
+ # @param user_identifier [Integer]
8
+ # @return [true, false]
9
+ def display?(item_name, user_identifier)
10
+ HideAndSeek::Item.new(item_name, user_identifier).display?
11
+ end
12
+
13
+ # Hides an item for a given user.
14
+ #
15
+ # @param item_name [String]
16
+ # @param user_identifier [Integer]
17
+ # @return [true, false]
18
+ def hide(item_name, user_identifier)
19
+ HideAndSeek::Item.new(item_name, user_identifier).hide
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module HideAndSeek
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,55 +1,67 @@
1
- require 'spec_helper'
2
-
3
- describe HideAndSeek::ItemsController, :type => :controller do
1
+ RSpec.describe HideAndSeek::ItemsController, :type => :controller do
4
2
  routes { HideAndSeek::Engine.routes }
5
3
 
6
- let(:current_user){double("User", :id => 1)}
4
+ let(:current_user){ double("User", :id => 1) }
7
5
 
8
6
  before do
9
7
  allow(controller).to receive(:current_user).and_return(current_user)
10
8
  end
11
9
 
10
+ describe '#hide_and_seek' do
11
+ before { controller.params.merge!(item_name: 'foo') }
12
+
13
+ context 'when a user_id parameter has not been provided' do
14
+ it "instantiates a HideAndSeek::Item for the current user" do
15
+ expect(HideAndSeek::Item).to receive(:new).with('foo', 1)
16
+ controller.hide_and_seek
17
+ end
18
+ end
19
+
20
+ context 'when a user_id parameter has been provided' do
21
+ before { controller.params.merge!(user_id: 9001) }
22
+
23
+ it "instantiates a HideAndSeek::Item for the requested user" do
24
+ expect(HideAndSeek::Item).to receive(:new).with('foo', 9001)
25
+ controller.hide_and_seek
26
+ end
27
+ end
28
+ end
12
29
 
13
- context "#show" do
30
+ describe "#show" do
14
31
  it "should check hide_and_seek with the id and the current user" do
15
- expect($hide_and_seek).to receive(:display?).with("foo", 1).and_return(true)
16
- get :show, id: "foo", format: :json
32
+ expect_any_instance_of(HideAndSeek::Item).to receive(:display?).and_return(true)
33
+ get :show, item_name: "foo", format: :json
17
34
  end
35
+
18
36
  it "should render json display => true if successful" do
19
- allow($hide_and_seek).to receive(:display?).and_return true
20
- get :show, id: "foo", format: :json
37
+ allow_any_instance_of(HideAndSeek::Item).to receive(:display?).and_return true
38
+ get :show, item_name: "foo", format: :json
21
39
  expect(response.body).to eq([display: true].to_json)
22
40
  end
41
+
23
42
  it "should render json display => false if unsuccessful" do
24
- allow($hide_and_seek).to receive(:display?).and_return false
25
- get :show, id: "foo", format: :json
43
+ allow_any_instance_of(HideAndSeek::Item).to receive(:display?).and_return false
44
+ get :show, item_name: "foo", format: :json
26
45
  expect(response.body).to eq([display: false].to_json)
27
46
  end
28
- it "should params[:user_id] over current_user if sent" do
29
- expect($hide_and_seek).to receive(:display?).with("foo", 9001).and_return(true)
30
- get :show, id: "foo", format: :json, user_id: 9001
31
- end
32
47
  end
33
48
 
34
- context "#update" do
49
+ describe "#update" do
35
50
  it "should hide the item for the current_user" do
36
- expect($hide_and_seek).to receive(:hide).with("foo", 1)
37
- patch :update, id: "foo", format: :json
51
+ expect_any_instance_of(HideAndSeek::Item).to receive(:hide)
52
+ patch :update, item_name: "foo", format: :json
38
53
  end
54
+
39
55
  it "should render 200" do
40
- allow($hide_and_seek).to receive(:hide).and_return true
41
- patch :update, id: "foo", format: :json
56
+ allow_any_instance_of(HideAndSeek::Item).to receive(:hide).and_return true
57
+ patch :update, item_name: "foo", format: :json
42
58
  expect(response.status).to eq(200)
43
59
  end
60
+
44
61
  it "should respond with 502 if it can't be saved." do
45
- allow($hide_and_seek).to receive(:hide).and_return false
46
- patch :update, id: "foo", format: :json
62
+ allow_any_instance_of(HideAndSeek::Item).to receive(:hide).and_return false
63
+ patch :update, item_name: "foo", format: :json
47
64
  expect(response.status).to eq(502)
48
65
  end
49
- it "should params[:user_id] over current_user if sent" do
50
- expect($hide_and_seek).to receive(:hide).with("foo", 9001).and_return(true)
51
- patch :update, id: "foo", format: :json, user_id: 9001
52
- end
53
-
54
66
  end
55
67
  end
@@ -1,45 +1,39 @@
1
- require 'spec_helper'
2
-
3
- describe HideAndSeek::Item, :type => :model do
4
- before do
5
- $redis = double(:exists => true, :set => "OK")
6
- end
7
-
8
- subject{HideAndSeek::Item.new $redis}
1
+ RSpec.describe HideAndSeek::Item, :type => :model do
2
+ before { described_class.storage = storage }
3
+ after { described_class.storage = nil }
4
+ let(:storage) { double(:exists => true, :set => "OK") }
5
+ subject(:item) { described_class.new('foo', 1) }
9
6
 
10
7
  describe "#display?" do
11
8
  it "should return false if key exists" do
12
- allow($redis).to receive(:exists).with("foo-1").and_return true
13
- expect(subject.display?("foo", 1)).to be false
9
+ allow(storage).to receive(:exists).with("foo-1").and_return true
10
+ expect(item.display?).to be false
14
11
  end
15
12
  it "should return true if key doesn't exist" do
16
- allow($redis).to receive(:exists).with("foo-1").and_return false
17
- expect(subject.display?("foo", 1)).to be true
13
+ allow(storage).to receive(:exists).with("foo-1").and_return false
14
+ expect(item.display?).to be true
18
15
  end
19
16
  end
20
17
 
21
18
  describe "#key_name" do
22
- it "should join the item name and the user identifier" do
23
- subject.item_name = "foo"
24
- subject.user_identifier = 1
25
- expect(subject.key_name).to eql "foo-1"
19
+ it "joins the item_name and user_identifier." do
20
+ expect(described_class.new('foo', 1).key_name).to eq('foo-1')
26
21
  end
27
-
28
22
  end
29
23
 
30
24
  describe "#hide" do
31
25
  it "should receive a key with the current time" do
32
- expect($redis).to receive(:set).with("foo-1", anything())
33
- subject.hide("foo", 1)
26
+ expect(storage).to receive(:set).with("foo-1", anything())
27
+ item.hide
34
28
  end
35
29
  it "should return true if successful" do
36
- allow($redis).to receive(:set).with("foo-1", anything()).and_return "OK"
37
- expect(subject.hide("foo", 1)).to be true
30
+ allow(storage).to receive(:set).with("foo-1", anything()).and_return "OK"
31
+ expect(item.hide).to be true
38
32
  end
39
33
 
40
34
  it "should return false if unsuccessful" do
41
- allow($redis).to receive(:set).and_return "FALSE"
42
- expect(subject.hide("foo", 1)).to be false
35
+ allow(storage).to receive(:set).and_return "FALSE"
36
+ expect(item.hide).to be false
43
37
  end
44
38
  end
45
39
  end
@@ -1,6 +1,6 @@
1
1
  require 'redis'
2
2
  require 'redis-namespace'
3
3
 
4
- $redis = Redis.new(logger: Rails.logger)
5
- $ns = Redis::Namespace.new("hide-and-seek-#{Rails.env}", :redis => $redis)
6
- $hide_and_seek = HideAndSeek::Item.new($ns)
4
+ redis = Redis.new(logger: Rails.logger)
5
+ ns = Redis::Namespace.new("hide-and-seek-#{Rails.env}", :redis => redis)
6
+ HideAndSeek::Item.storage = ns
@@ -0,0 +1,30 @@
1
+ require 'hide_and_seek/helpers'
2
+
3
+ RSpec.describe HideAndSeek::Helpers, type: :helper do
4
+ let(:storage) { double(exists: true, set: 'OK') }
5
+ before { HideAndSeek::Item.storage = storage }
6
+
7
+ describe '#display?' do
8
+ it "instantiates a HideAndSeek::Item with the given item_name and user_identifier" do
9
+ expect(HideAndSeek::Item).to receive(:new).with('foo', 1).and_call_original
10
+ helper.display?('foo', 1)
11
+ end
12
+
13
+ it "calls #display? on the HideAndSee::Item instance" do
14
+ expect_any_instance_of(HideAndSeek::Item).to receive(:display?).once.and_call_original
15
+ helper.display?('foo', 1)
16
+ end
17
+ end
18
+
19
+ describe '#hide' do
20
+ it "instantiates a HideAndSeek::Item with the given item_name and user_identifier" do
21
+ expect(HideAndSeek::Item).to receive(:new).with('foo', 1).and_call_original
22
+ helper.hide('foo', 1)
23
+ end
24
+
25
+ it "calls #hide on the HideAndSee::Item instance" do
26
+ expect_any_instance_of(HideAndSeek::Item).to receive(:hide).once.and_call_original
27
+ helper.hide('foo', 1)
28
+ end
29
+ end
30
+ end
data/spec/spec_helper.rb CHANGED
@@ -22,8 +22,8 @@ RSpec.configure do |config|
22
22
  config.use_transactional_fixtures = true
23
23
  config.infer_base_class_for_anonymous_controllers = false
24
24
  config.order = "random"
25
+ config.disable_monkey_patching!
25
26
  end
26
27
 
27
28
  RSpec.configure do |config|
28
- $redis = Redis.new
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hide_and_seek
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Clark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-12 00:00:00.000000000 Z
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -115,6 +115,7 @@ files:
115
115
  - config/routes.rb
116
116
  - lib/hide_and_seek.rb
117
117
  - lib/hide_and_seek/engine.rb
118
+ - lib/hide_and_seek/helpers.rb
118
119
  - lib/hide_and_seek/version.rb
119
120
  - lib/tasks/hide_and_seek_tasks.rake
120
121
  - spec/hide_and_seek/controllers/items_controller_spec.rb
@@ -159,6 +160,7 @@ files:
159
160
  - spec/internal/public/422.html
160
161
  - spec/internal/public/500.html
161
162
  - spec/internal/public/favicon.ico
163
+ - spec/lib/hide_and_seek/helpers_spec.rb
162
164
  - spec/spec_helper.rb
163
165
  homepage: http://www.github.com/goldstar/hide_and_seek
164
166
  licenses:
@@ -227,5 +229,6 @@ test_files:
227
229
  - spec/internal/public/favicon.ico
228
230
  - spec/internal/Rakefile
229
231
  - spec/internal/README.rdoc
232
+ - spec/lib/hide_and_seek/helpers_spec.rb
230
233
  - spec/spec_helper.rb
231
234
  has_rdoc: