gamifier 1.0.3

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.
Files changed (41) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +60 -0
  5. data/Rakefile +2 -0
  6. data/examples/dev.rb +186 -0
  7. data/examples/preprod.rb +218 -0
  8. data/gamifier.gemspec +24 -0
  9. data/lib/gamifier/collection.rb +26 -0
  10. data/lib/gamifier/dsl/network.rb +33 -0
  11. data/lib/gamifier/dsl/site.rb +134 -0
  12. data/lib/gamifier/dsl.rb +24 -0
  13. data/lib/gamifier/engine.rb +84 -0
  14. data/lib/gamifier/errors.rb +6 -0
  15. data/lib/gamifier/model.rb +212 -0
  16. data/lib/gamifier/models/activity.rb +8 -0
  17. data/lib/gamifier/models/activity_definition.rb +6 -0
  18. data/lib/gamifier/models/group.rb +6 -0
  19. data/lib/gamifier/models/player.rb +20 -0
  20. data/lib/gamifier/models/reward.rb +6 -0
  21. data/lib/gamifier/models/reward_definition.rb +14 -0
  22. data/lib/gamifier/models/site.rb +6 -0
  23. data/lib/gamifier/models/track.rb +6 -0
  24. data/lib/gamifier/models/unit.rb +16 -0
  25. data/lib/gamifier/models/user.rb +6 -0
  26. data/lib/gamifier/version.rb +3 -0
  27. data/lib/gamifier.rb +76 -0
  28. data/spec/integration/dsl_integration_spec.rb +76 -0
  29. data/spec/integration/player_integration_spec.rb +41 -0
  30. data/spec/integration/user_integration_spec.rb +19 -0
  31. data/spec/spec_helper.rb +7 -0
  32. data/spec/spec_integration_helper.rb +51 -0
  33. data/spec/unit/collection_spec.rb +21 -0
  34. data/spec/unit/dsl/network_spec.rb +34 -0
  35. data/spec/unit/dsl/site_spec.rb +54 -0
  36. data/spec/unit/dsl_spec.rb +9 -0
  37. data/spec/unit/engine_spec.rb +135 -0
  38. data/spec/unit/gamifier_spec.rb +60 -0
  39. data/spec/unit/model_spec.rb +182 -0
  40. data/spec/unit/models/player_spec.rb +40 -0
  41. metadata +179 -0
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gamifier::Model do
4
+ def raw_find
5
+ '{"data":{"name":null,"created_at":"2012-05-31T04:59:22-07:00","email":"1234@dimelo.com","_id":"4fc75d1a3dc648153b006183"},"paging":null}'
6
+ end
7
+
8
+ def raw_create
9
+ '{"name": null, "created_at": "2012-05-31T05:37:51-07:00", "email": "1234@test.com", "_id": "4fc7661f49f83857cd006044"}'
10
+ end
11
+
12
+ before do
13
+ Gamifier.set :uri => "http://somewhere.ltd/path/", :key => "1234"
14
+ Gamifier::Model.reset!
15
+ end
16
+
17
+ it "should return the underscored version of the class name" do
18
+ class ActivityDefinition < Gamifier::Model; end
19
+ ActivityDefinition.path.should == "activity_definitions"
20
+ Gamifier::Model.path.should == "models"
21
+ end
22
+
23
+ it "should allow to set a custom path" do
24
+ Gamifier::Model.path.should == "models"
25
+ Gamifier::Model.path "another/path.json"
26
+ Gamifier::Model.path.should == "another/path.json"
27
+ class ActivityDefinition < Gamifier::Model
28
+ path "somewhere"
29
+ end
30
+ ActivityDefinition.path.should == "somewhere"
31
+ end
32
+
33
+ describe "model instances" do
34
+ before do
35
+ @model = Gamifier::Model.new :x => "y", :a => "b"
36
+ end
37
+
38
+ it "should return the attributes Hash" do
39
+ @model.attributes.should == {:x => "y", :a => "b"}
40
+ end
41
+
42
+ it "should forward #engine to Gamifier" do
43
+ @model.engine.should == Gamifier.engine
44
+ end
45
+
46
+ it "should behave like an openstruct" do
47
+ @model.xyz = "yeah"
48
+ @model.xyz.should == "yeah"
49
+ end
50
+
51
+ it "should be instantiable with a hash" do
52
+ model = Gamifier::Model.new :k => "v", "other" => ["1", 2]
53
+ model.k.should == "v"
54
+ model.other.should == ["1", 2]
55
+ end
56
+
57
+ it "should compute the path to itself" do
58
+ @model._id = "1234"
59
+ @model.path.should == "models/1234"
60
+ end
61
+
62
+ it "should be new if no id yet" do
63
+ @model.should be_new
64
+ end
65
+
66
+ it "should not be new if an id already exists" do
67
+ @model._id = "1234"
68
+ @model.should_not be_new
69
+ end
70
+
71
+ describe "existing model" do
72
+ before do
73
+ @model.stub!(:new?).and_return false
74
+ @model.stub!(:path).and_return("models/1234")
75
+ end
76
+ it "should save itself using PUT if not new" do
77
+ @model.engine.should_receive(:transmit).with(:put, "models/1234", :body => {"model" => hash_including(:x => "y")})
78
+ @model.save
79
+ end
80
+ it "should destroy itself using DELETE if not new" do
81
+ @model.engine.should_receive(:transmit).with(:delete, "models/1234")
82
+ @model.destroy
83
+ end
84
+ it "should update the model with the given attributes" do
85
+ @model.engine.should_receive(:transmit).with(:put, "models/1234", :body => {"model" => {:key => "value"}})
86
+ @model.update_attributes(:key => "value")
87
+ end
88
+ end
89
+
90
+ describe "new model" do
91
+ before do
92
+ @model.stub!(:new?).and_return true
93
+ end
94
+ it "should save itself using POST if new" do
95
+ @model.engine.should_receive(:transmit).with(:post, "models", :body => {"model" => hash_including(:x => "y")})
96
+ @model.save
97
+ end
98
+ it "should return true if destroying and new" do
99
+ @model.destroy.should be_true
100
+ end
101
+ it "should raise an error if trying to call update_attributes" do
102
+ expect{ @model.update_attributes(:key => "value") }.to raise_error(Gamifier::Error)
103
+ end
104
+ end
105
+
106
+ describe "#payload_for_submission" do
107
+ it "should reject any attribute not in the list of the mutable_attributes (if any)" do
108
+ @model.class.mutable_attributes :last_name
109
+ @model.first_name = "hello"
110
+ @model.last_name = "world"
111
+ @model.payload_for_submission.should == {"model" => {:last_name => "world"}}
112
+ end
113
+ end
114
+
115
+ describe "replace_if_successful" do
116
+ it "should replace the attributes if res is a Hash and has a 'data' key" do
117
+ @model.replace_if_successful(JSON.parse(raw_create)).should == @model
118
+ @model._id.should == "4fc7661f49f83857cd006044"
119
+ end
120
+
121
+ [true, false, nil].each do |value|
122
+ it "should do nothing if given response is #{value.inspect}" do
123
+ @model.replace_if_successful(value).should == value
124
+ end
125
+ end
126
+ end
127
+
128
+ end
129
+
130
+ describe "FinderMethods" do
131
+ before do
132
+ @engine = mock(Gamifier::Engine)
133
+ @collection = Gamifier::Collection.new(@engine, Gamifier::Model)
134
+ end
135
+ describe "all" do
136
+ it "should issue a get request with no params" do
137
+ @engine.should_receive(:transmit).with(:get, "models", :query => {:page => 1, :per_page => 50})
138
+ @collection.all
139
+ end
140
+ it "should issue a get request with the given params" do
141
+ @engine.should_receive(:transmit).with(:get, "models", :query => {:page => 1, :per_page => 50, :limit => 5})
142
+ @collection.all :limit => 5
143
+ end
144
+ it "should return a list of entries" do
145
+ @engine.should_receive(:transmit).and_return({'data' => [{'key' => 'value1'}, {'key' => 'value2'}]})
146
+ entries = @collection.all
147
+ entries.length.should == 2
148
+ entries.all?{|e| e.kind_of?(Gamifier::Model)}.should be_true
149
+ end
150
+ end
151
+
152
+ describe "find" do
153
+ it "should return the first entry if data is returned" do
154
+ @engine.should_receive(:transmit).with(:get, "models/1234@dimelo.com", :query => {}).
155
+ and_return JSON.parse(raw_find)
156
+ user = @collection.find("1234@dimelo.com")
157
+ user.should be_a(Gamifier::Model)
158
+ user._id.should == "4fc75d1a3dc648153b006183"
159
+ end
160
+ end
161
+
162
+ describe "find_by_name" do
163
+ before do
164
+ @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
+ end
166
+ it "should return the first entry if a matching entry can be found" do
167
+ @engine.should_receive(:transmit).with(:get, "models", instance_of(Hash)).
168
+ and_return JSON.parse(@raw_payload)
169
+ entry = @collection.find_by_name("karma")
170
+ entry.should be_a(Gamifier::Model)
171
+ entry.label.should == "Karma"
172
+ end
173
+ it "should return nil if no entry can be found" do
174
+ @engine.should_receive(:transmit).with(:get, "models", instance_of(Hash)).
175
+ and_return JSON.parse('{"data": [], "paging": {}}')
176
+ entry = @collection.find_by_name("karma")
177
+ entry.should be_nil
178
+ end
179
+ end
180
+
181
+ end
182
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gamifier::Player do
4
+ before do
5
+ @engine = mock(Gamifier::Engine)
6
+ end
7
+ describe "crediting" do
8
+ it "should create a new activity and save it" do
9
+ player = Gamifier::Player.new(:_id => "1234")
10
+ player.engine = @engine
11
+
12
+ @engine.should_receive(:activities).
13
+ and_return(collection = mock(Gamifier::Collection))
14
+ collection.should_receive(:build).
15
+ with(:data1 => "value", :player_id => "1234", :verb => "event").
16
+ and_return(mock(Gamifier::Activity, :save => true))
17
+
18
+ player.credit('event', :data1 => "value").should == true
19
+ end
20
+ end
21
+
22
+ describe "FinderMethods" do
23
+ before do
24
+ @collection = Gamifier::Collection.new(@engine, Gamifier::Player)
25
+ end
26
+
27
+ it "should have included all the methods" do
28
+ (Gamifier::Model::FinderMethods.public_instance_methods + Gamifier::Player::FinderMethods.public_instance_methods).uniq.each do |method|
29
+ @collection.should respond_to(method)
30
+ end
31
+ end
32
+
33
+ describe "find_by_site_and_email" do
34
+ 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})
36
+ @collection.find_by_site_and_email 'test.ltd', 'hi@hello.com', :limit => 5
37
+ end
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gamifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cyril Rohr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.4.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.4.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2'
78
+ - !ruby/object:Gem::Dependency
79
+ name: webmock
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Gem for accessing game engine APIs
95
+ email:
96
+ - cyril.rohr@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - examples/dev.rb
107
+ - examples/preprod.rb
108
+ - gamifier.gemspec
109
+ - lib/gamifier.rb
110
+ - lib/gamifier/collection.rb
111
+ - lib/gamifier/dsl.rb
112
+ - lib/gamifier/dsl/network.rb
113
+ - lib/gamifier/dsl/site.rb
114
+ - lib/gamifier/engine.rb
115
+ - lib/gamifier/errors.rb
116
+ - lib/gamifier/model.rb
117
+ - lib/gamifier/models/activity.rb
118
+ - lib/gamifier/models/activity_definition.rb
119
+ - lib/gamifier/models/group.rb
120
+ - lib/gamifier/models/player.rb
121
+ - lib/gamifier/models/reward.rb
122
+ - lib/gamifier/models/reward_definition.rb
123
+ - lib/gamifier/models/site.rb
124
+ - lib/gamifier/models/track.rb
125
+ - lib/gamifier/models/unit.rb
126
+ - lib/gamifier/models/user.rb
127
+ - lib/gamifier/version.rb
128
+ - spec/integration/dsl_integration_spec.rb
129
+ - spec/integration/player_integration_spec.rb
130
+ - spec/integration/user_integration_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/spec_integration_helper.rb
133
+ - spec/unit/collection_spec.rb
134
+ - spec/unit/dsl/network_spec.rb
135
+ - spec/unit/dsl/site_spec.rb
136
+ - spec/unit/dsl_spec.rb
137
+ - spec/unit/engine_spec.rb
138
+ - spec/unit/gamifier_spec.rb
139
+ - spec/unit/model_spec.rb
140
+ - spec/unit/models/player_spec.rb
141
+ homepage: ''
142
+ licenses: []
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 1.8.21
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Currently supports Badgeville Core API
165
+ test_files:
166
+ - spec/integration/dsl_integration_spec.rb
167
+ - spec/integration/player_integration_spec.rb
168
+ - spec/integration/user_integration_spec.rb
169
+ - spec/spec_helper.rb
170
+ - spec/spec_integration_helper.rb
171
+ - spec/unit/collection_spec.rb
172
+ - spec/unit/dsl/network_spec.rb
173
+ - spec/unit/dsl/site_spec.rb
174
+ - spec/unit/dsl_spec.rb
175
+ - spec/unit/engine_spec.rb
176
+ - spec/unit/gamifier_spec.rb
177
+ - spec/unit/model_spec.rb
178
+ - spec/unit/models/player_spec.rb
179
+ has_rdoc: