gamifier 1.0.8 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,37 +23,112 @@ describe Gamifier::Engine do
23
23
  end
24
24
 
25
25
  describe "#uri_to" do
26
+
26
27
  it "should correctly concatenate URIs" do
27
28
  @engine.uri_to.to_s.should == "http://somewhere.ltd/path/1234/"
28
29
  @engine.uri_to("resource").to_s.should == "http://somewhere.ltd/path/1234/resource"
29
30
  @engine.uri_to("/resource").to_s.should == "http://somewhere.ltd/resource"
30
31
  @engine.uri_to("../resource").to_s.should == "http://somewhere.ltd/path/resource"
31
32
  end
33
+
32
34
  end
33
35
 
34
36
  describe "configuration" do
37
+
35
38
  before do
36
39
  @engine = Gamifier::Engine.new(:key => "new-key")
37
40
  end
41
+
38
42
  it "should overwrite the global configuration with the given parameters at intialization time" do
39
43
  @engine.config.should == {:uri => "http://somewhere.ltd/path/", :key => "new-key"}
40
44
  Gamifier.config.should == {:uri => "http://somewhere.ltd/path/", :key => "1234"}
41
45
  end
42
46
 
43
47
  describe "#ok?" do
44
- it "should be true" do
45
- @engine.should be_ok
46
- end
47
48
 
48
- it "should be false if the key is missing" do
49
- @engine.config[:key] = ""
50
- @engine.should_not be_ok
49
+ subject { @engine.ok? }
50
+
51
+ context "with enterprise_key" do
52
+
53
+ before do
54
+ @engine.config[:key] = ''
55
+ @engine.config[:enterprise_key] = '5678'
56
+ end
57
+
58
+ context "with uri" do
59
+
60
+ before do
61
+ @engine.config[:uri] = 'http://foo.com'
62
+ end
63
+
64
+ it { should be_true }
65
+
66
+ end
67
+
68
+ context "without uri" do
69
+
70
+ before do
71
+ @engine.config[:uri] = ''
72
+ end
73
+
74
+ it { should_not be_true }
75
+
76
+ end
77
+
51
78
  end
52
79
 
53
- it "should be false if the uri is missing" do
54
- @engine.config[:uri] = ""
55
- @engine.should_not be_ok
80
+ context "without enterprise_key" do
81
+
82
+ before do
83
+ @engine.config[:enterprise_key] = ''
84
+ end
85
+
86
+ context "with key and uri" do
87
+
88
+ before do
89
+ @engine.config[:key] = '1234'
90
+ @engine.config[:uri] = 'http://foo.com'
91
+ end
92
+
93
+ it { should be_true }
94
+
95
+ end
96
+
97
+ context "with key" do
98
+
99
+ before do
100
+ @engine.config[:key] = '1234'
101
+ @engine.config[:uri] = ''
102
+ end
103
+
104
+ it { should_not be_true }
105
+
106
+ end
107
+
108
+ context "with uri" do
109
+
110
+ before do
111
+ @engine.config[:key] = ''
112
+ @engine.config[:uri] = 'http://foo.com'
113
+ end
114
+
115
+ it { should_not be_true }
116
+
117
+ end
118
+
119
+ context "without key and uri" do
120
+
121
+ before do
122
+ @engine.config[:key] = ''
123
+ @engine.config[:uri] = ''
124
+ end
125
+
126
+ it { should_not be_true }
127
+
128
+ end
129
+
56
130
  end
131
+
57
132
  end
58
133
 
59
134
  end
@@ -71,26 +146,31 @@ describe Gamifier::Engine do
71
146
  end
72
147
 
73
148
  describe "#transmit" do
149
+
74
150
  it "transmits POST requests to the API" do
75
151
  stub_request(:post, "http://somewhere.ltd/path/1234/resource.json").
76
152
  with(:body => {:a => "b", :c => ["d", "e"]}, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
77
153
  @engine.transmit :post, "resource", :body => {"a" => "b", :c => ["d", "e"]}
78
154
  end
155
+
79
156
  it "transmits GET requests to the API" do
80
157
  stub_request(:get, "http://somewhere.ltd/path/1234/resource.json").
81
158
  with(:query => {"limit" => "5"})
82
159
  @engine.transmit :get, "resource", :query => {"limit" => "5"}
83
160
  end
161
+
84
162
  it "should return true if the response is 204" do
85
163
  stub_request(:get, "http://somewhere.ltd/resource.json").
86
164
  to_return(:status => 204)
87
165
  @engine.transmit(:get, "/resource").should be_true
88
166
  end
167
+
89
168
  it "should return the parsed response if the status is in the 2xx range (except 204)" do
90
169
  stub_request(:get, "http://somewhere.ltd/resource.json").
91
170
  to_return(:body => JSON.dump(site), :headers => {'Content-Type' => 'application/json;charset=utf-8'})
92
171
  @engine.transmit(:get, "/resource").should == site
93
172
  end
173
+
94
174
  [:get, :head].each do |method|
95
175
  it "should return nil if the response status is 422 (Unprocessable Entity) and method is #{method}" do
96
176
  stub_request(method, "http://somewhere.ltd/resource.json").
@@ -98,6 +178,7 @@ describe Gamifier::Engine do
98
178
  @engine.transmit(method, "/resource").should be_nil
99
179
  end
100
180
  end
181
+
101
182
  [:delete, :post, :put].each do |method|
102
183
  it "should return false if the response status is 422 (Unprocessable Entity) and method is #{method}" do
103
184
  stub_request(method, "http://somewhere.ltd/resource.json").
@@ -105,11 +186,13 @@ describe Gamifier::Engine do
105
186
  @engine.transmit(method, "/resource").should be_false
106
187
  end
107
188
  end
189
+
108
190
  it "should return nil if the response status is 404" do
109
191
  stub_request(:get, "http://somewhere.ltd/resource.json").
110
192
  to_return(:status => 404)
111
193
  @engine.transmit(:get, "/resource").should be_nil
112
194
  end
195
+
113
196
  it "should raise an HTTPServerError for a 5xx code" do
114
197
  stub_request(:get, "http://somewhere.ltd/resource.json").
115
198
  to_return(:status => 500)
@@ -117,6 +200,7 @@ describe Gamifier::Engine do
117
200
  @engine.transmit(:get, "/resource")
118
201
  }.to raise_error Gamifier::HTTPServerError
119
202
  end
203
+
120
204
  it "should raise an HTTPClientError for a 4xx code" do
121
205
  stub_request(:get, "http://somewhere.ltd/resource.json").
122
206
  to_return(:status => 400)
@@ -124,14 +208,17 @@ describe Gamifier::Engine do
124
208
  @engine.transmit(:get, "/resource")
125
209
  }.to raise_error Gamifier::HTTPClientError
126
210
  end
211
+
127
212
  end
128
213
 
129
214
  describe "collections" do
215
+
130
216
  %w{activities activity_definitions groups players rewards reward_definitions sites units users}.each do |collection|
131
217
  it "should respond ##{collection}" do
132
218
  @engine.should respond_to(collection.to_sym)
133
219
  end
134
220
  end
221
+
135
222
  it "should instantiate a new collection object with the correct model" do
136
223
  collection = @engine.players
137
224
  collection.should be_a(Gamifier::Collection)
@@ -140,4 +227,5 @@ describe Gamifier::Engine do
140
227
  end
141
228
 
142
229
  end
143
- end
230
+
231
+ end
@@ -8,18 +8,18 @@ describe Gamifier do
8
8
 
9
9
  it "should allow to set the API URI" do
10
10
  Gamifier.set :uri, "http://somwhere.ltd"
11
- Gamifier.config[:uri].should == "http://somwhere.ltd"
11
+ Gamifier.config[:uri].should eq "http://somwhere.ltd"
12
12
  end
13
13
 
14
14
  it "should allow to set the API KEY" do
