punching_bag 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe PunchingBag do
4
+ let(:article) { Article.create title: 'Hector', content: 'Ding, ding ding... ding. Ding. DING. DING! ' }
5
+ let(:human_request) { OpenStruct.new(bot?: false) }
6
+ let(:bot_request) { OpenStruct.new(bot?: true) }
7
+
8
+ subject { PunchingBag }
9
+
10
+ describe '.punch' do
11
+ it 'does nothing when the request is from a bot' do
12
+ expect(PunchingBag.punch(article, bot_request)).to be_false
13
+ end
14
+
15
+ it 'creates a new punch when the request is valid' do
16
+ expect { PunchingBag.punch(article, human_request) }.to change { Punch.count }.by 1
17
+ end
18
+
19
+ it 'creates a new punch when there is no request' do
20
+ expect { PunchingBag.punch(article) }.to change { Punch.count }.by 1
21
+ end
22
+ end
23
+
24
+ describe '.average_time' do
25
+ let(:time) { Time.zone.now.beginning_of_day }
26
+ let(:punch_1) { Punch.new(average_time: time + 15.seconds, hits: 2) }
27
+ let(:punch_2) { Punch.new(average_time: time + 30.seconds, hits: 4) }
28
+
29
+ it 'finds an average time for multiple punches' do
30
+ expect(PunchingBag.average_time(punch_1, punch_2)).to eql (time + 25.seconds)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe Punch do
4
+ let(:day) { Time.now.beginning_of_day }
5
+ let(:month) { Time.now.beginning_of_month }
6
+ let(:year) { Time.now.beginning_of_year }
7
+
8
+ let(:attrs) { {} }
9
+ let(:article) { Article.create title: 'Bluths', content: "I know, I just call her Annabelle cause she's shaped like a... she's the belle of the ball!" }
10
+ let(:punch) { Punch.new attrs.merge(punchable: article) }
11
+
12
+ subject { punch }
13
+ before { subject.valid? } # sets default values
14
+
15
+ context 'with one hit' do
16
+ its(:hits) { should eql 1 }
17
+ its(:jab?) { should be_true }
18
+ its(:combo?) { should be_false }
19
+ end
20
+
21
+ context 'with two hits' do
22
+ let(:attrs) { {hits: 2} }
23
+
24
+ its(:hits) { should eql 2 }
25
+ its(:jab?) { should be_false }
26
+ its(:combo?) { should be_true }
27
+ end
28
+
29
+ context 'with start time same as end time' do
30
+ its(:timeframe) { should eql :second }
31
+ its(:day_combo?) { should be_false }
32
+ its(:month_combo?) { should be_false }
33
+ its(:year_combo?) { should be_false }
34
+ end
35
+
36
+ context 'with start time in the same day as end time' do
37
+ let(:attrs) { {starts_at: day + 1.hour, ends_at: day + 2.hours } }
38
+
39
+ its(:timeframe) { should eql :day }
40
+ its(:day_combo?) { should be_true }
41
+ its(:month_combo?) { should be_false }
42
+ its(:year_combo?) { should be_false }
43
+ end
44
+
45
+ context 'with start time in the same month as end time' do
46
+ let(:attrs) { {starts_at: month + 1.day, ends_at: month + 2.days } }
47
+
48
+ its(:timeframe) { should eql :month }
49
+ its(:day_combo?) { should be_false }
50
+ its(:month_combo?) { should be_true }
51
+ its(:year_combo?) { should be_false }
52
+ end
53
+
54
+ context 'with start time in the same year as end time' do
55
+ let(:attrs) { {starts_at: year + 1.month, ends_at: year + 2.months } }
56
+
57
+ its(:timeframe) { should eql :year }
58
+ its(:day_combo?) { should be_false }
59
+ its(:month_combo?) { should be_false }
60
+ its(:year_combo?) { should be_true }
61
+ end
62
+
63
+ context 'with another punch on the same day' do
64
+ let(:attrs) { {hits: 1, starts_at: day + 1.hour } }
65
+ let!(:other_punch) { Punch.create punchable: article, starts_at: day + 2.hours }
66
+ let!(:next_week_punch) { Punch.create punchable: article, starts_at: day + 7.days }
67
+
68
+ before { punch.save! }
69
+
70
+ describe '#combine_with' do
71
+ it 'destroys the punch' do
72
+ expect { punch.combine_with other_punch }.to change { punch.destroyed? }.from(false).to true
73
+ end
74
+
75
+ it 'combines the hits' do
76
+ expect { punch.combine_with other_punch }.to change { other_punch.hits }.from(1).to 2
77
+ end
78
+
79
+ it 'changes starts_at or ends_at' do
80
+ expect { punch.combine_with other_punch }.to change { other_punch.starts_at }.from(day + 2.hours).to(day + 1.hour)
81
+ end
82
+
83
+ it 'changes the average_time' do
84
+ expect { punch.combine_with other_punch }.to change { other_punch.average_time }.from(day + 2.hours).to(day + 90.minutes)
85
+ end
86
+ end
87
+
88
+ describe '#find_combo_for' do
89
+ it 'finds the other punch in the day' do
90
+ expect(punch.find_combo_for(:day)).to eql other_punch
91
+ end
92
+
93
+ it "does't find the next week punch" do
94
+ expect(punch.find_combo_for(:day)).to_not eql next_week_punch
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Article do
4
+ let(:article_1) { Article.create title: 'Bacon', content: 'Bacon ipsum dolor sit amet turkey short ribs tri-tip' }
5
+ let(:article_2) { Article.create title: 'Hipsters', content: 'American Apparel aute Banksy officia ugh.' }
6
+ let(:article_3) { Article.create title: 'Lebowski', content: 'Lebowski ipsum over the line! Dolor sit amet, consectetur adipiscing elit praesent ac.' }
7
+
8
+ # Instance methods
9
+ describe 'Article' do
10
+ subject { article_1 }
11
+
12
+ describe '#hits' do
13
+ context 'with no hits' do
14
+ its(:hits) { should eql 0 }
15
+ end
16
+
17
+ context 'with one hit' do
18
+ before { subject.punch }
19
+ its(:hits) { should eql 1 }
20
+ end
21
+ end
22
+
23
+ describe '#punch' do
24
+ it 'incleases hits by one' do
25
+ expect { subject.punch }.to change { subject.hits }.by 1
26
+ end
27
+ end
28
+ end
29
+
30
+ # Class methods
31
+ describe 'Article' do
32
+ subject { Article }
33
+
34
+ before do
35
+ 2.times { article_3.punch }
36
+ article_1.punch
37
+ end
38
+
39
+ describe '.most_hit' do
40
+ its(:most_hit) { should include article_3 }
41
+ its(:most_hit) { should include article_1 }
42
+ its(:most_hit) { should_not include article_2 }
43
+
44
+ its('most_hit.first') { should eql article_3 }
45
+ its('most_hit.second') { should eql article_1 }
46
+ end
47
+
48
+ describe '.sort_by_popularity' do
49
+ its(:sort_by_popularity) { should include article_1 }
50
+ its(:sort_by_popularity) { should include article_2 }
51
+ its(:sort_by_popularity) { should include article_3 }
52
+
53
+ its('sort_by_popularity.first') { should eql article_3 }
54
+ its('sort_by_popularity.second') { should eql article_1 }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'bundler/setup'
4
+ require 'combustion'
5
+
6
+ Bundler.require :default
7
+
8
+ Combustion.initialize! :active_record
9
+
10
+ require 'rspec/rails'
11
+ require 'rspec/autorun'
12
+
13
+ require 'active_support/time'
14
+ require 'ostruct'
15
+
16
+ RSpec.configure do |config|
17
+ config.use_transactional_fixtures = true
18
+ end
metadata CHANGED
@@ -1,29 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: punching_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Crownoble
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-02 00:00:00.000000000 Z
11
+ date: 2013-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: voight_kampff
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ~>
31
+ - - '>='
18
32
  - !ruby/object:Gem::Version
19
- version: 0.1.1
33
+ version: 0.2.0
20
34
  type: :runtime
21
35
  prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 4.0.0
48
+ type: :development
49
+ prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
52
  - - ~>
25
53
  - !ruby/object:Gem::Version
26
- version: 0.1.1
54
+ version: 4.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: combustion
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
27
97
  description: PunchingBag is a hit counting and simple trending engine for Ruby on
28
98
  Rails
29
99
  email: adam@obledesign.com
@@ -31,6 +101,7 @@ executables: []
31
101
  extensions: []
32
102
  extra_rdoc_files: []
33
103
  files:
104
+ - MIT-LICENSE
34
105
  - app/models/punch.rb
35
106
  - lib/punching_bag/version.rb
36
107
  - lib/punching_bag/engine.rb
@@ -40,6 +111,15 @@ files:
40
111
  - lib/generators/punching_bag/templates/create_punches_table.rb
41
112
  - lib/punching_bag.rb
42
113
  - lib/tasks/punching_bag.rake
114
+ - spec/spec_helper.rb
115
+ - spec/lib/punching_bag_spec.rb
116
+ - spec/models/punch_spec.rb
117
+ - spec/models/punchable_spec.rb
118
+ - spec/internal/db/schema.rb
119
+ - spec/internal/db/combustion_test.sqlite
120
+ - spec/internal/config/database.yml
121
+ - spec/internal/app/models/article.rb
122
+ - spec/internal/log/test.log
43
123
  homepage: https://github.com/biola/punching_bag
44
124
  licenses: []
45
125
  metadata: {}
@@ -63,4 +143,13 @@ rubygems_version: 2.0.3
63
143
  signing_key:
64
144
  specification_version: 4
65
145
  summary: PunchingBag hit conter and trending plugin
66
- test_files: []
146
+ test_files:
147
+ - spec/spec_helper.rb
148
+ - spec/lib/punching_bag_spec.rb
149
+ - spec/models/punch_spec.rb
150
+ - spec/models/punchable_spec.rb
151
+ - spec/internal/db/schema.rb
152
+ - spec/internal/db/combustion_test.sqlite
153
+ - spec/internal/config/database.yml
154
+ - spec/internal/app/models/article.rb
155
+ - spec/internal/log/test.log