15
15
  Gamifier.set :key, "1234"
16
- Gamifier.config[:key].should == "1234"
16
+ Gamifier.config[:key].should eq "1234"
17
17
  end
18
18
 
19
19
  it "should allow to set multiple configuration options at once" do
20
20
  Gamifier.set :uri => "http://somewhere.ltd", "key" => "1234"
21
- Gamifier.config[:uri].should == "http://somewhere.ltd"
22
- Gamifier.config[:key].should == "1234"
21
+ Gamifier.config[:uri].should eq "http://somewhere.ltd"
22
+ Gamifier.config[:key].should eq "1234"
23
23
  end
24
24
 
25
25
  it "should allow to reset the state" do
@@ -31,10 +31,11 @@ describe Gamifier do
31
31
  it "should accept a logger object" do
32
32
  logger = mock("logger")
33
33
  Gamifier.logger = logger
34
- Gamifier.logger.should == logger
34
+ Gamifier.logger.should eq logger
35
35
  end
36
36
 
37
37
  describe "with proper configuration" do
38
+
38
39
  before do
39
40
  Gamifier.set :uri => "http://somewhere.ltd/path/", :key => "1234"
40
41
  end
@@ -48,13 +49,14 @@ describe Gamifier do
48
49
  it "should return an instance of Gamifier::Engine" do
49
50
  Gamifier.engine.should be_instance_of(Gamifier::Engine)
50
51
  end
51
-
52
+
52
53
  it "should create a new engine object after reset" do
53
54
  engine = Gamifier.engine
54
55
  Gamifier.reset!
55
56
  Gamifier.set :uri => "http://somewhere.ltd/path/", :key => "1234"
56
57
  Gamifier.engine.should_not be_equal(engine)
57
58
  end
59
+
58
60
  end
59
61
 
60
- end
62
+ end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gamifier::Model do
4
+
4
5
  def raw_find
5
6
  '{"data":{"name":null,"created_at":"2012-05-31T04:59:22-07:00","email":"1234@dimelo.com","_id":"4fc75d1a3dc648153b006183"},"paging":null}'
6
7
  end
@@ -31,6 +32,7 @@ describe Gamifier::Model do
31
32
  end
32
33
 
33
34
  describe "model instances" do
35
+
34
36
  before do
35
37
  @model = Gamifier::Model.new :x => "y", :a => "b"
36
38
  end
@@ -69,50 +71,63 @@ describe Gamifier::Model do
69
71
  end
70
72
 
71
73
  describe "existing model" do
74
+
72
75
  before do
73
76
  @model.stub!(:new?).and_return false
74
77
  @model.stub!(:path).and_return("models/1234")
75
78
  end
79
+
76
80
  it "should save itself using PUT if not new" do
77
81
  @model.engine.should_receive(:transmit).with(:put, "models/1234", :body => {"model" => hash_including(:x => "y")})
78
82
  @model.save
79
83
  end
84
+
80
85
  it "should destroy itself using DELETE if not new" do
81
86
  @model.engine.should_receive(:transmit).with(:delete, "models/1234")
82
87
  @model.destroy
83
88
  end
89
+
84
90
  it "should update the model with the given attributes" do
85
91
  @model.engine.should_receive(:transmit).with(:put, "models/1234", :body => {"model" => {:key => "value"}})
86
92
  @model.update_attributes(:key => "value")
87
93
  end
94
+
88
95
  end
89
96
 
90
97
  describe "new model" do
98
+
91
99
  before do
92
100
  @model.stub!(:new?).and_return true
93
101
  end
102
+
94
103
  it "should save itself using POST if new" do
95
104
  @model.engine.should_receive(:transmit).with(:post, "models", :body => {"model" => hash_including(:x => "y")})
96
105
  @model.save
97
106
  end
107
+
98
108
  it "should return true if destroying and new" do
99
109
  @model.destroy.should be_true
100
110
  end
111
+
101
112
  it "should raise an error if trying to call update_attributes" do
102
113
  expect{ @model.update_attributes(:key => "value") }.to raise_error(Gamifier::Error)
103
114
  end
115
+
104
116
  end
105
117
 
106
118
  describe "#payload_for_submission" do
119
+
107
120
  it "should reject any attribute not in the list of the mutable_attributes (if any)" do
108
121
  @model.class.mutable_attributes :last_name
109
122
  @model.first_name = "hello"
110
123
  @model.last_name = "world"
111
124
  @model.payload_for_submission.should == {"model" => {:last_name => "world"}}
112
125
  end
126
+
113
127
  end
114
128
 
115
129
  describe "replace_if_successful" do
130
+
116
131
  it "should replace the attributes if res is a Hash and has a 'data' key" do
117
132
  @model.replace_if_successful(JSON.parse(raw_create)).should == @model
118
133
  @model._id.should == "4fc7661f49f83857cd006044"
@@ -123,33 +138,41 @@ describe Gamifier::Model do
123
138
  @model.replace_if_successful(value).should == value
124
139
  end
125
140
  end
141
+
126
142
  end
127
143
 
128
144
  end
129
145
 
130
146
  describe "FinderMethods" do
147
+
131
148
  before do
132
149
  @engine = mock(Gamifier::Engine)
133
150
  @collection = Gamifier::Collection.new(@engine, Gamifier::Model)
134
151
  end
152
+
135
153
  describe "all" do
154
+
136
155
  it "should issue a get request with no params" do
137
156
  @engine.should_receive(:transmit).with(:get, "models", :query => {:page => 1, :per_page => 50})
138
157
  @collection.all
139
158
  end
159
+
140
160
  it "should issue a get request with the given params" do
141
161
  @engine.should_receive(:transmit).with(:get, "models", :query => {:page => 1, :per_page => 50, :limit => 5})
142
162
  @collection.all :limit => 5
143
163
  end
164
+
144
165
  it "should return a list of entries" do
145
166
  @engine.should_receive(:transmit).and_return({'data' => [{'key' => 'value1'}, {'key' => 'value2'}]})
146
167
  entries = @collection.all
147
168
  entries.length.should == 2
148
169
  entries.all?{|e| e.kind_of?(Gamifier::Model)}.should be_true
149
170
  end
171
+
150
172
  end
151
173
 
152
174
  describe "find" do
175
+
153
176
  it "should return the first entry if data is returned" do
154
177
  @engine.should_receive(:transmit).with(:get, "models/1234@dimelo.com", :query => {}).
155
178
  and_return JSON.parse(raw_find)
@@ -157,12 +180,15 @@ describe Gamifier::Model do
157
180
  user.should be_a(Gamifier::Model)
158
181
  user._id.should == "4fc75d1a3dc648153b006183"
159
182
  end
183
+
160
184
  end
161
185
 
162
186
  describe "find_by_name" do
187
+
163
188
  before do
164
189
  @raw_payload = '{"data":[{"name":"karma","label":"Karma","normalized_name":"unit_karma","type":"point","abbreviation":"kar","display_priority":0},{"name":"yoh","label":"Yoh","normalized_name":"unit_yoh","type":"point","abbreviation":"yoh","display_priority":0},{"name":"karma","label":"Karma","normalized_name":"unit_karma","type":"point","abbreviation":"kar","display_priority":0}],"paging":{}}'
165
190
  end
191
+
166
192
  it "should return the first entry if a matching entry can be found" do
167
193
  @engine.should_receive(:transmit).with(:get, "models", instance_of(Hash)).
168
194
  and_return JSON.parse(@raw_payload)
@@ -170,13 +196,16 @@ describe Gamifier::Model do
170
196
  entry.should be_a(Gamifier::Model)
171
197
  entry.label.should == "Karma"
172
198
  end
199
+
173
200
  it "should return nil if no entry can be found" do
174
201
  @engine.should_receive(:transmit).with(:get, "models", instance_of(Hash)).
175
202
  and_return JSON.parse('{"data": [], "paging": {}}')
176
203
  entry = @collection.find_by_name("karma")
177
204
  entry.should be_nil
178
205
  end
206
+
179
207
  end
180
208
 
181
209
  end
210
+
182
211
  end
@@ -10,9 +10,9 @@ describe Gamifier::Player do
10
10
 
11
11
  subject { Gamifier::ActivityDefinition.new.limit(1, 24.0) }
12
12
 
13
- it { subject.enable_rate_limiting.should be_true }
14
- it { subject.bucket_max_capacity.should == 1 }
15
- it { subject.bucket_drain_rate.should == 1 / 24.0 }
13
+ its(:enable_rate_limiting) { should be_true }
14
+ its(:bucket_max_capacity) { should eq(1) }
15
+ its(:bucket_drain_rate) { should eq(1 / 24.0) }
16
16
 
17
17
  end
18
18
 
@@ -20,9 +20,9 @@ describe Gamifier::Player do
20
20
 
21
21
  subject { Gamifier::ActivityDefinition.new.limit_per_day(1) }
22
22
 
23
- it { subject.enable_rate_limiting.should be_true }
24
- it { subject.bucket_max_capacity.should == 1 }
25
- it { subject.bucket_drain_rate.should == 1 / 24.0 }
23
+ its(:enable_rate_limiting) { should be_true }
24
+ its(:bucket_max_capacity) { should eq(1) }
25
+ its(:bucket_drain_rate) { should eq(1 / 24.0) }
26
26
 
27
27
  end
28
28
 
@@ -30,9 +30,9 @@ describe Gamifier::Player do
30
30
 
31
31
  subject { Gamifier::ActivityDefinition.new.limit_per_week(1) }
32
32
 
33
- it { subject.enable_rate_limiting.should be_true }
34
- it { subject.bucket_max_capacity.should == 1 }
35
- it { subject.bucket_drain_rate.should == 1 / (24.0 * 7.0) }
33
+ its(:enable_rate_limiting) { should be_true }
34
+ its(:bucket_max_capacity) { should eq(1) }
35
+ its(:bucket_drain_rate) { should eq(1 / (24.0 * 7.0)) }
36
36
 
37
37
  end
38
38
 
@@ -40,9 +40,9 @@ describe Gamifier::Player do
40
40
 
41
41
  subject { Gamifier::ActivityDefinition.new.limit_once_per_day }
42
42
 
43
- it { subject.enable_rate_limiting.should be_true }
44
- it { subject.bucket_max_capacity.should == 1 }
45
- it { subject.bucket_drain_rate.should == 1 / 24.0 }
43
+ its(:enable_rate_limiting) { should be_true }
44
+ its(:bucket_max_capacity) { should eq(1) }
45
+ its(:bucket_drain_rate) { should eq(1 / 24.0) }
46
46
 
47
47
  end
48
48
 
@@ -50,9 +50,9 @@ describe Gamifier::Player do
50
50
 
51
51
  subject { Gamifier::ActivityDefinition.new.limit_once_per_week }
52
52
 
53
- it { subject.enable_rate_limiting.should be_true }
54
- it { subject.bucket_max_capacity.should == 1 }
55
- it { subject.bucket_drain_rate.should == 1 / (24.0 * 7.0) }
53
+ its(:enable_rate_limiting) { should be_true }
54
+ its(:bucket_max_capacity) { should eq(1) }
55
+ its(:bucket_drain_rate) { should eq(1 / (24.0 * 7.0)) }
56
56
 
57
57
  end
58
58
 
@@ -1,15 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gamifier::Player do
4
- before do
5
- @engine = mock(Gamifier::Engine)
6
- end
4
+
5
+ let(:engine) { mock(Gamifier::Engine) }
6
+
7
7
  describe "crediting" do
8
+
8
9
  it "should create a new activity and save it" do
9
10
  player = Gamifier::Player.new(:_id => "1234")
10
- player.engine = @engine
11
+ player.engine = engine
11
12
 
12
- @engine.should_receive(:activities).
13
+ engine.should_receive(:activities).
13
14
  and_return(collection = mock(Gamifier::Collection))
14
15
  collection.should_receive(:build).
15
16
  with(:data1 => "value", :player_id => "1234", :verb => "event").
@@ -17,11 +18,13 @@ describe Gamifier::Player do
17
18
 
18
19
  player.credit('event', :data1 => "value").should == true
19
20
  end
21
+
20
22
  end
21
-
23
+
22
24
  describe "FinderMethods" do
25
+
23
26
  before do
24
- @collection = Gamifier::Collection.new(@engine, Gamifier::Player)
27
+ @collection = Gamifier::Collection.new(engine, Gamifier::Player)
25
28
  end
26
29
 
27
30
  it "should have included all the methods" do
@@ -31,21 +34,25 @@ describe Gamifier::Player do
31
34
  end
32
35
 
33
36
  describe "find_by_site_and_email" do
37
+
34
38
  it "should issue a get request with the correct params" do
35
- @engine.should_receive(:transmit).with(:get, "players", :query => {:site=>"test.ltd", :email=>"hi@hello.com", :limit => 5})
39
+ engine.should_receive(:transmit).with(:get, "players", :query => {:site=>"test.ltd", :email=>"hi@hello.com", :limit => 5})
36
40
  @collection.find_by_site_and_email 'test.ltd', 'hi@hello.com', :limit => 5
37
41
  end
42
+
38
43
  end
39
-
44
+
40
45
  describe "find_by_player_id" do
41
-
46
+
42
47
  let(:player_id) { '123456789' }
43
-
48
+
44
49
  it "should issue a get request with the correct params" do
45
- @engine.should_receive(:transmit).with(:get, "players/#{player_id}")
50
+ engine.should_receive(:transmit).with(:get, "players/#{player_id}")
46
51
  @collection.find_by_player_id(player_id)
47
52
  end
48
-
53
+
49
54
  end
55
+
50
56
  end
51
- end
57
+
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gamifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - .gitignore
102
+ - .rspec
102
103
  - Gemfile
103
104
  - LICENSE
104
105
  - README.md
@@ -109,6 +110,7 @@ files:
109
110
  - lib/gamifier.rb
110
111
  - lib/gamifier/collection.rb
111
112
  - lib/gamifier/dsl.rb
113
+ - lib/gamifier/dsl/api_key.rb
112
114
  - lib/gamifier/dsl/network.rb
113
115
  - lib/gamifier/dsl/site.rb
114
116
  - lib/gamifier/engine.rb
@@ -116,7 +118,9 @@ files:
116
118
  - lib/gamifier/model.rb
117
119
  - lib/gamifier/models/activity.rb
118
120
  - lib/gamifier/models/activity_definition.rb
121
+ - lib/gamifier/models/api_key.rb
119
122
  - lib/gamifier/models/group.rb
123
+ - lib/gamifier/models/network.rb
120
124
  - lib/gamifier/models/player.rb
121
125
  - lib/gamifier/models/reward.rb
122
126
  - lib/gamifier/models/reward_definition.rb
@@ -132,6 +136,7 @@ files:
132
136
  - spec/spec_helper.rb
133
137
  - spec/spec_integration_helper.rb
134
138
  - spec/unit/collection_spec.rb
139
+ - spec/unit/dsl/api_key_spec.rb
135
140
  - spec/unit/dsl/network_spec.rb
136
141
  - spec/unit/dsl/site_spec.rb
137
142
  - spec/unit/dsl_spec.rb
@@ -172,6 +177,7 @@ test_files:
172
177
  - spec/spec_helper.rb
173
178
  - spec/spec_integration_helper.rb
174
179
  - spec/unit/collection_spec.rb
180
+ - spec/unit/dsl/api_key_spec.rb
175
181
  - spec/unit/dsl/network_spec.rb
176
182
  - spec/unit/dsl/site_spec.rb
177
183
  - spec/unit/dsl_spec.rb